Skip to content

Commit

Permalink
MAN-172: Update contact details
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcphee77 committed Feb 4, 2025
1 parent 0a9bf27 commit 98bde13
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
12 changes: 6 additions & 6 deletions server/utils/validationUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ describe('is uk postcode', () => {

describe('is chars or less', () => {
it.each([
['empty string', ['', 3], true],
['null', [null, 3], true],
['undefined', [undefined, 3], true],
['populated valid', ['XXX', 3], true],
['populated valid', ['XX', 3], true],
['populated invalid', ['XXLL', 3], true],
['empty string', [3, ''], true],
['null', [3, null], true],
['undefined', [3, undefined], true],
['populated valid', [3, 'XXX'], true],
['populated valid', [3, 'XX'], true],
['populated invalid', [3, 'XXLL'], false],
])('%s charsOrLess(%s, %s)', (_: string, a: [], expected: boolean) => {
expect(charsOrLess(a)).toEqual(expected)
})
Expand Down
43 changes: 25 additions & 18 deletions server/utils/validationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ export interface ValidationSpec {
[index: string]: ErrorChecks
}

export const isEmail = (string: string) =>
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
string,
)
export const isEmail = (string: string) => /^\S+@\S+\.\S+$/.test(string)
export const isNotEmpty = (args: any[]) => {
return !!args[0] && args[0] !== undefined
}
Expand All @@ -33,7 +30,7 @@ export const isUkPostcode = (args: any[]) => {
return postcodeValidator(args[0], 'GB')
}
export const charsOrLess = (args: any[]) => {
return args[1].length <= args[0]
return !args[1] || args[1].length <= args[0]
}
export const isValidDate = (args: any[]) => {
return !!args[0] && DateTime.fromFormat(args[0], 'd/M/yyyy').isValid
Expand Down Expand Up @@ -64,23 +61,33 @@ export function validateWithSpec<R extends Validateable>(request: R, validationS
Object.entries(validationSpec).forEach(entry => {
const fieldName = entry[0]
const checks = entry[1]
if (!request[fieldName] && checks.optional === true) {
return
}
if (Object.prototype.hasOwnProperty.call(request, fieldName)) {
if (!request[fieldName] && checks.optional === true) {
return
}
for (const c of checks.checks) {
let args: any[] = c?.length ? [c.length, request[fieldName]] : [request[fieldName]]
if (c?.crossField) {
args = [request[c?.crossField], request[fieldName]]
}
if (!c.validator(args)) {
errors[fieldName] = c.msg
break
}
}
const error = executeValidator(checks.checks, fieldName, request)
if (error) errors[fieldName] = error
} else if (checks.optional === false) {
errors[fieldName] = checks.checks[0].msg
}
})
return errors
}

function executeValidator(checks: ErrorCheck[], fieldName: string, request: Validateable) {
for (const check of checks) {
const args: any[] = setArgs(fieldName, check, request)
if (!check.validator(args)) {
return check.msg
}
}
return null
}

function setArgs(fieldName: string, check: ErrorCheck, request: Validateable) {
let args: any[] = check?.length ? [check.length, request[fieldName]] : [request[fieldName]]
if (check?.crossField) {
args = [request[check.crossField], request[fieldName]]
}
return args
}

0 comments on commit 98bde13

Please sign in to comment.