UNPKG

json-schema-describes-subset

Version:

Tools for static JSON schema analysis, including functions to determine if one schema describes a subset of another or if a schema describes the empty set or to convert a schema to its disjunctive normal form (DNF).

44 lines 1.69 kB
import intersection from 'lodash/intersection.js'; import { TypeAtomicSchema } from '../built-in-plugins/type.js'; import { NotSchema } from '../atomic-schema/index.js'; import { InstancesByConstructor } from '../utils/instances-by-constructor/index.js'; import { allJSONSchemaTypes, } from '../json-schema-type/index.js'; export function groupLogicLiteralConjuncts(literals) { const atomicSchemasByConstructor = new InstancesByConstructor(); const negatedAtomicSchemasByConstructor = new InstancesByConstructor(); let hasOnlyBooleanLiterals = true; for (const literal of literals) { if (literal === false) { return { booleanSchema: false }; } if (literal === true) { continue; } hasOnlyBooleanLiterals = false; if (literal instanceof NotSchema) { negatedAtomicSchemasByConstructor.push(literal.not); } else { atomicSchemasByConstructor.push(literal); } } const typeSchemas = atomicSchemasByConstructor.get(TypeAtomicSchema); /* delete type schemas, since this is returned separately */ atomicSchemasByConstructor.delete(TypeAtomicSchema); const types = typeSchemas.length === 0 ? [...allJSONSchemaTypes] : intersection(...typeSchemas.map(({ type }) => type)); if (types.length === 0) { /* no possible type */ return { booleanSchema: false }; } if (hasOnlyBooleanLiterals) { return { booleanSchema: true }; } return { atomicSchemasByConstructor, negatedAtomicSchemasByConstructor, types, }; } //# sourceMappingURL=group-logic-literal-conjuncts.js.map