adxvcasas
adxvcasas
ZZod
Created by adxvcasas on 4/11/2024 in #questions
adxvcasas - Hello everyone, How can I make the ...
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
dryRunStart: z.string().optional(),
dryRunEnd: z.string().optional(),
}).superRefine(({ type, dryRunDate, dryRunStart, dryRunEnd }, ctx) => {
if (type === 'yes') {
if (!dryRunDate) {
ctx.addIssue({
code: 'custom',
message: 'dryRunDate is required to fill up if you choose yes.',
path: ['dryRunDate']
})
}
if (!dryRunStart) {
ctx.addIssue({
code: 'custom',
message: 'dryRunStart is required to fill up if you choose yes.',
path: ['dryRunStart']
})
}
if (!dryRunEnd) {
ctx.addIssue({
code: 'custom',
message: 'dryRunEnd is required to fill up if you choose yes.',
path: ['dryRunEnd']
})
}
}
});
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
dryRunStart: z.string().optional(),
dryRunEnd: z.string().optional(),
}).superRefine(({ type, dryRunDate, dryRunStart, dryRunEnd }, ctx) => {
if (type === 'yes') {
if (!dryRunDate) {
ctx.addIssue({
code: 'custom',
message: 'dryRunDate is required to fill up if you choose yes.',
path: ['dryRunDate']
})
}
if (!dryRunStart) {
ctx.addIssue({
code: 'custom',
message: 'dryRunStart is required to fill up if you choose yes.',
path: ['dryRunStart']
})
}
if (!dryRunEnd) {
ctx.addIssue({
code: 'custom',
message: 'dryRunEnd is required to fill up if you choose yes.',
path: ['dryRunEnd']
})
}
}
});
This one works fine, Thank you
16 replies
ZZod
Created by adxvcasas on 4/11/2024 in #questions
adxvcasas - Hello everyone, How can I make the ...
It works on 1 pair only but if I add the other two, the warning didn't show.
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
dryRunStart: z.string().optional(),
dryRunEnd: z.string().optional(),
}).superRefine(({type,dryRunDate, dryRunStart, dryRunEnd}, ctx ) => {
if(type === 'yes' && dryRunDate === '' && dryRunStart === '' && dryRunEnd === ''){
ctx.addIssue({
code: 'custom',
message: "Please fill up the field",
path: ['dryRunDate', 'dryRunStart', 'dryRunEnd']
})
}
})
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
dryRunStart: z.string().optional(),
dryRunEnd: z.string().optional(),
}).superRefine(({type,dryRunDate, dryRunStart, dryRunEnd}, ctx ) => {
if(type === 'yes' && dryRunDate === '' && dryRunStart === '' && dryRunEnd === ''){
ctx.addIssue({
code: 'custom',
message: "Please fill up the field",
path: ['dryRunDate', 'dryRunStart', 'dryRunEnd']
})
}
})
16 replies
ZZod
Created by adxvcasas on 4/11/2024 in #questions
adxvcasas - Hello everyone, How can I make the ...
Hello, Svish, I did this thing,
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
}).superRefine(({type,dryRunDate}, ctx ) => {
if(type === 'yes' && dryRunDate === ''){
ctx.addIssue({
code: 'custom',
message: "Please fill up the field",
path: ['dryRunDate']
})
}
})
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
}).superRefine(({type,dryRunDate}, ctx ) => {
if(type === 'yes' && dryRunDate === ''){
ctx.addIssue({
code: 'custom',
message: "Please fill up the field",
path: ['dryRunDate']
})
}
})
16 replies
ZZod
Created by adxvcasas on 4/11/2024 in #questions
adxvcasas - Hello everyone, How can I make the ...
Hello, I tried experemnting it, is this thing worth?
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
}).refine(data => {
if (data.type === "yes" && !data.dryRunDate) {
throw new Error("Dry run date is required when type is 'yes'.");
}
return true;
});
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
}).refine(data => {
if (data.type === "yes" && !data.dryRunDate) {
throw new Error("Dry run date is required when type is 'yes'.");
}
return true;
});
16 replies
ZZod
Created by adxvcasas on 4/11/2024 in #questions
adxvcasas - Hello everyone, How can I make the ...
what if I change the type.enum to true or false? is this still applicable? Thank you
16 replies
ZZod
Created by adxvcasas on 4/11/2024 in #questions
adxvcasas - Hello everyone, How can I make the ...
Hello @Svish , I tried doing it like this.
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
dryRunStart: z.string().optional(),
dryRunEnd: z.string().optional(),
}).refine(data => {
if (data.type === 'yes') {
return {
dryRunDate: data.dryRunDate || 'Required field',
dryRunStart: data.dryRunStart || 'Required field',
dryRunEnd: data.dryRunEnd || 'Required field',
};
}
return true;
};
const FormSchema = z.object({
type: z.enum(["yes", "no"], {
required_error: "You need to select a notification type.",
}),
dryRunDate: z.string().optional(),
dryRunStart: z.string().optional(),
dryRunEnd: z.string().optional(),
}).refine(data => {
if (data.type === 'yes') {
return {
dryRunDate: data.dryRunDate || 'Required field',
dryRunStart: data.dryRunStart || 'Required field',
dryRunEnd: data.dryRunEnd || 'Required field',
};
}
return true;
};
I don't know if i'm doing it correctly or not, Thanks
16 replies