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
import{z}from'zod'import{createConformSchema,Predicates}from'create-conform-schema'typeServerValidationNames='isUsernameUnique'exporttypeServerValidations=Predicates<ServerValidationNames>exportconst{ client, server }=createConformSchema<ServerValidationNames>(({ server })=>z.object({username: z.string({required_error: 'Username is required'}).regex(/^[a-zA-Z0-9]+$/,'Invalid username: only letters or numbers are allowed').pipe(server.isUsernameUnique('Username is already used')),password: z.string({required_error: 'Password is required'})}))// Client-sideconstsubmission=parseWithZod(formData,{schema: client})// Server-sideconstserverValidations: ServerValidations={isUsernameUnique: async(username: string)=>!!awaitdb.user.findUnique({where: { username },select: null})}constsubmission=awaitparseWithZod(formData,{schema: server(serverValidations),async: true,})
Manual refinement helper
import{z}from'zod'import{createConformSchema,refine}from'create-conform-schema'exportconst{client: schema}=createConformSchema(({ intent })=>z.object({username: z.string({required_error: 'Username is required'}).regex(/^[a-zA-Z0-9]+$/,'Invalid username: only letters or numbers are allowed').pipe(refine<string>((username,{ skip, validateOnServer, addIssue, path })=>{if(intent?.type=='validate'&&intent.payload.name!==path[0])returnskip()if(!'Something?')returnvalidateOnServer()if(username=='jimmy')addIssue('jimmy is a bad username')// Creates an issue with code: 'custom'if(username.length<3)addIssue({code: 'too_small',minimum: 3,inclusive: true,type: 'string'})})),password: z.string({required_error: 'Password is required'})}))
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I made this thing to make using conform easier. create-conform-schema
It abstracts the pattern described at conform.guide/validation
Server-side validations
Manual refinement helper
Beta Was this translation helpful? Give feedback.
All reactions