From ba9a62973c6a2a01d0c2e91249a4c83236762211 Mon Sep 17 00:00:00 2001 From: Cemre Mengu Date: Sun, 26 Aug 2018 15:16:40 +0300 Subject: [PATCH] Update Validation-and-Serialization.md (#1094) --- docs/Validation-and-Serialization.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/docs/Validation-and-Serialization.md b/docs/Validation-and-Serialization.md index 58a723c987..60167f78e8 100644 --- a/docs/Validation-and-Serialization.md +++ b/docs/Validation-and-Serialization.md @@ -107,18 +107,30 @@ The function `getSchemas` returns all shared schemas that were added by `addSche #### Schema Compiler -The `schemaCompiler` is a function that returns a function that validates the body, url parameters, headers, and query string. The default `schemaCompiler` returns a function that implements the `ajv` validation interface. Fastify uses it internally to speed the validation up. +The `schemaCompiler` is a function that returns a function that validates the body, url parameters, headers, and query string. The default `schemaCompiler` returns a function that implements the [ajv](https://ajv.js.org/) validation interface. Fastify uses it internally to speed the validation up. -While you cannot change the configuration options of the default `ajv` instance, you can create your own: +Fastify's baseline ajv configuration is: + +```js +{ + removeAdditional: true, + useDefaults: true, + coerceTypes: true +} +``` + +This baseline configuration cannot be modified. If you want to change or set additional config options, you will need to create your own instance and override the existing one like: ```js const fastify = require('fastify')() const Ajv = require('ajv') const ajv = new Ajv({ - // the fastify defaults + // the fastify defaults (if needed) removeAdditional: true, useDefaults: true, coerceTypes: true + // any other options + // ... }) fastify.setSchemaCompiler(function (schema) { return ajv.compile(schema)