Add JSON Logic support to Ajv (Another JSON Validator) for handling complex validation logic at runtime.
- Adds a
jsonLogic
keyword to validate data using JSON Logic rules - Adds a
conditionalRequired
keyword to make fields conditionally required based on JSON Logic rules - Supports both CommonJS and ES modules
- TypeScript support with type definitions
npm install ajv-json-logic
// ES Module
import Ajv from 'ajv';
import ajvJsonLogic from 'ajv-json-logic';
// OR CommonJS
// const Ajv = require('ajv');
// const ajvJsonLogic = require('ajv-json-logic');
// Create an instance of Ajv
const ajv = new Ajv({
strictTuples: false, // Required for json-logic
allowUnionTypes: true, // Required for json-logic
});
// Add the conditionalRequired keyword
ajvJsonLogic(ajv);
The jsonLogic
keyword allows you to use JSON Logic rules directly in your schema:
const schema = {
type: 'object',
properties: {
age: { type: 'number' },
score: { type: 'number' },
},
jsonLogic: {
and: [{ '>=': [{ var: 'age' }, 18] }, { '>=': [{ var: 'score' }, 70] }],
},
};
const validate = ajv.compile(schema);
// Valid data
console.log(validate({ age: 20, score: 85 })); // true
// Invalid data
console.log(validate({ age: 17, score: 85 })); // false
console.log(validate({ age: 20, score: 65 })); // false
The conditionalRequired
keyword makes fields required based on JSON Logic conditions:
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
parentConsent: { type: 'boolean' },
},
conditionalRequired: [
{
content: 'parentConsent',
rule: { '<': [{ var: 'age' }, 18] },
},
],
};
const validate = ajv.compile(schema);
// Valid data - Adult without parent consent
console.log(validate({ name: 'John', age: 25 })); // true
// Valid data - Minor with parent consent
console.log(validate({ name: 'Amy', age: 16, parentConsent: true })); // true
// Invalid data - Minor without parent consent
console.log(validate({ name: 'Bob', age: 16 })); // false
You can also use the object format for a single condition:
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
},
conditionalRequired: {
content: 'name',
rule: { '==': [{ var: 'age' }, 18] },
},
};
The conditionalSchema
keyword is used to apply a schema conditionally based on JSON Logic conditions:
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
parentConsent: { type: 'boolean' },
},
conditionalSchema: [
{
schema: {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
parentConsent: { type: 'boolean' },
},
required: ['name', 'age', 'parentConsent'],
},
rule: { '<': [{ var: 'age' }, 18] },
},
],
};
const validate = ajv.compile(schema);
// Valid data - Adult without parent consent
console.log(validate({ name: 'John', age: 25 })); // true
// Valid data - Minor with parent consent
console.log(validate({ name: 'Amy', age: 16, parentConsent: true })); // true
// Invalid data - Minor without parent consent
console.log(validate({ name: 'Bob', age: 16 })); // false
This library includes TypeScript definitions and can be used in TypeScript projects:
MIT