boolean-json-schema

JSON Schema for boolean expressions

Usage no npm install needed!

<script type="module">
  import booleanJsonSchema from 'https://cdn.skypack.dev/boolean-json-schema';
</script>

README

var schema = require('boolean-json-schema')

The package exports a JSON Schema. You will need a compatible library to validate objects:

var validate = require('tv4').validate

The simplest valid expression is just a variable name:

var assert = require('assert')

assert(validate('first', schema))

The syntax is very minimal. The schema permits negation, conjunction, disjunction, and arbitrary combinations:

assert(validate({ not: 'x' }, schema))

assert(validate({ and: [ 'x', 'y' ] }, schema))

assert(validate({ or: [ 'x', 'y' ] }, schema))

assert(validate({ and: [ { or: [ 'x', 'y' ] }, { not: 'z' } ] }, schema))

Conjunctions and disjunctions must have at least two operands:

assert(validate({ and: [ 'x' ]}, schema) === false)
assert(validate({ and: [ 'x', 'y', 'z' ]}, schema) === true)

assert(validate({ or: [ 'x' ]}, schema) === false)
assert(validate({ or: [ 'x', 'y', 'z' ]}, schema) === true)