Skip to content

Commit

Permalink
If elements are provided, the value needs to be one of them
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Jul 26, 2024
1 parent 216234e commit f3053ac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/dataviews/src/test/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,21 @@ describe( 'validation', () => {
const result = isItemValid( item, fields, form );
expect( result ).toBe( false );
} );

it( 'field is invalid if value is not one of the elements', () => {
const item = { id: 1, author: 3 };
const fields: Field< {} >[] = [
{
id: 'author',
type: 'integer',
elements: [
{ value: 1, label: 'Jane' },
{ value: 2, label: 'John' },
],
},
];
const form = { visibleFields: [ 'author' ] };
const result = isItemValid( item, fields, form );
expect( result ).toBe( false );
} );
} );
7 changes: 7 additions & 0 deletions packages/dataviews/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export function isItemValid< Item >(
return false;
}

if ( field.elements ) {
const validValues = field.elements.map( ( f ) => f.value );
if ( ! validValues.includes( value ) ) {
return false;
}
}

// Nothing to validate.
return true;
} );
Expand Down

0 comments on commit f3053ac

Please sign in to comment.