Z
Zod7mo ago
Gludek

Gludek - Hey,I have objects like this (there's...

Hey, I have objects like this (there's little bit more nesting), but for some reason the superRefine doesn't trigger for guardian's BaseData, only for the patient
const baseData = z
.object({
adress: address,
documentNumber: z.string().nullish(),
email: z.literal('').nullish().or(z.string().email()),
name: z.string().min(3).trim(),
pesel: z.string().nullish(),
phoneNumber: z.string().min(9).trim(),
phonePrefix: z.string().min(2).trim(),
surname: z.string().min(3).trim(),
})
.superRefine((data, ctx) => {
if (!data.pesel && !data.documentNumber) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'peselOrDocumentNumberRequired',
},
path: ['pesel'],
});

ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'peselOrDocumentNumberRequired',
},
path: ['documentNumber'],
});
}
if (data.pesel) {
if (data.pesel) {
if (data.pesel.length !== 11) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'invalid_pesel',
},
path: ['pesel'],
});
} else {
if (checkSum(data.pesel) !== parseInt(data.pesel[10])) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'invalid_pesel',
},
path: ['pesel'],
});
}
}
}
}
});

const guardian = z.object({
baseData: baseData,
});

const patientData = z
.object({
baseData: baseData,
birthDate: z.date(),
gender: z.boolean().nullish(),
guardian: guardian.nullish(),
symptomes: z.array(z.string()).nonempty(),
under18: z.boolean(),
})
const baseData = z
.object({
adress: address,
documentNumber: z.string().nullish(),
email: z.literal('').nullish().or(z.string().email()),
name: z.string().min(3).trim(),
pesel: z.string().nullish(),
phoneNumber: z.string().min(9).trim(),
phonePrefix: z.string().min(2).trim(),
surname: z.string().min(3).trim(),
})
.superRefine((data, ctx) => {
if (!data.pesel && !data.documentNumber) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'peselOrDocumentNumberRequired',
},
path: ['pesel'],
});

ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'peselOrDocumentNumberRequired',
},
path: ['documentNumber'],
});
}
if (data.pesel) {
if (data.pesel) {
if (data.pesel.length !== 11) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'invalid_pesel',
},
path: ['pesel'],
});
} else {
if (checkSum(data.pesel) !== parseInt(data.pesel[10])) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'invalid_pesel',
},
path: ['pesel'],
});
}
}
}
}
});

const guardian = z.object({
baseData: baseData,
});

const patientData = z
.object({
baseData: baseData,
birthDate: z.date(),
gender: z.boolean().nullish(),
guardian: guardian.nullish(),
symptomes: z.array(z.string()).nonempty(),
under18: z.boolean(),
})
6 Replies
janglad
janglad7mo ago
does it never run for the guardian? I would guess it should run if patienData.baseData is valid Actually I think it should run either way, except if the inner schema that the refine is chained on to is not valid
Gludek
GludekOP7mo ago
nope, never. Sorry for late response, totally forgot about this Even if I move superRefine onto baseData in guardian and remove nullish it's not running for some reason That seems to be correct but it works for the patients baseData if inner schema is not correct that's weird
janglad
janglad7mo ago
Could you share the data you're using to test it?
Gludek
GludekOP7mo ago
it's an empty form basically
patientData: [
{
baseData: {
adress: {
city: '',
country: '',
street: '',
zipCode: '',
flatNumber: '',
},
documentNumber: '',
email: '',
name: '',
pesel: '',
phoneNumber: '',
phonePrefix: '',
surname: '',
},
birthDate: undefined,
gender: false,
guardian: {
baseData: {
adress: {
city: '',
country: '',
street: '',
zipCode: '',
flatNumber: '',
},
documentNumber: '',
email: '',
name: '',
pesel: '',
phoneNumber: '',
phonePrefix: '',
surname: '',
},
ssnMode:"true",
differentAddress:false,
},
symptomes: [],
under18: undefined,
},
]
patientData: [
{
baseData: {
adress: {
city: '',
country: '',
street: '',
zipCode: '',
flatNumber: '',
},
documentNumber: '',
email: '',
name: '',
pesel: '',
phoneNumber: '',
phonePrefix: '',
surname: '',
},
birthDate: undefined,
gender: false,
guardian: {
baseData: {
adress: {
city: '',
country: '',
street: '',
zipCode: '',
flatNumber: '',
},
documentNumber: '',
email: '',
name: '',
pesel: '',
phoneNumber: '',
phonePrefix: '',
surname: '',
},
ssnMode:"true",
differentAddress:false,
},
symptomes: [],
under18: undefined,
},
]
something like this
Gludek
GludekOP7mo ago
No description
Gludek
GludekOP7mo ago
It seems that it works if I do it like this
const baseData = z
.object({
adress: address,
email: z.literal('').nullish().or(z.string().email()),
name: z.string().min(3).trim(),
phoneNumber: z.string().min(9).trim(),
phonePrefix: z.string().min(1).trim(),
surname: z.string().min(3).trim(),
})
.and(
z
.object({
documentNumber: z.string().nullish(),
pesel: z.string().nullish(),
})
.superRefine((data, ctx) => {
if (!data.pesel && !data.documentNumber) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'peselOrDocumentNumberRequired',
},
path: ['pesel'],
});
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'peselOrDocumentNumberRequired',
},
path: ['documentNumber'],
});
}
if (data.pesel) {
if (data.pesel) {
if (data.pesel.length !== 11) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'invalid_pesel',
},
path: ['pesel'],
});
} else {
if (checkSum(data.pesel) !== parseInt(data.pesel[10])) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'invalid_pesel',
},
path: ['pesel'],
});
}
}
}
}
})
);
const baseData = z
.object({
adress: address,
email: z.literal('').nullish().or(z.string().email()),
name: z.string().min(3).trim(),
phoneNumber: z.string().min(9).trim(),
phonePrefix: z.string().min(1).trim(),
surname: z.string().min(3).trim(),
})
.and(
z
.object({
documentNumber: z.string().nullish(),
pesel: z.string().nullish(),
})
.superRefine((data, ctx) => {
if (!data.pesel && !data.documentNumber) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'peselOrDocumentNumberRequired',
},
path: ['pesel'],
});
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'peselOrDocumentNumberRequired',
},
path: ['documentNumber'],
});
}
if (data.pesel) {
if (data.pesel) {
if (data.pesel.length !== 11) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'invalid_pesel',
},
path: ['pesel'],
});
} else {
if (checkSum(data.pesel) !== parseInt(data.pesel[10])) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
Intl: 'invalid_pesel',
},
path: ['pesel'],
});
}
}
}
}
})
);
Want results from more Discord servers?
Add your server