@cfworker/json-schema
Advanced tools
Comparing version 1.2.18 to 1.3.0
import { Schema, SchemaDraft, ValidationResult } from './types'; | ||
export declare function validate(instance: any, schema: Schema | boolean, draft?: SchemaDraft, lookup?: Record<string, boolean | Schema>, recursiveAnchor?: Schema | null, instanceLocation?: string, schemaLocation?: string, evaluated?: { | ||
export declare function validate(instance: any, schema: Schema | boolean, draft?: SchemaDraft, lookup?: Record<string, boolean | Schema>, shortCircuit?: boolean, recursiveAnchor?: Schema | null, instanceLocation?: string, schemaLocation?: string, evaluated?: { | ||
properties?: Record<string, boolean>; | ||
items?: number; | ||
}): ValidationResult; |
@@ -6,3 +6,3 @@ import { deepCompareStrict } from './deep-compare-strict'; | ||
import { ucs2length } from './ucs2-length'; | ||
export function validate(instance, schema, draft = '2019-09', lookup = dereference(schema), recursiveAnchor = null, instanceLocation = '#', schemaLocation = '#', evaluated) { | ||
export function validate(instance, schema, draft = '2019-09', lookup = dereference(schema), shortCircuit = true, recursiveAnchor = null, instanceLocation = '#', schemaLocation = '#', evaluated) { | ||
if (schema === true) { | ||
@@ -62,3 +62,3 @@ return { valid: true, errors: [] }; | ||
const keywordLocation = `${schemaLocation}/$ref`; | ||
const result = validate(instance, refSchema, draft, lookup, recursiveAnchor, instanceLocation, keywordLocation, evaluated); | ||
const result = validate(instance, refSchema, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, keywordLocation, evaluated); | ||
if (!result.valid) { | ||
@@ -81,3 +81,3 @@ errors.push({ | ||
const keywordLocation = `${schemaLocation}/$recursiveRef`; | ||
const result = validate(instance, recursiveAnchor === null ? schema : recursiveAnchor, draft, lookup, recursiveAnchor, instanceLocation, keywordLocation, evaluated); | ||
const result = validate(instance, recursiveAnchor === null ? schema : recursiveAnchor, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, keywordLocation, evaluated); | ||
if (!result.valid) { | ||
@@ -174,3 +174,3 @@ errors.push({ | ||
const keywordLocation = `${schemaLocation}/not`; | ||
const result = validate(instance, $not, draft, lookup, recursiveAnchor, instanceLocation, keywordLocation); | ||
const result = validate(instance, $not, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, keywordLocation); | ||
if (result.valid) { | ||
@@ -191,3 +191,3 @@ errors.push({ | ||
const subSchema = $anyOf[i]; | ||
const result = validate(instance, subSchema, draft, lookup, recursiveAnchor, instanceLocation, `${keywordLocation}/${i}`, evaluated); | ||
const result = validate(instance, subSchema, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${keywordLocation}/${i}`, evaluated); | ||
errors.push(...result.errors); | ||
@@ -214,3 +214,3 @@ anyValid = anyValid || result.valid; | ||
const subSchema = $allOf[i]; | ||
const result = validate(instance, subSchema, draft, lookup, recursiveAnchor, instanceLocation, `${keywordLocation}/${i}`, evaluated); | ||
const result = validate(instance, subSchema, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${keywordLocation}/${i}`, evaluated); | ||
errors.push(...result.errors); | ||
@@ -235,3 +235,3 @@ allValid = allValid && result.valid; | ||
const matches = $oneOf.filter((subSchema, i) => { | ||
const result = validate(instance, subSchema, draft, lookup, recursiveAnchor, instanceLocation, `${keywordLocation}/${i}`, evaluated); | ||
const result = validate(instance, subSchema, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${keywordLocation}/${i}`, evaluated); | ||
errors.push(...result.errors); | ||
@@ -254,6 +254,6 @@ return result.valid; | ||
const keywordLocation = `${schemaLocation}/if`; | ||
const conditionResult = validate(instance, $if, draft, lookup, recursiveAnchor, instanceLocation, keywordLocation, evaluated).valid; | ||
const conditionResult = validate(instance, $if, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, keywordLocation, evaluated).valid; | ||
if (conditionResult) { | ||
if ($then !== undefined) { | ||
const thenResult = validate(instance, $then, draft, lookup, recursiveAnchor, instanceLocation, `${schemaLocation}/then`, evaluated); | ||
const thenResult = validate(instance, $then, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${schemaLocation}/then`, evaluated); | ||
if (!thenResult.valid) { | ||
@@ -270,3 +270,3 @@ errors.push({ | ||
else if ($else !== undefined) { | ||
const elseResult = validate(instance, $else, draft, lookup, recursiveAnchor, instanceLocation, `${schemaLocation}/else`, evaluated); | ||
const elseResult = validate(instance, $else, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${schemaLocation}/else`, evaluated); | ||
if (!elseResult.valid) { | ||
@@ -316,3 +316,3 @@ errors.push({ | ||
const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`; | ||
const result = validate(key, $propertyNames, draft, lookup, recursiveAnchor, subInstancePointer, keywordLocation); | ||
const result = validate(key, $propertyNames, draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, keywordLocation); | ||
if (!result.valid) { | ||
@@ -350,3 +350,3 @@ errors.push({ | ||
if (key in instance) { | ||
const result = validate(instance, $dependentSchemas[key], draft, lookup, recursiveAnchor, instanceLocation, `${keywordLocation}/${encodePointer(key)}`, evaluated); | ||
const result = validate(instance, $dependentSchemas[key], draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${keywordLocation}/${encodePointer(key)}`, evaluated); | ||
if (!result.valid) { | ||
@@ -381,3 +381,3 @@ errors.push({ | ||
else { | ||
const result = validate(instance, propsOrSchema, draft, lookup, recursiveAnchor, instanceLocation, `${keywordLocation}/${encodePointer(key)}`); | ||
const result = validate(instance, propsOrSchema, draft, lookup, shortCircuit, recursiveAnchor, instanceLocation, `${keywordLocation}/${encodePointer(key)}`); | ||
if (!result.valid) { | ||
@@ -407,3 +407,3 @@ errors.push({ | ||
const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`; | ||
const result = validate(instance[key], $properties[key], draft, lookup, recursiveAnchor, subInstancePointer, `${keywordLocation}/${encodePointer(key)}`); | ||
const result = validate(instance[key], $properties[key], draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, `${keywordLocation}/${encodePointer(key)}`); | ||
if (result.valid) { | ||
@@ -413,3 +413,3 @@ evaluated.properties[key] = thisEvaluated[key] = true; | ||
else { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push({ | ||
@@ -421,3 +421,4 @@ instanceLocation, | ||
}, ...result.errors); | ||
break; | ||
if (stop) | ||
break; | ||
} | ||
@@ -436,3 +437,3 @@ } | ||
const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`; | ||
const result = validate(instance[key], subSchema, draft, lookup, recursiveAnchor, subInstancePointer, `${keywordLocation}/${encodePointer(pattern)}`); | ||
const result = validate(instance[key], subSchema, draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, `${keywordLocation}/${encodePointer(pattern)}`); | ||
if (result.valid) { | ||
@@ -442,3 +443,3 @@ evaluated.properties[key] = thisEvaluated[key] = true; | ||
else { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push({ | ||
@@ -461,3 +462,3 @@ instanceLocation, | ||
const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`; | ||
const result = validate(instance[key], $additionalProperties, draft, lookup, recursiveAnchor, subInstancePointer, keywordLocation); | ||
const result = validate(instance[key], $additionalProperties, draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, keywordLocation); | ||
if (result.valid) { | ||
@@ -467,3 +468,3 @@ evaluated.properties[key] = true; | ||
else { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push({ | ||
@@ -483,3 +484,3 @@ instanceLocation, | ||
const subInstancePointer = `${instanceLocation}/${encodePointer(key)}`; | ||
const result = validate(instance[key], $unevaluatedProperties, draft, lookup, recursiveAnchor, subInstancePointer, keywordLocation); | ||
const result = validate(instance[key], $unevaluatedProperties, draft, lookup, shortCircuit, recursiveAnchor, subInstancePointer, keywordLocation); | ||
if (result.valid) { | ||
@@ -528,5 +529,5 @@ evaluated.properties[key] = true; | ||
for (; i < length2; i++) { | ||
const result = validate(instance[i], $items[i], draft, lookup, recursiveAnchor, `${instanceLocation}/${i}`, `${keywordLocation}/${i}`); | ||
const result = validate(instance[i], $items[i], draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, `${keywordLocation}/${i}`); | ||
if (!result.valid) { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push({ | ||
@@ -538,3 +539,4 @@ instanceLocation, | ||
}, ...result.errors); | ||
break; | ||
if (stop) | ||
break; | ||
} | ||
@@ -545,5 +547,5 @@ } | ||
for (; i < length; i++) { | ||
const result = validate(instance[i], $items, draft, lookup, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation); | ||
const result = validate(instance[i], $items, draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation); | ||
if (!result.valid) { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push({ | ||
@@ -555,3 +557,4 @@ instanceLocation, | ||
}, ...result.errors); | ||
break; | ||
if (stop) | ||
break; | ||
} | ||
@@ -564,5 +567,5 @@ } | ||
for (; i < length; i++) { | ||
const result = validate(instance[i], $additionalItems, draft, lookup, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation); | ||
const result = validate(instance[i], $additionalItems, draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation); | ||
if (!result.valid) { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push({ | ||
@@ -582,3 +585,3 @@ instanceLocation, | ||
for (i = Math.max(evaluated.items, 0); i < length; i++) { | ||
const result = validate(instance[i], $unevaluatedItems, draft, lookup, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation); | ||
const result = validate(instance[i], $unevaluatedItems, draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation); | ||
if (!result.valid) { | ||
@@ -617,3 +620,3 @@ errors.push({ | ||
for (let i = 0; i < length; i++) { | ||
const result = validate(instance[i], $contains, draft, lookup, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation); | ||
const result = validate(instance[i], $contains, draft, lookup, shortCircuit, recursiveAnchor, `${instanceLocation}/${i}`, keywordLocation); | ||
if (result.valid) { | ||
@@ -620,0 +623,0 @@ contained++; |
@@ -5,6 +5,7 @@ import { Schema, SchemaDraft } from './types'; | ||
private readonly draft; | ||
private readonly shortCircuit; | ||
private readonly lookup; | ||
constructor(schema: Schema | boolean, draft?: SchemaDraft); | ||
constructor(schema: Schema | boolean, draft?: SchemaDraft, shortCircuit?: boolean); | ||
validate(instance: any): import("./types").ValidationResult; | ||
addSchema(schema: Schema, id?: string): void; | ||
} |
import { dereference } from './dereference'; | ||
import { validate } from './validate'; | ||
export class Validator { | ||
constructor(schema, draft = '2019-09') { | ||
constructor(schema, draft = '2019-09', shortCircuit = true) { | ||
this.schema = schema; | ||
this.draft = draft; | ||
this.shortCircuit = shortCircuit; | ||
this.lookup = dereference(schema); | ||
} | ||
validate(instance) { | ||
return validate(instance, this.schema, this.draft, this.lookup); | ||
return validate(instance, this.schema, this.draft, this.lookup, this.shortCircuit); | ||
} | ||
@@ -12,0 +13,0 @@ addSchema(schema, id) { |
{ | ||
"name": "@cfworker/json-schema", | ||
"type": "module", | ||
"version": "1.2.18", | ||
"version": "1.3.0", | ||
"description": "A JSON schema validator that will run on Cloudflare workers. Supports drafts 4, 7, and 2019-09.", | ||
@@ -49,3 +49,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "718240d2d61043e71232f2ad2a58c65a6aad776e" | ||
"gitHead": "b173eb534bc4d6c69fb5e5713962633369521ce0" | ||
} |
@@ -40,1 +40,26 @@ # @cfworker/json-schema | ||
``` | ||
4. Include all errors | ||
By default the validator will stop processing an object or array after the first error. Specifying shortCircuit to false will produce all errors. | ||
```js | ||
const validator = new Validator( | ||
{ | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
email: { type: 'string' }, | ||
number: { type: 'number' }, | ||
required: ['name', 'email', 'number'] | ||
} | ||
}, | ||
'2019-09', | ||
false | ||
); | ||
const result = validator.validate({ | ||
name: 'hello', | ||
email: 5, //invalid type | ||
number: 'Hello' //invalid type | ||
}); | ||
``` |
@@ -19,2 +19,3 @@ import { deepCompareStrict } from './deep-compare-strict'; | ||
lookup = dereference(schema), | ||
shortCircuit = true, | ||
recursiveAnchor: Schema | null = null, | ||
@@ -140,2 +141,3 @@ instanceLocation = '#', | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -173,2 +175,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -282,2 +285,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -309,2 +313,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -341,2 +346,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -371,2 +377,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -399,2 +406,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -412,2 +420,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -436,2 +445,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -499,2 +509,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -546,2 +557,3 @@ subInstancePointer, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -589,2 +601,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -629,2 +642,3 @@ instanceLocation, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -637,3 +651,3 @@ subInstancePointer, | ||
} else { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push( | ||
@@ -648,3 +662,3 @@ { | ||
); | ||
break; | ||
if (stop) break; | ||
} | ||
@@ -671,2 +685,3 @@ } | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -679,3 +694,3 @@ subInstancePointer, | ||
} else { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push( | ||
@@ -707,2 +722,3 @@ { | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -715,3 +731,3 @@ subInstancePointer, | ||
} else { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push( | ||
@@ -740,2 +756,3 @@ { | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -797,2 +814,3 @@ subInstancePointer, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -803,3 +821,3 @@ `${instanceLocation}/${i}`, | ||
if (!result.valid) { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push( | ||
@@ -814,3 +832,3 @@ { | ||
); | ||
break; | ||
if (stop) break; | ||
} | ||
@@ -825,2 +843,3 @@ } | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -831,3 +850,3 @@ `${instanceLocation}/${i}`, | ||
if (!result.valid) { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push( | ||
@@ -842,3 +861,3 @@ { | ||
); | ||
break; | ||
if (stop) break; | ||
} | ||
@@ -858,2 +877,3 @@ } | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -864,3 +884,3 @@ `${instanceLocation}/${i}`, | ||
if (!result.valid) { | ||
stop = true; | ||
stop = shortCircuit; | ||
errors.push( | ||
@@ -889,2 +909,3 @@ { | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -934,2 +955,3 @@ `${instanceLocation}/${i}`, | ||
lookup, | ||
shortCircuit, | ||
recursiveAnchor, | ||
@@ -936,0 +958,0 @@ `${instanceLocation}/${i}`, |
@@ -10,3 +10,4 @@ import { dereference } from './dereference'; | ||
private readonly schema: Schema | boolean, | ||
private readonly draft: SchemaDraft = '2019-09' | ||
private readonly draft: SchemaDraft = '2019-09', | ||
private readonly shortCircuit = true | ||
) { | ||
@@ -17,3 +18,9 @@ this.lookup = dereference(schema); | ||
public validate(instance: any) { | ||
return validate(instance, this.schema, this.draft, this.lookup); | ||
return validate( | ||
instance, | ||
this.schema, | ||
this.draft, | ||
this.lookup, | ||
this.shortCircuit | ||
); | ||
} | ||
@@ -20,0 +27,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
107606
2766
65