improve client side data validation
This commit is contained in:
parent
ef70bd9d92
commit
4145d84d65
|
@ -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 }) {
|
||||
|
|
Loading…
Reference in New Issue