Skip to content

Commit

Permalink
Merge pull request #39 from WordPress/fix/correct-namespace-check-for…
Browse files Browse the repository at this point in the history
…-hooks

Correctly enable dashes in regexes in validateNamespace
  • Loading branch information
Adam Silverstein authored Sep 29, 2017
2 parents cc83cde + 3b19868 commit 74212b9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/hooks/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ test( 'cannot add filters with namespaces missing a functionDescription', () =>
);
} );

test( 'Can add filters with dashes in namespaces', () => {
addFilter( 'hook_name', 'my_name/with-dashes/action', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );
} );

test( 'Can add filters with capitals in namespaces', () => {
addFilter( 'hook_name', 'my_name/OhNo/action', () => null );
expect( console.error ).toHaveBeenCalledTimes( 0 );
Expand Down
4 changes: 2 additions & 2 deletions packages/hooks/src/validateNamespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ function validateNamespace( namespace ) {
return false;
}

if ( ! /^[a-zA-Z][a-zA-Z0-9_.-/]*$/.test( namespace ) ) {
if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-/]*$/.test( namespace ) ) {
console.error( 'The namespace can only contain numbers, letters, dashes, periods and underscores, plus the forward slash dividing slug and description in the namespace.' );
return false;
}

if ( ! /^[a-zA-Z][a-zA-Z0-9_.-]*\/[a-zA-Z][a-zA-Z0-9_.-]*\/[a-zA-Z][a-zA-Z0-9_.-]*$/.test( namespace ) ) {
if ( ! /^[a-zA-Z][a-zA-Z0-9_.\-]*\/[a-zA-Z][a-zA-Z0-9_.\-]*\/[a-zA-Z][a-zA-Z0-9_.\-]*$/.test( namespace ) ) {
console.error( 'The namespace must take the form `vendor/plugin/function`.' );
return false;
}
Expand Down

0 comments on commit 74212b9

Please sign in to comment.