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).
21 lines • 911 B
JavaScript
import { assertJSONSchema, isJSONSchema, } from '../json-schema/index.js';
import { AllOfSchema, AnyOfSchema, NotSchema, } from '../atomic-schema/index.js';
export function extractIfThenElse(split, ifSchema, thenSchema, elseSchema) {
const allOf = [];
if (thenSchema !== undefined) {
allOf.push(new AnyOfSchema([new NotSchema(split(ifSchema)), split(thenSchema)]));
}
if (elseSchema !== undefined) {
allOf.push(new AnyOfSchema([split(ifSchema), split(elseSchema)]));
}
return new AllOfSchema(allOf);
}
export const ifThenElseExtraction = {
extract: ({ schema, split }) => {
const { if: ifValue, then: thenValue, else: elseValue } = schema;
if (!isJSONSchema(ifValue))
return true;
return extractIfThenElse(split, ifValue, assertJSONSchema(thenValue), assertJSONSchema(elseValue));
},
};
//# sourceMappingURL=if-then-else.js.map