@hyperjump/json-schema
Advanced tools
Comparing version 0.17.2 to 0.18.0
const isObject = (value) => typeof value === "object" && !Array.isArray(value) && value !== null; | ||
const escapeRegExp = (string) => string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); | ||
module.exports = { isObject, escapeRegExp }; | ||
const splitUrl = (url) => { | ||
const indexOfHash = url.indexOf("#"); | ||
const ndx = indexOfHash === -1 ? url.length : indexOfHash; | ||
const urlReference = url.slice(0, ndx); | ||
const urlFragment = url.slice(ndx + 1); | ||
return [decodeURI(urlReference), decodeURI(urlFragment)]; | ||
}; | ||
module.exports = { isObject, escapeRegExp, splitUrl }; |
@@ -7,3 +7,3 @@ const { Core, Schema } = require("@hyperjump/json-schema-core"); | ||
const validationMetaSchema = require("../meta/draft/2019-09/meta/validation"); | ||
const metaDataMetaSchema = require("../meta/draft/2019-09/meta/meta-data.js"); | ||
const metaDataMetaSchema = require("../meta/draft/2019-09/meta/meta-data"); | ||
const formatMetaSchema = require("../meta/draft/2019-09/meta/format"); | ||
@@ -10,0 +10,0 @@ const contentMetaSchema = require("../meta/draft/2019-09/meta/content"); |
@@ -1,2 +0,2 @@ | ||
const { Core, Schema } = require("@hyperjump/json-schema-core"); | ||
const { Core, Schema, InvalidSchemaError } = require("@hyperjump/json-schema-core"); | ||
const Keywords = require("./keywords"); | ||
@@ -23,4 +23,5 @@ | ||
VERBOSE: Core.VERBOSE, | ||
Keywords: Keywords | ||
Keywords: Keywords, | ||
InvalidSchemaError: InvalidSchemaError | ||
}; | ||
@@ -21,4 +21,4 @@ const { Core, Schema } = require("@hyperjump/json-schema-core"); | ||
const collectEvaluatedItems = (items, instance, ast, dynamicAnchors) => { | ||
return items.reduce((acc, schemaUrl) => { | ||
const collectEvaluatedItems = (allOf, instance, ast, dynamicAnchors) => { | ||
return allOf.reduce((acc, schemaUrl) => { | ||
const itemIndexes = acc !== false && Core.collectEvaluatedItems(schemaUrl, instance, ast, dynamicAnchors); | ||
@@ -25,0 +25,0 @@ return itemIndexes !== false && new Set([...acc, ...itemIndexes]); |
@@ -18,3 +18,3 @@ const { Core, Schema } = require("@hyperjump/json-schema-core"); | ||
const propertyNames = Core.collectEvaluatedProperties(schemaUrl, instance, ast, dynamicAnchors); | ||
return propertyNames !== false ? [...(acc || []), ...propertyNames] : acc; | ||
return propertyNames !== false ? [...acc || [], ...propertyNames] : acc; | ||
}, false); | ||
@@ -26,3 +26,3 @@ }; | ||
const itemIndexes = Core.collectEvaluatedItems(schemaUrl, instance, ast, dynamicAnchors); | ||
return itemIndexes !== false ? new Set([...(acc || []), ...itemIndexes]) : acc; | ||
return itemIndexes !== false ? new Set([...acc || [], ...itemIndexes]) : acc; | ||
}, false); | ||
@@ -29,0 +29,0 @@ }; |
const { Core, Schema } = require("@hyperjump/json-schema-core"); | ||
const { splitUrl } = require("@hyperjump/json-schema-core/lib/common"); | ||
const { splitUrl } = require("../common"); | ||
@@ -4,0 +4,0 @@ |
const { Core, Schema, Instance } = require("@hyperjump/json-schema-core"); | ||
const Pact = require("@hyperjump/pact"); | ||
const compile = (schema, ast) => { | ||
const compile = async (schema, ast) => { | ||
if (Schema.typeOf(schema, "array")) { | ||
return Pact.pipeline([ | ||
Schema.map((itemSchema) => Core.compileSchema(itemSchema, ast)), | ||
Pact.all | ||
], schema); | ||
const tupleItems = await Schema.map((itemSchema) => Core.compileSchema(itemSchema, ast), schema); | ||
return Promise.all(tupleItems); | ||
} else { | ||
@@ -12,0 +9,0 @@ return Core.compileSchema(schema, ast); |
@@ -5,5 +5,4 @@ const { Schema, Instance } = require("@hyperjump/json-schema-core"); | ||
const compile = (schema) => Schema.value(schema); | ||
const interpret = (maxProperties, instance) => !Instance.typeOf(instance, "object") || Instance.keys(instance).length <= maxProperties; | ||
module.exports = { compile, interpret }; |
@@ -5,4 +5,4 @@ const { Schema, Instance } = require("@hyperjump/json-schema-core"); | ||
const compile = (schema) => Schema.value(schema); | ||
const interpret = (minLength, instance) => !Instance.typeOf(instance, "string") || Instance.value(instance).length >= minLength; | ||
const interpret = (minLength, instance) => !Instance.typeOf(instance, "string") || Instance.length(instance) >= minLength; | ||
module.exports = { compile, interpret }; |
@@ -5,8 +5,4 @@ const { Schema, Instance } = require("@hyperjump/json-schema-core"); | ||
const compile = (schema) => Schema.value(schema); | ||
const interpret = (minProperties, instance) => !Instance.typeOf(instance, "object") || Instance.keys(instance).length >= minProperties; | ||
const interpret = (minProperties, instance) => { | ||
const value = Instance.value(instance); | ||
return !Instance.typeOf(instance, "object") || Object.keys(value).length >= minProperties; | ||
}; | ||
module.exports = { compile, interpret }; |
const { Core, Schema } = require("@hyperjump/json-schema-core"); | ||
const Pact = require("@hyperjump/pact"); | ||
const compile = (schema, ast) => Pact.pipeline([ | ||
Schema.map(async (itemSchema) => Core.compileSchema(await itemSchema, ast)), | ||
Pact.all | ||
], schema); | ||
const compile = async (schema, ast) => { | ||
const oneOf = await Schema.map((itemSchema) => Core.compileSchema(itemSchema, ast), schema); | ||
return Promise.all(oneOf); | ||
}; | ||
@@ -10,0 +9,0 @@ const interpret = (oneOf, instance, ast, dynamicAnchors) => { |
@@ -7,5 +7,5 @@ const { Schema, Instance } = require("@hyperjump/json-schema-core"); | ||
const interpret = (required, instance) => { | ||
return !Instance.typeOf(instance, "object") || required.every((propertyName) => Instance.value(instance).hasOwnProperty(propertyName)); | ||
return !Instance.typeOf(instance, "object") || required.every((propertyName) => Object.prototype.hasOwnProperty.call(Instance.value(instance), propertyName)); | ||
}; | ||
module.exports = { compile, interpret }; |
@@ -13,5 +13,5 @@ const { Schema, Instance } = require("@hyperjump/json-schema-core"); | ||
const normalizedItems = Instance.map((item) => jsonStringify(Instance.value(item)), instance); | ||
return (new Set(normalizedItems)).size === normalizedItems.length; | ||
return new Set(normalizedItems).size === normalizedItems.length; | ||
}; | ||
module.exports = { compile, interpret }; |
{ | ||
"name": "@hyperjump/json-schema", | ||
"version": "0.17.2", | ||
"version": "0.18.0", | ||
"description": "A JSON Schema Validator", | ||
"main": "lib/index.js", | ||
"exports": { | ||
"require": "./lib/index.js", | ||
"import": "./lib/index.mjs" | ||
}, | ||
"scripts": { | ||
"clean": "xargs -a .gitignore rm -rf", | ||
"lint": "eslint lib", | ||
"test": "mocha 'lib/**/*.spec.js'", | ||
"test": "mocha --require ts-node/register 'lib/**/*.spec.ts'", | ||
"build": "rollup --config rollup.config.js", | ||
@@ -33,15 +37,22 @@ "prepublishOnly": "npm run build" | ||
"@rollup/plugin-node-resolve": "^7.1.3", | ||
"chai": "^4.2.0", | ||
"eslint": "^4.19.1", | ||
"eslint-import-resolver-node": "^0.3.4", | ||
"eslint-plugin-import": "^2.23.4", | ||
"@types/chai": "^4.2.21", | ||
"@types/mocha": "^9.0.0", | ||
"@typescript-eslint/eslint-plugin": "^4.29.2", | ||
"@typescript-eslint/parser": "^4.29.2", | ||
"chai": "^4.3.4", | ||
"eslint": "^7.32.0", | ||
"eslint-import-resolver-node": "^0.3.6", | ||
"eslint-import-resolver-typescript": "^2.4.0", | ||
"eslint-plugin-import": "^2.24.0", | ||
"json-schema-test-suite": "github:json-schema-org/JSON-Schema-Test-Suite", | ||
"mocha": "^8.4.0", | ||
"mocha": "^9.0.3", | ||
"rollup": "^2.50.5", | ||
"rollup-plugin-terser": "^5.3.1" | ||
"rollup-plugin-terser": "^5.3.1", | ||
"ts-node": "^10.2.0", | ||
"typescript": "^4.3.5" | ||
}, | ||
"dependencies": { | ||
"@hyperjump/json-schema-core": "^0.21.0", | ||
"@hyperjump/json-schema-core": "^0.22.0", | ||
"fastest-stable-stringify": "^2.0.2" | ||
} | ||
} |
@@ -20,5 +20,4 @@ # Hyperjump - JSON Schema Validator | ||
## Install | ||
JSV is designed to run in a vanilla node.js environment, but has no dependencies | ||
on node.js specific libraries so it can be bundled for the browser. No | ||
compilers, preprocessors, or bundlers are used. | ||
JSV includes support for node.js JavaScript (CommonJS and ES Modules), | ||
TypeScript, and browsers. | ||
@@ -61,3 +60,3 @@ ### Node.js | ||
"type": "string" | ||
} | ||
}; | ||
JsonSchema.add(schemaJson); | ||
@@ -94,4 +93,36 @@ const schema = await JsonSchema.get("http://example.com/schemas/string"); | ||
## TypeScript | ||
Although the package is written in JavaScript, type definitions are included for | ||
TypeScript support. The following example shows the types you might want to | ||
know. | ||
```typescript | ||
import JsonSchema, { InvalidSchemaError } from "@hyperjump/json-schema"; | ||
import type { SchemaDocument, Validator, Result, Draft202012Schema } from "@hyperjump/json-schema"; | ||
const schemaJson: Draft202012Schema = { | ||
"$id": "https://json-schema.hyperjump.io/schema", | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"type": "string" | ||
}; | ||
JsonSchema.add(schemaJson); | ||
const schema: SchemaDocument = await JsonSchema.get("https://json-schema.hyperjump.io/schema"); | ||
try { | ||
const isString: Validator = await JsonSchema.validate(schema); | ||
const result: Result = isString("foo"); | ||
console.log("isString:", result.valid); | ||
} catch (error: unknown) { | ||
if (error instanceof InvalidSchemaError) { | ||
console.log(error.output); | ||
} else { | ||
console.log(error); | ||
} | ||
} | ||
``` | ||
## API | ||
* **add**: (schema: object, url?: URI, schemaVersion?: string) => undefined | ||
* **add**: (schema: object, url?: URI, schemaVersion?: string) => SDoc | ||
@@ -132,3 +163,3 @@ Load a schema. See [JSC - $id](https://github.com/hyperjump-io/json-schema-core#id) | ||
* The format vocabulary | ||
* The format-assertion vocabulary | ||
@@ -135,0 +166,0 @@ ## Contributing |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
4509567
110
28096
177
17
+ Added@hyperjump/json-pointer@0.9.8(transitive)
+ Added@hyperjump/json-schema-core@0.22.3(transitive)
+ Added@hyperjump/pact@0.2.5(transitive)
+ Addedjust-curry-it@5.3.0(transitive)
- Removed@hyperjump/json-pointer@0.8.0(transitive)
- Removed@hyperjump/json-schema-core@0.21.1(transitive)
- Removed@hyperjump/pact@0.1.2(transitive)