Zod Validation plugin
A plugin for adding validation for field arguments based on zod. This plugin does not expose zod directly, but most of the options map closely to the validations available in zod.
Usage
Install
To use the zod plugin you will need to install both zod
package and the zod plugin:
Setup
Options
validationError
: (optional) A function that will be called when validation fails. The function
will be passed the the zod validation error, as well as the args, context and info objects. It can
throw an error, or return an error message or custom Error instance.
Examples
With custom message
Validating List
Using your own zod schemas
If you just want to use a zod schema defined somewhere else, rather than using the validation
options you can use the schema
option:
You can also validate all arguments together using a zod schema:
API
On Object fields (for validating field arguments)
validate
:Refinement<T>
|Refinement<T>[]
|ValidationOptions
.
On InputObjects (for validating all fields of an input object)
validate
:Refinement<T>
|Refinement<T>[]
|ValidationOptions
.
On arguments or input object fields (for validating a specific input field or argument)
validate
:Refinement<T>
|Refinement<T>[]
|ValidationOptions
.
Refinement
A Refinement
is a function that will be passed to the zod
refine
method. It receives the args
object, input object, or value of the specific field the refinement is defined on. It should return
a boolean
or Promise<boolean>
.
Refinement
s can either be just a function: (val) => isValid(val)
, or an array with the function,
and an options object like: [(val) => isValid(val), { message: 'field should be valid' }]
.
The options object may have a message
property, and if the type being validated is an object, it
can also include a path
property with an array of strings indicating the path of the field in the
object being validated. See the zod docs on refine
for more details.
ValidationOptions
The validation options available depend on the type being validated. Each property of
ValidationOptions
can either be a value specific to the constraint, or an array with the value,
and the options passed to the underlying zod method. This options object can be used to set a custom
error message:
Number
type
?:'number'
refine
?:Refinement<number> | Refinement<number>[]
min
?:Constraint<number>
max
?:Constraint<number>
positive
?:Constraint<boolean>
nonnegative
?:Constraint<boolean>
negative
?:Constraint<boolean>
nonpositive
?:Constraint<boolean>
int
?:Constraint<boolean>
schema
?:ZodSchema<number>
BigInt
type
?:'bigint'
refine
?:Refinement<bigint> | Refinement<bigint>[]
schema
?:ZodSchema<bigint>
Boolean
type
?:'boolean'
refine
?:Refinement<boolean> | Refinement<boolean>[]
schema
?:ZodSchema<boolean>
Date
type
?:'boolean'
refine
?:Refinement<boolean> | Refinement<boolean>[]
schema
?:ZodSchema<Date>
String
type
?:'string'
;refine
?:Refinement<string> | Refinement<string>[]
minLength
?:Constraint<number>
maxLength
?:Constraint<number>
length
?:Constraint<number>
url
?:Constraint<boolean>
uuid
?:Constraint<boolean>
email
?:Constraint<boolean>
regex
?:Constraint<RegExp>
schema
?:ZodSchema<string>
Object
type
?:'object'
;refine
?:Refinement<T> | Refinement<T>[]
schema
?:ZodSchema<Ts>
Array
type
?:'array'
;refine
?:Refinement<T[]> | Refinement<T[]>[]
minLength
?:Constraint<number>
maxLength
?:Constraint<number>
length
?:Constraint<number>
items
?:ValidationOptions<T> | Refinement<T>
schema
?:ZodSchema<T[]>
How it works
Each arg on an object field, and each field on an input type with validation will build its own zod
validator. These validators will be a union of all potential types that can apply the validations
defined for that field. For example, if you define an optional field with a maxLength
validator,
it will create a zod schema that looks something like:
If you set and email
validation instead the schema might look like:
At runtime, we don't know anything about the types being used by your schema, we can't infer the
expected js type from the type definition, so the best we can do is limit the valid types based on
what validations they support. The type
validation allows explicitly validating the type
of a
field to be one of the base types supported by zod:
There are a few exceptions the above:
-
args and input fields that are
InputObject
s always usezod.object()
rather than creating a union of potential types. -
args and input fields that are list types always use
zod.array()
. -
If you only include a
refine
validation (or just pass a function directly to validate) we will just usezod
s unknown validator instead:
If the validation options include a schema
that schema will be used as an intersection wit the
generated validator:
Sharing schemas with client code
The easiest way to share validators is the use the to define schemas for your fields in an external
file using the normal zod APIs, and then attaching those to your fields using the schema
option.
You can also use the createZodSchema
helper from the plugin directly to create zod Schemas from an
options object: