json-schema-library
Advanced tools
Comparing version 7.0.0 to 7.1.0
@@ -35,2 +35,3 @@ import { createError, createCustomError } from "./lib/utils/createCustomError"; | ||
import { JSONSchema, JSONPointer, JSONError, JSONValidator, JSONTypeValidator } from "./lib/types"; | ||
export type { CreateError, DraftConfig, EachCallback, EachSchemaCallback, ErrorData, JSONError, JSONPointer, JSONSchema, JSONTypeValidator, JSONValidator }; | ||
import { JSType } from "./lib/getTypeOf"; | ||
export type { CreateError, DraftConfig, EachCallback, EachSchemaCallback, ErrorData, JSONError, JSONPointer, JSONSchema, JSONTypeValidator, JSONValidator, JSType }; |
@@ -7,3 +7,3 @@ import step from "../step"; | ||
import resolveAnyOf from "../resolveAnyOf"; | ||
import getTemplate from "../getTemplate"; | ||
import getTemplate, { TemplateOptions } from "../getTemplate"; | ||
import getChildSchemaSelection from "../getChildSchemaSelection"; | ||
@@ -103,3 +103,3 @@ import getSchema from "../getSchema"; | ||
*/ | ||
getTemplate(data?: unknown, schema?: JSONSchema): any; | ||
getTemplate(data?: unknown, schema?: JSONSchema, opts?: TemplateOptions): any; | ||
isValid(data: any, schema?: JSONSchema, pointer?: JSONPointer): boolean; | ||
@@ -106,0 +106,0 @@ resolveAnyOf(data: any, schema: JSONSchema, pointer?: JSONPointer): JSONSchema; |
import { JSONSchema } from "./types"; | ||
import { Draft as Core } from "./draft"; | ||
interface TemplateOptions { | ||
export declare type TemplateOptions = { | ||
/** Add all properties (required and optional) to the generated data */ | ||
addOptionalProps: boolean; | ||
} | ||
}; | ||
declare const _default: (core: Core, data?: any, schema?: JSONSchema, opts?: TemplateOptions) => any; | ||
export default _default; |
@@ -85,4 +85,4 @@ export class Draft { | ||
*/ | ||
getTemplate(data, schema) { | ||
return this.config.getTemplate(this, data, schema); | ||
getTemplate(data, schema, opts) { | ||
return this.config.getTemplate(this, data, schema, opts); | ||
} | ||
@@ -89,0 +89,0 @@ isValid(data, schema, pointer) { |
@@ -194,2 +194,13 @@ /* eslint quote-props: 0, max-statements-per-line: ["error", { "max": 2 }] */ | ||
} | ||
if (schema.if && (schema.then || schema.else)) { | ||
const isValid = core.isValid(d, schema.if); | ||
if (isValid && schema.then) { | ||
const additionalData = core.getTemplate(d, { type: "object", ...schema.then }, opts); | ||
Object.assign(d, additionalData); | ||
} | ||
else if (!isValid && schema.else) { | ||
const additionalData = core.getTemplate(d, { type: "object", ...schema.else }, opts); | ||
Object.assign(d, additionalData); | ||
} | ||
} | ||
// returns object, which is ordered by json-schema | ||
@@ -196,0 +207,0 @@ return d; |
const toString = Object.prototype.toString; | ||
export default function getTypeOf(value) { | ||
// eslint-disable-next-line newline-per-chained-call | ||
return toString.call(value).match(/\s([^\]]+)\]/).pop().toLowerCase(); | ||
return toString | ||
.call(value) | ||
.match(/\s([^\]]+)\]/) | ||
.pop() | ||
.toLowerCase(); | ||
} |
@@ -127,2 +127,19 @@ import getTypeOf from "./getTypeOf"; | ||
} | ||
// @draft >= 07 | ||
if (schema.if && (schema.then || schema.else)) { | ||
// console.log("test if-then-else"); | ||
const isValid = core.isValid(data, schema.if); | ||
if (isValid && schema.then) { | ||
const resolvedThen = step(core, key, schema.then, data, pointer); | ||
if (typeof resolvedThen.type === "string" && resolvedThen.type !== "error") { | ||
return resolvedThen; | ||
} | ||
} | ||
if (!isValid && schema.else) { | ||
const resolvedElse = step(core, key, schema.else, data, pointer); | ||
if (typeof resolvedElse.type === "string" && resolvedElse.type !== "error") { | ||
return resolvedElse; | ||
} | ||
} | ||
} | ||
// find matching property key | ||
@@ -129,0 +146,0 @@ if (getTypeOf(schema.patternProperties) === "object") { |
@@ -51,2 +51,3 @@ import { createError, createCustomError } from "./lib/utils/createCustomError"; | ||
import { JSONSchema, JSONPointer, JSONError, JSONValidator, JSONTypeValidator } from "./lib/types"; | ||
import { JSType } from "./lib/getTypeOf"; | ||
@@ -63,3 +64,4 @@ export type { | ||
JSONTypeValidator, | ||
JSONValidator | ||
JSONValidator, | ||
JSType | ||
}; |
@@ -7,3 +7,3 @@ import step from "../step"; | ||
import resolveAnyOf from "../resolveAnyOf"; | ||
import getTemplate from "../getTemplate"; | ||
import getTemplate, { TemplateOptions } from "../getTemplate"; | ||
import getChildSchemaSelection from "../getChildSchemaSelection"; | ||
@@ -146,4 +146,4 @@ import getSchema from "../getSchema"; | ||
*/ | ||
getTemplate(data?: unknown, schema?: JSONSchema) { | ||
return this.config.getTemplate(this, data, schema); | ||
getTemplate(data?: unknown, schema?: JSONSchema, opts?: TemplateOptions) { | ||
return this.config.getTemplate(this, data, schema, opts); | ||
} | ||
@@ -150,0 +150,0 @@ |
@@ -10,6 +10,6 @@ /* eslint quote-props: 0, max-statements-per-line: ["error", { "max": 2 }] */ | ||
interface TemplateOptions { | ||
export type TemplateOptions = { | ||
/** Add all properties (required and optional) to the generated data */ | ||
addOptionalProps: boolean; | ||
} | ||
}; | ||
@@ -270,2 +270,21 @@ const defaultOptions: TemplateOptions = { | ||
if (schema.if && (schema.then || schema.else)) { | ||
const isValid = core.isValid(d, schema.if); | ||
if (isValid && schema.then) { | ||
const additionalData = core.getTemplate( | ||
d, | ||
{ type: "object", ...schema.then }, | ||
opts | ||
); | ||
Object.assign(d, additionalData); | ||
} else if (!isValid && schema.else) { | ||
const additionalData = core.getTemplate( | ||
d, | ||
{ type: "object", ...schema.else }, | ||
opts | ||
); | ||
Object.assign(d, additionalData); | ||
} | ||
} | ||
// returns object, which is ordered by json-schema | ||
@@ -272,0 +291,0 @@ return d; |
const toString = Object.prototype.toString; | ||
export type JSType = "array"|"bigint"|"boolean"|"function"|"null"|"number"|"object"|"string"|"symbol"|"undefined"; | ||
export type JSType = | ||
| "array" | ||
| "bigint" | ||
| "boolean" | ||
| "function" | ||
| "null" | ||
| "number" | ||
| "object" | ||
| "string" | ||
| "symbol" | ||
| "undefined"; | ||
export default function getTypeOf(value: unknown) : JSType { | ||
export default function getTypeOf(value: unknown): JSType { | ||
// eslint-disable-next-line newline-per-chained-call | ||
return toString.call(value).match(/\s([^\]]+)\]/).pop().toLowerCase(); | ||
return toString | ||
.call(value) | ||
.match(/\s([^\]]+)\]/) | ||
.pop() | ||
.toLowerCase(); | ||
} |
@@ -178,2 +178,20 @@ import getTypeOf from "./getTypeOf"; | ||
// @draft >= 07 | ||
if (schema.if && (schema.then || schema.else)) { | ||
// console.log("test if-then-else"); | ||
const isValid = core.isValid(data, schema.if); | ||
if (isValid && schema.then) { | ||
const resolvedThen = step(core, key, schema.then, data, pointer); | ||
if (typeof resolvedThen.type === "string" && resolvedThen.type !== "error") { | ||
return resolvedThen; | ||
} | ||
} | ||
if (!isValid && schema.else) { | ||
const resolvedElse = step(core, key, schema.else, data, pointer); | ||
if (typeof resolvedElse.type === "string" && resolvedElse.type !== "error") { | ||
return resolvedElse; | ||
} | ||
} | ||
} | ||
// find matching property key | ||
@@ -180,0 +198,0 @@ if (getTypeOf(schema.patternProperties) === "object") { |
{ | ||
"name": "json-schema-library", | ||
"version": "7.0.0", | ||
"version": "7.1.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", |
@@ -742,3 +742,3 @@ <p align="center"><img src="./docs/json-schema-library.png" width="256" alt="json-schema-library"></p> | ||
validateKeyword: { | ||
customKeyword: <JSONValidator>myCustomKeywordValidator | ||
customKeyword: myCustomKeywordValidator as JSONValidator | ||
}, | ||
@@ -763,3 +763,3 @@ // in case for new keywords, or if keyword should be supported on other types | ||
...draft07Config.validateKeyword, | ||
customKeyword: <JSONValidator>myCustomKeywordValidator | ||
customKeyword: myCustomKeywordValidator as JSONValidator | ||
}, | ||
@@ -787,3 +787,3 @@ // in case for new keywords, or if keyword should be supported on other types | ||
validateFormat: { | ||
customFormat: <JSONValidator>myCustomFormatValidator | ||
customFormat: myCustomFormatValidator as JSONValidator | ||
} | ||
@@ -790,0 +790,0 @@ }); |
Sorry, the diff of this file is too big to display
427967
8187