fix: email and phone field validation (#24432)
Co-authored-by: Udit Takkar <[email protected]>
This commit is contained in:
1 parent
e5f14c9316
commit
f9b294cfc2
5 files changed
+239
-4
No files matched your search
@@ -176,6 +176,66 @@ describe("getBookingResponsesSchema", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test(`hidden required email field should not be validated`, async () => {
|
||||
const schema = getBookingResponsesSchema({
|
||||
bookingFields: [
|
||||
{
|
||||
name: "name",
|
||||
type: "name",
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
type: "email",
|
||||
required: true,
|
||||
hidden: true,
|
||||
},
|
||||
{
|
||||
name: "attendeePhoneNumber",
|
||||
type: "phone",
|
||||
required: true,
|
||||
},
|
||||
] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,
|
||||
view: "ALL_VIEWS",
|
||||
});
|
||||
const parsedResponses = await schema.safeParseAsync({
|
||||
name: "John",
|
||||
email: "",
|
||||
attendeePhoneNumber: "+919999999999",
|
||||
});
|
||||
expect(parsedResponses.success).toBe(true);
|
||||
});
|
||||
|
||||
test(`hidden required phone field should not be validated`, async () => {
|
||||
const schema = getBookingResponsesSchema({
|
||||
bookingFields: [
|
||||
{
|
||||
name: "name",
|
||||
type: "name",
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
type: "email",
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: "attendeePhoneNumber",
|
||||
type: "phone",
|
||||
required: true,
|
||||
hidden: true,
|
||||
},
|
||||
] as z.infer<typeof eventTypeBookingFields> & z.BRAND<"HAS_SYSTEM_FIELDS">,
|
||||
view: "ALL_VIEWS",
|
||||
});
|
||||
const parsedResponses = await schema.safeParseAsync({
|
||||
name: "John",
|
||||
email: "[email protected]",
|
||||
attendeePhoneNumber: "",
|
||||
});
|
||||
expect(parsedResponses.success).toBe(true);
|
||||
});
|
||||
|
||||
test(`firstName is required and lastName is optional by default`, async () => {
|
||||
const schema = getBookingResponsesSchema({
|
||||
bookingFields: [
|
||||
|
||||
@@ -174,7 +174,7 @@ function preprocess<T extends z.ZodType>({
|
||||
}
|
||||
|
||||
if (bookingField.type === "email") {
|
||||
if (!bookingField.hidden && checkOptional ? true : bookingField.required) {
|
||||
if (!bookingField.hidden && (checkOptional || bookingField.required)) {
|
||||
// Email RegExp to validate if the input is a valid email
|
||||
if (!emailSchema.safeParse(value).success) {
|
||||
ctx.addIssue({
|
||||
|
||||
Reference in new issue
Block a user