You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
P.S. The library's README suggests using eval(). Use new Function() instead.
Form Rendering
Auto Form. Convert the stored JSON schema back to zod.
Database design
e.g.:
modelForm {idString@id@default(dbgenerated("gen_random_uuid()"))creationTimestampDateTime@default(now())userUser@relation(fields: [userId], references: [id])userIdStringclubClub@relation(fields: [clubId], references: [id])clubIdIntformSchemaString// A JSON Schema converted from ZodFormResponseFormResponse[]}modelFormResponse {idString@id@default(dbgenerated("gen_random_uuid()"))creationTimestampDateTime@default(now())userUser@relation(fields: [userId], references: [id])userIdStringformForm@relation(fields: [formId], references: [id])formIdStringresponseJson// A JSON object that conforms to the schema as designed in the form}
It's best to validate the response data being fed into the database every INSERT or UPDATE.
We need a trigger at postgresql every UPDATE or INSERT. Check constraints won't work because they don't support subqueries. eg:
CREATE OR REPLACEFUNCTIONvalidate_formresponse_schema()
RETURNS TRIGGER AS $$
BEGIN-- Validate that the meta field matches the schema
PERFORM jsonb_matches_schema(
NEW.form.formSchema,
NEW.response
);
-- If validation passes, proceed with the insert/update
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATETRIGGERvalidate_formresponse_schema_trigger
BEFORE INSERT ORUPDATEON FormResponse
FOR EACH ROW
EXECUTE FUNCTION alidate_formresponse_schema();
The text was updated successfully, but these errors were encountered:
LGTM. I would definitely recommend using JSON (or other equivalent standardized data structures that can store all data in a form into one single field). By this we would only need one uniform form (& data) read / write server-side API.
At least for me, I prefer the solution of using as few controllers (or APIs or whatever they are called) as possible and passing them parameters in later uses.
So uh, I'm not familiar with designing a schema to store form responses with relational database. But here's what I've collected by far:
Zod ⇔ JSON Schema:
P.S. The library's README suggests using
eval()
. Usenew Function()
instead.Form Rendering
Database design
e.g.:
It's best to validate the response data being fed into the database every
INSERT
orUPDATE
.Resources:
The text was updated successfully, but these errors were encountered: