Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/abilities/src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function registerAbility( ability: Ability ) {
// Validate name format matches server implementation
if ( ! ABILITY_NAME_PATTERN.test( ability.name ) ) {
throw new Error(
'Ability name must be a string containing a namespace prefix, i.e. "my-plugin/my-ability". It can only contain lowercase alphanumeric characters, dashes and the forward slash.'
'Ability name must be a string containing a namespace prefix with 2-4 segments, e.g. "my-plugin/my-ability" or "core/posts/find". It can only contain lowercase alphanumeric characters, dashes and the forward slash.'
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/abilities/src/store/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
export const STORE_NAME = 'core/abilities';

// Validation patterns
export const ABILITY_NAME_PATTERN = /^[a-z0-9-]+\/[a-z0-9-]+$/;
export const ABILITY_NAME_PATTERN = /^[a-z0-9-]+(?:\/[a-z0-9-]+){1,3}$/;
export const CATEGORY_SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;

// Action types
Expand Down
37 changes: 35 additions & 2 deletions packages/abilities/src/store/tests/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ describe( 'Store Actions', () => {

it( 'should validate and reject ability with invalid name format', () => {
const testCases = [
'invalid', // No namespace
'my-plugin/feature/action', // Multiple slashes
'invalid', // No namespace (only 1 segment)
'my-plugin/a/b/c/d', // Too many slashes (5 segments)
'My-Plugin/feature', // Uppercase letters
'my_plugin/feature', // Underscores not allowed
'my-plugin/feature!', // Special characters not allowed
Expand Down Expand Up @@ -170,6 +170,39 @@ describe( 'Store Actions', () => {
}
} );

it( 'should accept valid nested namespace ability names (2-4 segments)', () => {
const validNames = [
'test/ability', // 2 segments
'core/posts/find', // 3 segments
'my-plugin/resource/action', // 3 segments
'my-plugin/resource/sub/action', // 4 segments
];

for ( const validName of validNames ) {
const ability: Ability = {
name: validName,
label: 'Test Ability',
description: 'Test description',
category: 'test-category',
callback: jest.fn(),
};

mockSelect.getAbility.mockReturnValue( null );
mockDispatch.mockClear();

const action = registerAbility( ability );
action( { select: mockSelect, dispatch: mockDispatch } );

expect( mockDispatch ).toHaveBeenCalledWith( {
type: REGISTER_ABILITY,
ability: {
...ability,
meta: { annotations: { clientRegistered: true } },
},
} );
}
} );

it( 'should validate and reject ability without label', () => {
const ability: Ability = {
name: 'test/ability',
Expand Down
2 changes: 1 addition & 1 deletion packages/abilities/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type PermissionCallback = (
export interface Ability {
/**
* The unique name/identifier of the ability, with its namespace.
* Example: 'my-plugin/my-ability'
* Supports 2-4 segments (e.g. 'my-plugin/my-ability', 'core/posts/find', 'my-plugin/resource/sub/action').
* @see WP_Ability::get_name()
*/
name: string;
Expand Down
Loading