Validation plugin
A plugin for adding validation to field arguments, input object fields, and input types using modern validation libraries like Zod, Valibot, and ArkType.
This plugin provides a library-agnostic approach to validation by supporting any validation library that implements the standard schema interface, making it flexible and future-proof.
Usage
Install
To use the validation plugin, you'll need to install the validation plugin and a compatible validation library:
Setup
Validation API Overview
The validation plugin supports validating inputs and arguments in several different ways:
- Argument validation:
t.arg.string({ validate: schema })
ort.arg.string().validate(schema)
- Validate individual arguments - Validate all field args:
t.field({ args, validate: schema, ... })
ort.field({ args: t.validate(args), ... }).validate(schema)
- Validate all arguments together - Input type validation:
builder.inputType({ validate: schema, ... })
ort.inputType({ ... }).validate(schema)
- Validate entire input objects - Input field validation:
t.string({ validate: schema })
ort.string().validate(schema)
- Validate individual input type fields
Validation Patterns
The validation plugin supports multiple validation patterns to give you fine-grained control over input validation:
Argument Validation
Validate each field argument independently using either the object syntax or chaining API:
Data Transformation with Argument Validation
When using the chaining API, you can transform data as part of the validation process:
Validating all Field Arguments Together
You can validate all arguments of a field together by passing a validation schema to the t.field
With transforms
To transform all arguments together, you will need to use t.validate(args):
Input Type Validation
Validate entire input objects with complex validation logic using either object syntax or chaining:
Input Type Transformation
Transform entire input types:
Input Field Validation
Validate individual fields within input types:
Input Field Transformation
Transform field values during validation:
Supported Validation Libraries
This plugin works with multiple validation libraries, giving you the flexibility to choose the one that best fits your needs:
- Zod - TypeScript-first schema validation with static type inference
- Valibot - The open source schema library for TypeScript with bundle size, type safety and developer experience in mind
- ArkType - TypeScript's 1:1 validator, optimized from editor to runtime
- Any library implementing the standard schema interface
This ensures broad compatibility and future-proofing as the validation ecosystem evolves.
Plugin Options
Configure the validation plugin behavior:
Custom Error Handling
The validationError
option allows you to customize how validation errors are handled and formatted. This is useful for:
- Customizing error messages for your application's needs
- Logging validation failures for monitoring
- Integrating with error tracking services
- Providing context-specific error messages
Error Handler Parameters
The validationError
function receives four parameters:
validationResult
- Contains the validation failure details from the validation libraryargs
- The field arguments that were being validatedcontext
- Your GraphQL context object
Return Values
Your error handler can return:
- Error object: Return a custom Error instance
- String: Return a string message (will be wrapped in a PothosValidationError)
- Throw: Throw an error directly
Validation Execution Order
Understanding when and how validations are executed:
- Input Field Validation: Individual input fields are validated first
- Input Type Validation: Whole input object validation runs after field validation passes
- Argument Validation: Individual field arguments are validated
- Field-Level Validation: Cross-field validation with
t.validate()
runs last
When there are multiple validations for the same field or type, they are executed in order, so that any transforms are applied before passing to the next schema. Validations for separate fields or arguments are executed in parallel, and their results are merged into a single set of issues.