npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ajv-json-logic

v1.1.2

Published

JSON Logic integration for Ajv validator

Downloads

40

Readme

ajv-json-logic

npm version License: MIT

Add JSON Logic support to Ajv (Another JSON Validator) for handling complex validation logic at runtime.

Features

  • 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

Installation

npm install ajv-json-logic

Usage

Basic Setup

// 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);

Using the jsonLogic Keyword

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

Using the conditionalRequired Keyword

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

TypeScript Support

This library includes TypeScript definitions and can be used in TypeScript projects:

License

MIT