Skip to content

Commit

Permalink
fix(general-petition): Fix form validation (#14589)
Browse files Browse the repository at this point in the history
* Fixing form validation

* update validation

* update isValidPhoneNumber & isValidEmail

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
joningi98 and kodiakhq[bot] authored Apr 30, 2024
1 parent 5cd0ad6 commit 57a4c05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
15 changes: 10 additions & 5 deletions libs/application/templates/general-petition/src/lib/dataSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ const FileSchema = z.object({

const isValidPhoneNumber = (phoneNumber: string) => {
const phone = parsePhoneNumberFromString(phoneNumber, 'IS')
return phone && phone.isValid()
return phone && phone.isValid() && phoneNumber.length > 0
}

const emailRegex =
/^[\w!#$%&'*+/=?`{|}~^-]+(?:\.[\w!#$%&'*+/=?`{|}~^-]+)*@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$/i
export const isValidEmail = (value: string) => emailRegex.test(value)
export const isValidEmail = (value: string): boolean =>
emailRegex.test(value) && value.length > 0

export const GeneralPetitionSchema = z.object({
approveTermsAndConditions: z
Expand Down Expand Up @@ -50,14 +51,18 @@ export const GeneralPetitionSchema = z.object({
})
.refine(
({ dateFrom, dateTil }) =>
!dateFrom || !dateTil || new Date(dateFrom) <= new Date(dateTil),
!dateFrom || !dateTil || new Date(dateFrom) < new Date(dateTil),
{
message: m.validationTilBeforeFrom.defaultMessage as string,
path: ['dateTil'],
},
),
phone: z.string().refine((v) => isValidPhoneNumber(v)),
email: z.string().refine((v) => isValidEmail(v)),
phone: z.string().refine((v) => isValidPhoneNumber(v), {
message: m.validationPhone.defaultMessage as string,
}),
email: z.string().refine((v) => isValidEmail(v), {
message: m.validationEmail.defaultMessage as string,
}),
})

export type File = z.TypeOf<typeof FileSchema>
Expand Down
13 changes: 12 additions & 1 deletion libs/application/templates/general-petition/src/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,18 @@ export const m = defineMessages({
},
validationTilBeforeFrom: {
id: 'gpl.application:validationTilBeforeFrom',
defaultMessage: 'Lokadagsetning má ekki vera á undan upphafsdagsetningu',
defaultMessage:
'Lokadagsetning má ekki vera á undan eða sú sama og upphafsdagsetning',
description: '',
},
validationPhone: {
id: 'gpl.application:validationPhone',
defaultMessage: 'Vinsamlegast sláðu inn gilt símanúmer',
description: '',
},
validationEmail: {
id: 'gpl.application:validationEmail',
defaultMessage: 'Vinsamlegast sláðu inn gilt netfang',
description: '',
},

Expand Down

0 comments on commit 57a4c05

Please sign in to comment.