Skip to content

Commit

Permalink
chore: rebase with feat/extend-validators
Browse files Browse the repository at this point in the history
  • Loading branch information
langz committed Sep 6, 2024
1 parent b3c01a4 commit 9f602db
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,23 @@ export const ValidationExtendValidator = () => {
? { status: 'valid' }
: { status: 'invalid' }

const validator = (value, errorMessages) => {
const myValidator = (
value,
{ validators: { dnrValidator, fnrValidator } },
) => {
const result = bornInApril(value)
return result.status === 'invalid'
? new Error(errorMessages.pattern)
: undefined
if (result.status === 'invalid') {
return new Error('My error')
}

return [dnrValidator, fnrValidator]
}

return (
<Field.NationalIdentityNumber
required
value="53050129159"
validator={validator}
validator={myValidator}
validateInitially
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function NationalIdentityNumber(props: Props) {
validator: validate
? props.validator || dnrAndFnrValidator
: undefined,
exportValidators: { dnrValidator, fnrValidator },
}

return <StringField {...StringFieldProps} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,16 @@ describe('Field.NationalIdentityNumber', () => {
? { status: 'valid' }
: { status: 'invalid' }

const customValidator = (value) => {
const customValidator = (
value,
{ validators: { dnrValidator, fnrValidator } }
) => {
const result = bornInApril(value)
return result.status === 'invalid'
? new Error('custom error')
: undefined
if (result.status === 'invalid') {
return new Error('custom error')
}

return [dnrValidator, fnrValidator]
}

it.each(validIds)('Valid identity number: %s', async (fnrNum) => {
Expand Down

0 comments on commit 9f602db

Please sign in to comment.