Skip to content

Commit

Permalink
fix(Field.OrganizationNumber): should validate on all digits(not only…
Browse files Browse the repository at this point in the history
… when 9) (#4071)

Co-authored-by: Tobias Høegh <tobias@tujo.no>
  • Loading branch information
langz and tujoworker authored Oct 8, 2024
1 parent 51bfd80 commit 08a4b51
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ function OrganizationNumber(props: Props) {

const { validate = true, omitMask } = props

const validationPattern = '^[0-9]{9}$'

const errorMessages = useErrorMessage(props.path, props.errorMessages, {
required: errorRequired,
pattern: errorPattern,
Expand All @@ -31,10 +29,7 @@ function OrganizationNumber(props: Props) {

const organizationNumberValidator = useCallback(
(value: string) => {
if (
new RegExp(validationPattern).test(value) &&
!isValidOrgNumber(value)
) {
if (!isValidOrgNumber(value)) {
return Error(errorPattern)
}
},
Expand All @@ -44,7 +39,7 @@ function OrganizationNumber(props: Props) {
const StringFieldProps: Props = {
...props,
className: 'dnb-forms-field-organization-number',
pattern: props.pattern ?? (validate ? validationPattern : undefined),
pattern: props.pattern ?? (validate ? '^[0-9]{9}$' : undefined),
label: props.label ?? label,
errorMessages,
mask,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,39 @@ describe('Field.OrganizationNumber', () => {
expect(input).toHaveAttribute('inputmode', 'numeric')
})

it('should not validate organization number when validate false', async () => {
it('should validate organization number based on the internal pattern', () => {
render(
<Form.Handler>
<Field.OrganizationNumber validateInitially value="123" />
</Form.Handler>
)

expect(screen.queryByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert').textContent).toBe(
nb.OrganizationNumber.errorPattern
)
})

it('should validate organization number based on the internal validator', async () => {
render(
<Form.Handler>
<Field.OrganizationNumber
validateInitially
value="123"
pattern="[1-3]"
/>
</Form.Handler>
)

await waitFor(() => {
expect(screen.queryByRole('alert')).toBeInTheDocument()
expect(screen.queryByRole('alert').textContent).toBe(
nb.OrganizationNumber.errorPattern
)
})
})

it('should not validate organization number when validate false', () => {
const invalidOrgNo = '987654321'

render(
Expand All @@ -78,7 +110,7 @@ describe('Field.OrganizationNumber', () => {
expect(screen.queryByRole('alert')).toBeNull()
})

it('should not validate custom validator when validate false', async () => {
it('should not validate custom validator when validate false', () => {
const invalidOrgNo = '987654321'

const firstNumIs1 = (value: string) =>
Expand Down Expand Up @@ -109,7 +141,7 @@ describe('Field.OrganizationNumber', () => {
expect(screen.queryByRole('alert')).toBeNull()
})

it('should not validate extended validator when validate false', async () => {
it('should not validate extended validator when validate false', () => {
const invalidOrgNo = '987654321'

const firstNumIs1 = (value: string) =>
Expand Down

0 comments on commit 08a4b51

Please sign in to comment.