Use this file to discover all available pages before exploring further.
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.
Create different validators for create, update, and patch:
Automatic Generation
Custom Validators
Single Hook
import { getDataValidator } from '@feathersjs/schema'import { Ajv } from '@feathersjs/schema'const userDataSchema = { $id: 'UserData', type: 'object', required: ['email', 'password'], properties: { email: { type: 'string' }, password: { type: 'string' }, name: { type: 'string' } }} as constconst validators = getDataValidator(userDataSchema, new Ajv())// validators.create - Uses schema as-is// validators.update - Same as create// validators.patch - Same as create but no required fieldsapp.service('users').hooks({ create: [validateData(validators.create)], update: [validateData(validators.update)], patch: [validateData(validators.patch)]})
import { getDataValidator } from '@feathersjs/typebox'import { Type } from '@feathersjs/typebox'import { Ajv } from '@feathersjs/schema'const userCreateSchema = Type.Object({ email: Type.String(), password: Type.String(), name: Type.String()})const userPatchSchema = Type.Object({ email: Type.Optional(Type.String()), name: Type.Optional(Type.String()) // password not allowed in patch})const validators = getDataValidator({ create: userCreateSchema, update: userCreateSchema, patch: userPatchSchema}, new Ajv())app.service('users').hooks({ create: [validateData(validators)]})
import { validateData, getDataValidator } from '@feathersjs/schema'import { Ajv } from '@feathersjs/schema'const validators = getDataValidator(userDataSchema, new Ajv())// The hook automatically uses the correct validator based on context.methodapp.service('users').hooks([ validateData(validators)])
Use a custom AJV instance for advanced validation:
1
Create Custom AJV
import Ajv from 'ajv'import addFormats from 'ajv-formats'const customAjv = new Ajv({ coerceTypes: true, addUsedSchema: false, removeAdditional: true // Remove properties not in schema})addFormats(customAjv)