fix: cache hotel response

This commit is contained in:
Simon Emanuelsson
2024-12-17 16:18:46 +01:00
parent 13a164242f
commit 1deab000bd
38 changed files with 339 additions and 246 deletions

View File

@@ -0,0 +1,12 @@
import { z } from "zod"
export const nullableNumberValidator = z
.number()
.nullish()
.transform((num) => (typeof num === "number" ? num : 0))
export const nullableIntValidator = z
.number()
.int()
.nullish()
.transform((num) => (typeof num === "number" ? num : 0))

View File

@@ -0,0 +1,46 @@
import { z } from "zod"
export const passwordValidators = {
length: {
matcher: (password: string) =>
password.length >= 10 && password.length <= 40,
message: "10 to 40 characters",
},
hasUppercase: {
matcher: (password: string) => /[A-Z]/.test(password),
message: "1 uppercase letter",
},
hasLowercase: {
matcher: (password: string) => /[a-z]/.test(password),
message: "1 lowercase letter",
},
hasNumber: {
matcher: (password: string) => /[0-9]/.test(password),
message: "1 number",
},
hasSpecialChar: {
matcher: (password: string) =>
/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/.test(password),
message: "1 special character",
},
}
export const passwordValidator = (msg = "Required field") =>
z
.string()
.min(1, msg)
.refine(passwordValidators.length.matcher, {
message: passwordValidators.length.message,
})
.refine(passwordValidators.hasUppercase.matcher, {
message: passwordValidators.hasUppercase.message,
})
.refine(passwordValidators.hasLowercase.matcher, {
message: passwordValidators.hasLowercase.message,
})
.refine(passwordValidators.hasNumber.matcher, {
message: passwordValidators.hasNumber.message,
})
.refine(passwordValidators.hasSpecialChar.matcher, {
message: passwordValidators.hasSpecialChar.message,
})

View File

@@ -0,0 +1,87 @@
import {
isPossiblePhoneNumber,
ParseError,
parsePhoneNumber,
validatePhoneNumberLength,
} from "libphonenumber-js"
import { z } from "zod"
const enum ParseErrorMessage {
INVALID_COUNTRY = "INVALID_COUNTRY",
INVALID_LENGTH = "INVALID_LENGTH",
NOT_A_NUMBER = "NOT_A_NUMBER",
TOO_LONG = "TOO_LONG",
TOO_SHORT = "TOO_SHORT",
}
export function phoneValidator(
msg = "Required field",
invalidMsg = "Invalid type"
) {
return z
.string({ invalid_type_error: invalidMsg, required_error: msg })
.min(1, { message: msg })
.superRefine((value, ctx) => {
if (value) {
try {
const phoneNumber = parsePhoneNumber(value)
if (phoneNumber) {
if (isPossiblePhoneNumber(value, phoneNumber.country)) {
return validatePhoneNumberLength(value, phoneNumber.country)
} else {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Please enter a valid phone number",
})
}
}
} catch (error) {
if (error instanceof ParseError) {
/**
* Only setup for when we need proper validation,
* should probably move to .superRefine to be able
* to return different messages depending on error.
*/
switch (error.message) {
case ParseErrorMessage.INVALID_COUNTRY:
ctx.addIssue({
code: z.ZodIssueCode.custom,
message:
"The country selected and country code doesn't match",
})
break
case ParseErrorMessage.INVALID_LENGTH:
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Please enter a valid phone number",
})
break
case ParseErrorMessage.NOT_A_NUMBER:
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Please enter a number",
})
break
case ParseErrorMessage.TOO_LONG:
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "The number you have entered is too long",
})
break
case ParseErrorMessage.TOO_SHORT:
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "The number you have entered is too short",
})
break
}
} else {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "The number you have entered is not valid",
})
}
}
}
})
}

View File

@@ -0,0 +1,12 @@
import { z } from "zod"
export const nullableStringValidator = z
.string()
.nullish()
.transform((str) => (str ? str : ""))
export const nullableStringUrlValidator = z
.string()
.url()
.nullish()
.transform((str) => (str ? str : ""))