Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adds length validator messages #4115

Merged
merged 1 commit into from
Oct 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ export const ValidationExtendValidator = () => {
}
}
const myValidator = (value, { validators }) => {
const { dnrValidator, fnrValidator } = validators
const { dnrAndFnrValidator } = validators

return [dnrValidator, fnrValidator, bornInAprilValidator]
return [dnrAndFnrValidator, bornInAprilValidator]
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,68 @@ function NationalIdentityNumber(props: Props) {
label,
errorRequired,
errorFnr,
errorFnrLength,
errorDnr,
errorDnrLength,
errorMinimumAgeValidator,
errorMinimumAgeValidatorLength,
} = translations
const errorMessages = useErrorMessage(props.path, props.errorMessages, {
required: errorRequired,
pattern: errorFnr,
errorFnr,
errorFnrLength,
errorDnr,
errorDnrLength,
errorMinimumAgeValidator,
errorMinimumAgeValidatorLength,
})

const identificationNumberIsOfLength = (
identificationNumber: string,
length: number
) => {
return identificationNumber?.length === length
}

const fnrValidator = useCallback(
(value: string) => {
if (
value !== undefined &&
(Number.parseInt(value.substring(0, 1)) > 3 ||
fnr(value).status === 'invalid')
) {
return Error(errorFnr)
if (value !== undefined) {
if (Number.parseInt(value.substring(0, 1)) > 3) {
return Error(errorFnr)
}

const fnrIs11Digits = identificationNumberIsOfLength(value, 11)

if (!fnrIs11Digits) {
return Error(errorFnrLength)
}
if (fnrIs11Digits && fnr(value).status === 'invalid') {
return Error(errorFnr)
}
}
},
[errorFnr]
[errorFnr, errorFnrLength]
)

const dnrValidator = useCallback(
(value: string) => {
if (
value !== undefined &&
(Number.parseInt(value.substring(0, 1)) < 4 ||
dnr(value).status === 'invalid')
) {
return Error(errorDnr)
if (value !== undefined) {
if (Number.parseInt(value.substring(0, 1)) < 4) {
return Error(errorDnr)
}

const dnrIs11Digits = identificationNumberIsOfLength(value, 11)

if (!dnrIs11Digits) {
return Error(errorDnrLength)
}
if (dnrIs11Digits && dnr(value).status === 'invalid') {
return Error(errorDnr)
}
}
},
[errorDnr]
[errorDnr, errorDnrLength]
)

const dnrAndFnrValidator = useCallback(
Expand Down Expand Up @@ -161,7 +188,18 @@ export function createMinimumAgeValidator(age: number) {
return // stop here
}

if (value.length > 6) {
const identificationNumberIs7DigitsOrMore = value?.length >= 7

if (!identificationNumberIs7DigitsOrMore) {
return new FormError(
'NationalIdentityNumber.errorMinimumAgeValidatorLength',
{
validationRule: 'errorMinimumAgeValidatorLength', // "validationRule" Will be removed in future PR
}
)
}

if (identificationNumberIs7DigitsOrMore) {
const date = getBirthDateByFnrOrDnr(value)
if (getAgeByBirthDate(date) >= age) {
return // stop here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ describe('Field.NationalIdentityNumber', () => {
'53137248022',
]

const invalidDNumTooShort = [
'6',
'5309724803',
'5309724',
'72127248',
'5313',
]

it.each(validDNum)('Valid D number: %s', async (dNum) => {
render(
<Field.NationalIdentityNumber value={dNum} validateInitially />
Expand All @@ -384,11 +392,7 @@ describe('Field.NationalIdentityNumber', () => {

it.each(invalidDNum)('Invalid D number: %s', async (dNum) => {
render(
<Field.NationalIdentityNumber
value={dNum}
validateUnchanged
validateInitially
/>
<Field.NationalIdentityNumber value={dNum} validateInitially />
)

fireEvent.blur(document.querySelector('input'))
Expand All @@ -400,6 +404,21 @@ describe('Field.NationalIdentityNumber', () => {
)
})
})

it.each(invalidDNumTooShort)('Invalid D number: %s', async (dNum) => {
render(
<Field.NationalIdentityNumber value={dNum} validateInitially />
)

fireEvent.blur(document.querySelector('input'))

await waitFor(() => {
expect(screen.queryByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert')).toHaveTextContent(
nb.NationalIdentityNumber.errorDnrLength
)
})
})
})

describe('should validate Norwegian national identity number(fnr)', () => {
Expand All @@ -424,6 +443,14 @@ describe('Field.NationalIdentityNumber', () => {
'13137248022',
]

const invalidFnrNumTooShort = [
'2',
'1309724803',
'1309724',
'321',
'131372480',
]

it.each(validFnrNum)(
'Valid national identity number(fnr): %s',
async (fnrNum) => {
Expand All @@ -441,11 +468,7 @@ describe('Field.NationalIdentityNumber', () => {
'Invalid national identity number(fnr): %s',
async (fnrNum) => {
render(
<Field.NationalIdentityNumber
validateInitially
validateUnchanged
value={fnrNum}
/>
<Field.NationalIdentityNumber validateInitially value={fnrNum} />
)

fireEvent.blur(document.querySelector('input'))
Expand All @@ -458,6 +481,24 @@ describe('Field.NationalIdentityNumber', () => {
})
}
)

it.each(invalidFnrNumTooShort)(
'Invalid national identity number(fnr): %s',
async (fnrNum) => {
render(
<Field.NationalIdentityNumber validateInitially value={fnrNum} />
)

fireEvent.blur(document.querySelector('input'))

await waitFor(() => {
expect(screen.queryByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert')).toHaveTextContent(
nb.NationalIdentityNumber.errorFnrLength
)
})
}
)
})

describe('should extend validation using custom validator', () => {
Expand All @@ -468,6 +509,8 @@ describe('Field.NationalIdentityNumber', () => {

const invalidFnrNumApril = ['29040112345', '13047248032']
const invalidDNumApril = ['69040112345', '53047248032']
const invalidFnrTooShort = ['2904011234', '1']
const invalidDNumTooShort = ['6904011234', '5']

const validFnrNumNotApril = [
'58081633086',
Expand All @@ -478,19 +521,16 @@ describe('Field.NationalIdentityNumber', () => {

const invalidIds = [...validFnrNumNotApril, ...validDNumNotApril]

const bornInApril = (value: string) =>
value.substring(2, 4) === '04'
? { status: 'valid' }
: { status: 'invalid' }
const bornInAprilValidator = (value: string) => {
if (value.substring(2, 4) !== '04') {
return new Error('custom error')
}
}

const customValidator: Validator<string> = (value, { validators }) => {
const { dnrAndFnrValidator } = validators
const result = bornInApril(value)
if (result.status === 'invalid') {
return new Error('custom error')
}

return [dnrAndFnrValidator]
return [dnrAndFnrValidator, bornInAprilValidator]
}

it.each(validIds)('Valid identity number: %s', async (fnrNum) => {
Expand All @@ -510,7 +550,6 @@ describe('Field.NationalIdentityNumber', () => {
<Field.NationalIdentityNumber
validator={customValidator}
validateInitially
validateUnchanged
value={id}
/>
)
Expand All @@ -528,7 +567,6 @@ describe('Field.NationalIdentityNumber', () => {
<Field.NationalIdentityNumber
validator={customValidator}
validateInitially
validateUnchanged
value={dNum}
/>
)
Expand All @@ -541,14 +579,30 @@ describe('Field.NationalIdentityNumber', () => {
})
})

it.each(invalidDNumTooShort)('Invalid D number: %s', async (dNum) => {
render(
<Field.NationalIdentityNumber
validator={customValidator}
validateInitially
value={dNum}
/>
)

await waitFor(() => {
expect(screen.queryByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert')).toHaveTextContent(
nb.NationalIdentityNumber.errorDnrLength
)
})
})

it.each(invalidFnrNumApril)(
'Invalid national identity number(fnr): %s',
async (fnr) => {
render(
<Field.NationalIdentityNumber
validator={customValidator}
validateInitially
validateUnchanged
value={fnr}
/>
)
Expand All @@ -561,5 +615,25 @@ describe('Field.NationalIdentityNumber', () => {
})
}
)

it.each(invalidFnrTooShort)(
'Invalid national identity number(fnr): %s',
async (fnr) => {
render(
<Field.NationalIdentityNumber
validator={customValidator}
validateInitially
value={fnr}
/>
)

await waitFor(() => {
expect(screen.queryByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert')).toHaveTextContent(
nb.NationalIdentityNumber.errorFnrLength
)
})
}
)
})
})
Loading
Loading