json-schema-library
Advanced tools
Comparing version 7.3.9 to 7.4.0
import { JSONSchema, JSONPointer, JSONError } from "./types"; | ||
import { Draft as Core } from "./draft"; | ||
export default function resolveAllOf(core: Core, data: any, schema?: JSONSchema, pointer?: JSONPointer): JSONSchema | JSONError; | ||
import { Draft } from "./draft"; | ||
export default function resolveAllOf(draft: Draft, data: any, schema?: JSONSchema, pointer?: JSONPointer): JSONSchema | JSONError; |
@@ -6,1 +6,5 @@ /** | ||
export default _default; | ||
/** | ||
* returns a new json-schema, where properties are combined and arrays are replaced | ||
*/ | ||
export declare const mergeArraysUnique: <T>(a: Partial<T>, b: Partial<T>) => T; |
@@ -0,11 +1,41 @@ | ||
/** | ||
* resolveAllOf is tricky: | ||
* | ||
* resolve all merges all schemas altough each schema in the list must be used | ||
* for validation. But to use this as a template schema to create data and a | ||
* resolved schema, structural data must be merged. Currently, it is merged in | ||
* all case, but separately validated and resolved. This will break at some | ||
* point, requiring us to be more specific on our current intent (validation | ||
* vs get (resolved) schema) | ||
*/ | ||
import copy from "./utils/copy"; | ||
import merge from "./utils/merge"; | ||
export default function resolveAllOf(core, data, schema = core.rootSchema, pointer = "#") { | ||
import { mergeArraysUnique } from "./utils/merge"; | ||
/** | ||
* resolves schema | ||
* when complete this will have much duplication to step.object etc | ||
*/ | ||
function resolveSchema(draft, schemaToResolve, data) { | ||
var _a; | ||
const schema = { ...((_a = draft.resolveRef(schemaToResolve)) !== null && _a !== void 0 ? _a : {}) }; | ||
// @draft >= 07 | ||
if (schema.if && (schema.then || schema.else)) { | ||
const isValid = draft.isValid(data, schema.if); | ||
if (isValid && schema.then) { | ||
return resolveSchema(draft, schema.then, data); | ||
} | ||
if (!isValid && schema.else) { | ||
return resolveSchema(draft, schema.else, data); | ||
} | ||
delete schema.if; | ||
delete schema.then; | ||
delete schema.else; | ||
} | ||
return schema; | ||
} | ||
export default function resolveAllOf(draft, data, schema = draft.rootSchema, pointer = "#") { | ||
let mergedSchema = copy(schema); | ||
for (let i = 0; i < schema.allOf.length; i += 1) { | ||
const allOfSchema = core.resolveRef(schema.allOf[i]); | ||
// if (core.isValid(data, allOfSchema, pointer) === false) { | ||
// return errors.allOfError({ value: data, pointer, allOf: JSON.stringify(schema.allOf) }); | ||
// } | ||
mergedSchema = merge(mergedSchema, allOfSchema); | ||
const allOfSchema = resolveSchema(draft, schema.allOf[i], data); | ||
mergedSchema = mergeArraysUnique(mergedSchema, allOfSchema); | ||
data = draft.getTemplate(data, mergedSchema); | ||
} | ||
@@ -12,0 +42,0 @@ delete mergedSchema.allOf; |
import deepmerge from "deepmerge"; | ||
// @ts-ignore | ||
const overwriteMerge = (destinationArray, sourceArray) => sourceArray; | ||
@@ -8,1 +7,10 @@ /** | ||
export default (a, b) => deepmerge(a, b, { arrayMerge: overwriteMerge }); | ||
// var d = c.filter((item, pos) => c.indexOf(item) === pos) | ||
const mergeUniqueItems = (destinationArray, sourceArray) => { | ||
const all = destinationArray.concat(sourceArray); | ||
return all.filter((item, pos) => all.indexOf(item) === pos); | ||
}; | ||
/** | ||
* returns a new json-schema, where properties are combined and arrays are replaced | ||
*/ | ||
export const mergeArraysUnique = (a, b) => deepmerge(a, b, { arrayMerge: mergeUniqueItems }); |
@@ -0,11 +1,45 @@ | ||
/** | ||
* resolveAllOf is tricky: | ||
* | ||
* resolve all merges all schemas altough each schema in the list must be used | ||
* for validation. But to use this as a template schema to create data and a | ||
* resolved schema, structural data must be merged. Currently, it is merged in | ||
* all case, but separately validated and resolved. This will break at some | ||
* point, requiring us to be more specific on our current intent (validation | ||
* vs get (resolved) schema) | ||
*/ | ||
import copy from "./utils/copy"; | ||
import merge from "./utils/merge"; | ||
import errors from "./validation/errors"; | ||
import { JSONSchema, JSONPointer, JSONError } from "./types"; | ||
import { Draft as Core } from "./draft"; | ||
import { Draft } from "./draft"; | ||
import { mergeArraysUnique } from "./utils/merge"; | ||
/** | ||
* resolves schema | ||
* when complete this will have much duplication to step.object etc | ||
*/ | ||
function resolveSchema(draft: Draft, schemaToResolve: JSONSchema, data: unknown): JSONSchema { | ||
const schema = { ...(draft.resolveRef(schemaToResolve) ?? {}) }; | ||
// @draft >= 07 | ||
if (schema.if && (schema.then || schema.else)) { | ||
const isValid = draft.isValid(data, schema.if); | ||
if (isValid && schema.then) { | ||
return resolveSchema(draft, schema.then, data); | ||
} | ||
if (!isValid && schema.else) { | ||
return resolveSchema(draft, schema.else, data); | ||
} | ||
delete schema.if; | ||
delete schema.then; | ||
delete schema.else; | ||
} | ||
return schema; | ||
} | ||
export default function resolveAllOf( | ||
core: Core, | ||
draft: Draft, | ||
data: any, | ||
schema: JSONSchema = core.rootSchema, | ||
schema: JSONSchema = draft.rootSchema, | ||
pointer: JSONPointer = "#" | ||
@@ -15,11 +49,8 @@ ): JSONSchema | JSONError { | ||
for (let i = 0; i < schema.allOf.length; i += 1) { | ||
const allOfSchema = core.resolveRef(schema.allOf[i]); | ||
// if (core.isValid(data, allOfSchema, pointer) === false) { | ||
// return errors.allOfError({ value: data, pointer, allOf: JSON.stringify(schema.allOf) }); | ||
// } | ||
mergedSchema = merge(mergedSchema, allOfSchema); | ||
const allOfSchema = resolveSchema(draft, schema.allOf[i], data); | ||
mergedSchema = mergeArraysUnique(mergedSchema, allOfSchema); | ||
data = draft.getTemplate(data, mergedSchema); | ||
} | ||
delete mergedSchema.allOf; | ||
return mergedSchema; | ||
} |
import deepmerge from "deepmerge"; | ||
// @ts-ignore | ||
const overwriteMerge = (destinationArray, sourceArray) => sourceArray; | ||
const overwriteMerge = (destinationArray: unknown[], sourceArray: unknown[]) => sourceArray; | ||
/** | ||
@@ -10,1 +10,13 @@ * returns a new json-schema, where properties are combined and arrays are replaced | ||
deepmerge(a, b, { arrayMerge: overwriteMerge }); | ||
// var d = c.filter((item, pos) => c.indexOf(item) === pos) | ||
const mergeUniqueItems = (destinationArray: unknown[], sourceArray: unknown[]) => { | ||
const all = destinationArray.concat(sourceArray); | ||
return all.filter((item, pos) => all.indexOf(item) === pos); | ||
}; | ||
/** | ||
* returns a new json-schema, where properties are combined and arrays are replaced | ||
*/ | ||
export const mergeArraysUnique = <T>(a: Partial<T>, b: Partial<T>): T => | ||
deepmerge(a, b, { arrayMerge: mergeUniqueItems }); |
{ | ||
"name": "json-schema-library", | ||
"version": "7.3.9", | ||
"version": "7.4.0", | ||
"description": "Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation", | ||
@@ -5,0 +5,0 @@ "module": "dist/module/index.js", |
Sorry, the diff of this file is too big to display
443017
8427