-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DataForm: migrate order action modal and introduce form validation (#…
…63895) Co-authored-by: oandregal <oandregal@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
- Loading branch information
1 parent
9f7e26f
commit 654fef6
Showing
7 changed files
with
172 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { isItemValid } from '../validation'; | ||
import type { Field } from '../types'; | ||
|
||
describe( 'validation', () => { | ||
it( 'fields not visible in form are not validated', () => { | ||
const item = { id: 1, valid_order: 2, invalid_order: 'd' }; | ||
const fields: Field< {} >[] = [ | ||
{ | ||
id: 'valid_order', | ||
type: 'integer', | ||
}, | ||
{ | ||
id: 'invalid_order', | ||
type: 'integer', | ||
}, | ||
]; | ||
const form = { visibleFields: [ 'valid_order' ] }; | ||
const result = isItemValid( item, fields, form ); | ||
expect( result ).toBe( true ); | ||
} ); | ||
|
||
it( 'integer field is valid if value is integer', () => { | ||
const item = { id: 1, order: 2, title: 'hi' }; | ||
const fields: Field< {} >[] = [ | ||
{ | ||
type: 'integer', | ||
id: 'order', | ||
}, | ||
]; | ||
const form = { visibleFields: [ 'order' ] }; | ||
const result = isItemValid( item, fields, form ); | ||
expect( result ).toBe( true ); | ||
} ); | ||
|
||
it( 'integer field is invalid if value is not integer', () => { | ||
const item = { id: 1, order: 'd' }; | ||
const fields: Field< {} >[] = [ | ||
{ | ||
id: 'order', | ||
type: 'integer', | ||
}, | ||
]; | ||
const form = { visibleFields: [ 'order' ] }; | ||
const result = isItemValid( item, fields, form ); | ||
expect( result ).toBe( false ); | ||
} ); | ||
|
||
it( 'integer field is invalid if value is empty', () => { | ||
const item = { id: 1, order: '' }; | ||
const fields: Field< {} >[] = [ | ||
{ | ||
id: 'order', | ||
type: 'integer', | ||
}, | ||
]; | ||
const form = { visibleFields: [ 'order' ] }; | ||
const result = isItemValid( item, fields, form ); | ||
expect( result ).toBe( false ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { normalizeFields } from './normalize-fields'; | ||
import type { Field, Form } from './types'; | ||
|
||
export function isItemValid< Item >( | ||
item: Item, | ||
fields: Field< Item >[], | ||
form: Form | ||
): boolean { | ||
const _fields = normalizeFields( | ||
fields.filter( ( { id } ) => !! form.visibleFields?.includes( id ) ) | ||
); | ||
return _fields.every( ( field ) => { | ||
const value = field.getValue( { item } ); | ||
|
||
// TODO: this implicitely means the value is required. | ||
if ( field.type === 'integer' && value === '' ) { | ||
return false; | ||
} | ||
|
||
if ( | ||
field.type === 'integer' && | ||
! Number.isInteger( Number( value ) ) | ||
) { | ||
return false; | ||
} | ||
|
||
// Nothing to validate. | ||
return true; | ||
} ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters