improve client side data validation

This commit is contained in:
andrzej 2024-06-25 12:20:56 +02:00
parent ae25aca0e8
commit 15a1309275
1 changed files with 24 additions and 0 deletions

View File

@ -40,6 +40,30 @@ const FormSchema = z.object({
responded: z.date().transform((date) => date.toString()).optional(),
responseId: z.coerce.number()
})
.refine(object => {
const submitted = new Date(object.submitted)
const responded = object.responded ? new Date(object.responded) : null
return responded >= submitted || responded === null
},
{
path: ["responded"],
message: "'Responded' must be a later date than 'submitted'"
})
.refine(object => {
if (object.responded) {
//there is a 'responded' date and the response is not 'pending'
return object.responseId !== 7
}
if (!object.responded) {
//there is not a 'responded' date and the response is pending
return object.responseId === 7
}
},
{
path: ["responseId"],
message: "A pending response cannot have a date, and a non-pending response must have a date"
}
)
export default function SubmissionForm({ stories, pubs, responses, createSub }) {