Skip to content

Parsing ​

ExportNotes
parse(schema, input, options?)Returns {ok: true, value} or {ok: false, error}; never throws for invalid input
parseOrThrow(schema, input, options?)Returns Parsed<typeof schema> or throws a ParseError
ParseOptions{reportInput?: boolean}. With reportInput, issues include the rejected value as input
ParseResult<T>, ParseOk<T>, ParseFailThe types of a parse result
ParseErrormessage is a report of every issue; issues holds them
ParseIssue{path, code, message}, plus input with reportInput
PathAn array of field names, array indexes and {_key, index} segments for items in arrays of objects
formatPath(path)Writes a path the way Sanity does: body[_key=="abc"].children[0].text; <root> for the empty path. Also takes Standard Schema paths
formatIssues(issues)Writes issues as a report, one per line; the same text as ParseError.message. Also takes the issues of any Standard Schema validate result
IssueLikeWhat formatIssues accepts: {message, path?, input?}, which both ParseIssue and a Standard Schema issue satisfy
PathSegmentLikeWhat formatPath accepts in a path: a field name, index, symbol, {_key, index} or Standard Schema {key} segment
schema['~standard']The Standard Schema properties; validate runs parse
ErrorCodeOne of the codes below
Error codeRaised when
INVALID_TYPEThe value has the wrong type or format
INVALID_ENUM_VALUEThe value is not one of the values given to enum()
INVALID_PRIMITIVE_UNIONThe value matches no member of a union of primitives
NUM_TOO_SMALL, NUM_TOO_BIGA number is outside the range given to number()
NUM_NOT_MULTIPLE_OFA number does not match the step constraint
INVALID_OBJECT_UNIONNo member of an object union matches the _type
ARRAY_ELEMENT_NOT_KEYED_OBJECTAn object in an array has no _key

See Parsing and errors for examples.