json-schema-library
Advanced tools
Comparing version 7.3.1 to 7.3.2
@@ -5,3 +5,4 @@ import { Draft } from "../draft"; | ||
* compiles the input root schema for `$ref` resolution and returns it again | ||
* @attention this modifies input schema but maintains object-structure | ||
* @attention this modifies input schema but maintains data-structure and thus returns | ||
* the same object with JSON.stringify | ||
* | ||
@@ -15,4 +16,4 @@ * for a compiled json-schema you can call getRef on any contained schema (location of type). | ||
* @param [force] = false - force compile json-schema | ||
* @return compiled copy of input json-schema | ||
* @return compiled input json-schema | ||
*/ | ||
export default function compileSchema(draft: Draft, schemaToCompile: JSONSchema, rootSchema?: JSONSchema, force?: boolean): JSONSchema; |
import { JSONSchema, JSONPointer } from "./types"; | ||
import { Draft as Core } from "./draft"; | ||
import { Draft } from "./draft"; | ||
/** | ||
@@ -7,11 +7,11 @@ * Returns the json-schema of a data-json-pointer. | ||
* Notes | ||
* - Uses core.step to walk through data and schema | ||
* - Uses draft.step to walk through data and schema | ||
* | ||
* @param core | ||
* @param draft | ||
* @param pointer - json pointer in data to get the json schema for | ||
* @param [data] - the data object, which includes the json pointers value. This is optional, as | ||
* long as no oneOf, anyOf, etc statement is part of the pointers schema | ||
* @param [schema] - the json schema to iterate. Defaults to core.rootSchema | ||
* @param [schema] - the json schema to iterate. Defaults to draft.rootSchema | ||
* @return json schema object of the json-pointer or an error | ||
*/ | ||
export default function getSchema(core: Core, pointer: JSONPointer, data?: unknown, schema?: JSONSchema): JSONSchema; | ||
export default function getSchema(draft: Draft, pointer: JSONPointer, data?: unknown, schema?: JSONSchema): JSONSchema; |
@@ -5,2 +5,3 @@ /* eslint max-statements-per-line: ["error", { "max": 2 }] */ | ||
import getRef from "./getRef"; | ||
import { get } from "gson-pointer"; | ||
const COMPILED = "__compiled"; | ||
@@ -13,3 +14,4 @@ const COMPILED_REF = "__ref"; | ||
* compiles the input root schema for `$ref` resolution and returns it again | ||
* @attention this modifies input schema but maintains object-structure | ||
* @attention this modifies input schema but maintains data-structure and thus returns | ||
* the same object with JSON.stringify | ||
* | ||
@@ -23,3 +25,3 @@ * for a compiled json-schema you can call getRef on any contained schema (location of type). | ||
* @param [force] = false - force compile json-schema | ||
* @return compiled copy of input json-schema | ||
* @return compiled input json-schema | ||
*/ | ||
@@ -33,3 +35,5 @@ export default function compileSchema(draft, schemaToCompile, rootSchema = schemaToCompile, force = false) { | ||
const compiledSchema = JSON.parse(rootSchemaAsString); | ||
// flag this schema as compiled | ||
Object.defineProperty(compiledSchema, COMPILED, { enumerable: false, value: true }); | ||
// add getRef-helper to this object | ||
Object.defineProperty(compiledSchema, GET_REF, { | ||
@@ -53,3 +57,11 @@ enumerable: false, | ||
eachSchema(compiledSchema, (schema, pointer) => { | ||
var _a; | ||
if (schema.id) { | ||
// if this is a schema being merged on root object, we cannot override | ||
// parents locations, but must reuse it | ||
if (schema.id.startsWith("http") && /(allOf|anyOf|oneOf)\/\d+$/.test(pointer)) { | ||
const parentPointer = pointer.replace(/\/(allOf|anyOf|oneOf)\/\d+$/, ""); | ||
const parentSchema = get(compiledSchema, parentPointer); | ||
schema.id = (_a = parentSchema.id) !== null && _a !== void 0 ? _a : schema.id; | ||
} | ||
context.ids[schema.id.replace(suffixes, "")] = pointer; | ||
@@ -56,0 +68,0 @@ } |
@@ -5,2 +5,3 @@ import { eachSchema } from "../../eachSchema"; | ||
import getRef from "../../compile/getRef"; | ||
import { get } from "gson-pointer"; | ||
const COMPILED = "__compiled"; | ||
@@ -54,3 +55,11 @@ const COMPILED_REF = "__ref"; | ||
eachSchema(compiledSchema, (schema, pointer) => { | ||
var _a; | ||
if (schema.$id) { | ||
// if this is a schema being merged on root object, we cannot override | ||
// parents locations, but must reuse it | ||
if (schema.$id.startsWith("http") && /(allOf|anyOf|oneOf)\/\d+$/.test(pointer)) { | ||
const parentPointer = pointer.replace(/\/(allOf|anyOf|oneOf)\/\d+$/, ""); | ||
const parentSchema = get(compiledSchema, parentPointer); | ||
schema.$id = (_a = parentSchema.$id) !== null && _a !== void 0 ? _a : schema.$id; | ||
} | ||
context.ids[schema.$id.replace(suffixes, "")] = pointer; | ||
@@ -57,0 +66,0 @@ } |
@@ -20,3 +20,3 @@ import gp from "gson-pointer"; | ||
} | ||
console.log(`Invalid schema in ${pointer}/${key}/${defId}`); | ||
// console.log(`Invalid schema in ${pointer}/${key}/${defId}`); | ||
}); | ||
@@ -23,0 +23,0 @@ } |
@@ -8,21 +8,22 @@ import gp from "gson-pointer"; | ||
* Notes | ||
* - Uses core.step to walk through data and schema | ||
* - Uses draft.step to walk through data and schema | ||
* | ||
* @param core | ||
* @param draft | ||
* @param pointer - json pointer in data to get the json schema for | ||
* @param [data] - the data object, which includes the json pointers value. This is optional, as | ||
* long as no oneOf, anyOf, etc statement is part of the pointers schema | ||
* @param [schema] - the json schema to iterate. Defaults to core.rootSchema | ||
* @param [schema] - the json schema to iterate. Defaults to draft.rootSchema | ||
* @return json schema object of the json-pointer or an error | ||
*/ | ||
export default function getSchema(core, pointer, data, schema = core.rootSchema) { | ||
export default function getSchema(draft, pointer, data, schema = draft.rootSchema) { | ||
const frags = gp.split(pointer); | ||
return _get(core, schema, frags, pointer, data); | ||
schema = draft.resolveRef(schema); | ||
return _get(draft, schema, frags, pointer, data); | ||
} | ||
function _get(core, schema, frags, pointer, data = emptyObject) { | ||
function _get(draft, schema, frags, pointer, data = emptyObject) { | ||
if (frags.length === 0) { | ||
return core.resolveRef(schema); | ||
return draft.resolveRef(schema); | ||
} | ||
const key = frags.shift(); // step key | ||
schema = core.step(key, schema, data, pointer); // step schema | ||
schema = draft.step(key, schema, data, pointer); // step schema | ||
if (isJSONError(schema)) { | ||
@@ -33,3 +34,3 @@ return schema; | ||
data = data[key]; // step data | ||
return _get(core, schema, frags, `${pointer}/${key}`, data); | ||
return _get(draft, schema, frags, `${pointer}/${key}`, data); | ||
} |
@@ -8,2 +8,3 @@ /* eslint max-statements-per-line: ["error", { "max": 2 }] */ | ||
import { Context } from "./types"; | ||
import { get } from "gson-pointer"; | ||
@@ -18,3 +19,4 @@ const COMPILED = "__compiled"; | ||
* compiles the input root schema for `$ref` resolution and returns it again | ||
* @attention this modifies input schema but maintains object-structure | ||
* @attention this modifies input schema but maintains data-structure and thus returns | ||
* the same object with JSON.stringify | ||
* | ||
@@ -28,3 +30,3 @@ * for a compiled json-schema you can call getRef on any contained schema (location of type). | ||
* @param [force] = false - force compile json-schema | ||
* @return compiled copy of input json-schema | ||
* @return compiled input json-schema | ||
*/ | ||
@@ -45,3 +47,5 @@ export default function compileSchema( | ||
// flag this schema as compiled | ||
Object.defineProperty(compiledSchema, COMPILED, { enumerable: false, value: true }); | ||
// add getRef-helper to this object | ||
Object.defineProperty(compiledSchema, GET_REF, { | ||
@@ -73,4 +77,12 @@ enumerable: false, | ||
const getRoot = () => compiledSchema; | ||
eachSchema(compiledSchema, (schema, pointer) => { | ||
if (schema.id) { | ||
// if this is a schema being merged on root object, we cannot override | ||
// parents locations, but must reuse it | ||
if (schema.id.startsWith("http") && /(allOf|anyOf|oneOf)\/\d+$/.test(pointer)) { | ||
const parentPointer = pointer.replace(/\/(allOf|anyOf|oneOf)\/\d+$/, ""); | ||
const parentSchema = get(compiledSchema, parentPointer); | ||
schema.id = parentSchema.id ?? schema.id; | ||
} | ||
context.ids[schema.id.replace(suffixes, "")] = pointer; | ||
@@ -77,0 +89,0 @@ } |
@@ -8,2 +8,3 @@ /* eslint max-statements-per-line: ["error", { "max": 2 }] */ | ||
import { JSONSchema } from "../../types"; | ||
import { get } from "gson-pointer"; | ||
@@ -79,2 +80,9 @@ const COMPILED = "__compiled"; | ||
if (schema.$id) { | ||
// if this is a schema being merged on root object, we cannot override | ||
// parents locations, but must reuse it | ||
if (schema.$id.startsWith("http") && /(allOf|anyOf|oneOf)\/\d+$/.test(pointer)) { | ||
const parentPointer = pointer.replace(/\/(allOf|anyOf|oneOf)\/\d+$/, ""); | ||
const parentSchema = get(compiledSchema, parentPointer); | ||
schema.$id = parentSchema.$id ?? schema.$id; | ||
} | ||
context.ids[schema.$id.replace(suffixes, "")] = pointer; | ||
@@ -81,0 +89,0 @@ } |
@@ -38,3 +38,3 @@ import gp from "gson-pointer"; | ||
} | ||
console.log(`Invalid schema in ${pointer}/${key}/${defId}`); | ||
// console.log(`Invalid schema in ${pointer}/${key}/${defId}`); | ||
}); | ||
@@ -41,0 +41,0 @@ } |
import gp from "gson-pointer"; | ||
import { JSONSchema, JSONPointer, isJSONError } from "./types"; | ||
import { Draft as Core } from "./draft"; | ||
import { Draft } from "./draft"; | ||
@@ -11,23 +11,24 @@ const emptyObject = {}; | ||
* Notes | ||
* - Uses core.step to walk through data and schema | ||
* - Uses draft.step to walk through data and schema | ||
* | ||
* @param core | ||
* @param draft | ||
* @param pointer - json pointer in data to get the json schema for | ||
* @param [data] - the data object, which includes the json pointers value. This is optional, as | ||
* long as no oneOf, anyOf, etc statement is part of the pointers schema | ||
* @param [schema] - the json schema to iterate. Defaults to core.rootSchema | ||
* @param [schema] - the json schema to iterate. Defaults to draft.rootSchema | ||
* @return json schema object of the json-pointer or an error | ||
*/ | ||
export default function getSchema( | ||
core: Core, | ||
draft: Draft, | ||
pointer: JSONPointer, | ||
data?: unknown, | ||
schema: JSONSchema = core.rootSchema | ||
schema: JSONSchema = draft.rootSchema | ||
): JSONSchema { | ||
const frags = gp.split(pointer); | ||
return _get(core, schema, frags, pointer, data); | ||
schema = draft.resolveRef(schema); | ||
return _get(draft, schema, frags, pointer, data); | ||
} | ||
function _get( | ||
core: Core, | ||
draft: Draft, | ||
schema: JSONSchema, | ||
@@ -39,7 +40,7 @@ frags: Array<string>, | ||
if (frags.length === 0) { | ||
return core.resolveRef(schema); | ||
return draft.resolveRef(schema); | ||
} | ||
const key = frags.shift(); // step key | ||
schema = core.step(key, schema, data, pointer); // step schema | ||
schema = draft.step(key, schema, data, pointer); // step schema | ||
if (isJSONError(schema)) { | ||
@@ -50,3 +51,3 @@ return schema; | ||
data = data[key]; // step data | ||
return _get(core, schema, frags, `${pointer}/${key}`, data); | ||
return _get(draft, schema, frags, `${pointer}/${key}`, data); | ||
} |
{ | ||
"name": "json-schema-library", | ||
"version": "7.3.1", | ||
"version": "7.3.2", | ||
"description": "Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation", | ||
@@ -15,3 +15,7 @@ "module": "dist/module/index.js", | ||
"test:unit": "TS_NODE_PROJECT=./test/tsconfig.json mocha -r ts-node/register -R spec 'test/unit/**/*.test.ts'", | ||
"test:unit:ci": "TS_NODE_PROJECT=./test/tsconfig.json mocha -r ts-node/register -R json 'test/unit/**/*.test.ts' > test-result-unit.json; exit 0", | ||
"test:spec": "TS_NODE_PROJECT=./test/tsconfig.json mocha -r ts-node/register -R spec 'test/spec/**/*.test.ts'", | ||
"test:spec:4:ci": "TS_NODE_PROJECT=./test/tsconfig.json mocha -r ts-node/register -R json 'test/spec/v4/*.test.ts' > test-result-spec4.json; exit 0", | ||
"test:spec:6:ci": "TS_NODE_PROJECT=./test/tsconfig.json mocha -r ts-node/register -R json 'test/spec/v6/*.test.ts' > test-result-spec6.json; exit 0", | ||
"test:spec:7:ci": "TS_NODE_PROJECT=./test/tsconfig.json mocha -r ts-node/register -R json 'test/spec/v7/*.test.ts' > test-result-spec7.json; exit 0", | ||
"test:4": "TS_NODE_PROJECT=./test/tsconfig.json mocha -r ts-node/register -R spec 'test/spec/v4/*.test.ts'", | ||
@@ -18,0 +22,0 @@ "test:6": "TS_NODE_PROJECT=./test/tsconfig.json mocha -r ts-node/register -R spec 'test/spec/v6/*.test.ts'", |
@@ -21,6 +21,8 @@ <p align="center"><img src="./docs/json-schema-library.png" width="256" alt="json-schema-library"></p> | ||
- This package is tested on node v16+ and in latest browsers | ||
- This library currently supports draft4, draft6 and draft7 features [@see benchmark](https://github.com/sagold/json-schema-benchmark) | ||
[](https://github.com/sagold/json-schema-library/actions/workflows/ci.yaml) | ||
[](https://github.com/sagold/json-schema-library/actions/workflows/ci.yaml) | ||
This library currently supports draft4, draft6 and draft7 features [@see benchmark](https://github.com/sagold/json-schema-benchmark) | ||
**usage** | ||
@@ -39,2 +41,3 @@ | ||
What follows is a description of the main draft methods. | ||
@@ -41,0 +44,0 @@ |
Sorry, the diff of this file is too big to display
437253
187
8317
898