Adds string sanitization keyword to Ajv JSON Schema validation. Current stable version 1.2.1 (2020). Low release cadence. Integrates validator.js sanitizers into Ajv schemas via a custom 'sanitize' keyword. Differs from alternatives like fast-json-stringify or json-schema-faker by mutating data in-place during validation. Peer dependency on Ajv 8.x. Supports ESM and CommonJS. Allows custom sanitizer functions per schema or globally.
npm install ajv-sanitizerNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to import ajv-sanitizer, create Ajv instance, define schema with sanitize keyword, and mutate data via validate().
Clone data before validate if original needed: const dataClone = JSON.parse(JSON.stringify(data));
Ensure schema property type is 'string' when using sanitize.
Always return a string from your custom sanitizer function.
Use more specific sanitizers (e.g., 'trim', 'escape') or implement custom functions.
Use default import: import ajvSanitizer from 'ajv-sanitizer';
Ensure you call ajvSanitizer(ajv) right after creating the Ajv instance.
Set Ajv option allErrors: true or re-validate after correcting data.