/zod

@thourum/fault/zod converts Zod validation failures into typed Result values.

safeZodParse

safeZodParse<S extends z.ZodType>(schema: S): (data: unknown) => Result<z.infer<S>, Fault>
safeZodParse<S extends z.ZodType>(schema: S, data: unknown): Result<z.infer<S>, Fault>
const parsed = safeZodParse(userSchema, JSON.parse(text))
const parseUser = safeZodParse(userSchema)
const parsedLater = parseUser(input)

Use the direct form for one value or the curried form when the same schema validates multiple values; validation failures are converted with fromZodError. safeZodParse(schema, undefined) parses undefined; only omitting the second argument returns a parser.

Tag produced: VALIDATION_ERROR.

safeFetchJSON<unknown>('/api/user')
  .andThen((data) => safeZodParse(userSchema, data))
  .match(showUser, showFault)

fromZodError

fromZodError(error: z.ZodError): Fault

const result = userSchema.safeParse(input)
const fault = result.success ? undefined : fromZodError(result.error)

The fault message uses the first issue, details remains unset, its cause is the original ZodError, and metadata contains zodIssues, issueCount, and fieldErrors from Zod 4's z.flattenError(error).

Tag produced: VALIDATION_ERROR.