Skip to main content
Data validation ensures that incoming data matches your schema definitions before it reaches your service methods. The Feathers schema system provides hooks for validating both data and queries.

Validation Hooks

The schema system provides two main validation hooks:
  • validateData - Validates data for create, update, and patch methods
  • validateQuery - Validates query parameters for all methods

Basic Data Validation

1

Define Your Schema

Create a schema that describes your data structure:
2

Add Validation Hook

Apply the validation hook to your service:
3

Automatic Validation

The hook automatically validates incoming data and throws BadRequest errors if validation fails.

Validation with Type Coercion

The default AJV instance enables type coercion, automatically converting data types:

Method-Specific Validation

Using getDataValidator

Create different validators for create, update, and patch:

Query Validation

Validate query parameters to ensure safe database queries:

Query Operators

The querySyntax helper creates schemas supporting Feathers query operators:

Custom Query Extensions

Extend query syntax with custom operators:

Validation with Custom AJV

Use a custom AJV instance for advanced validation:
1

Create Custom AJV

2

Add Custom Keywords

3

Use with Schema

Validation Errors

Validation failures throw BadRequest errors with detailed information:

Array Validation

The validateData hook automatically handles array data:

Best Practices

Apply validation hooks to prevent invalid data from entering your system:
While convenient, type coercion can hide bugs. Consider disabling it for stricter validation:
Always validate queries, especially with SQL databases:
Prevent unexpected properties in your schemas:

Next Steps

Schema Resolvers

Learn how to transform data with resolvers

Schema Overview

Back to schema system overview