json-schema-library
Advanced tools
Comparing version 3.1.0 to 3.2.0
const remotes = require("../remotes"); | ||
const precompile = require("./precompileSchema"); | ||
module.exports = function addSchema(url, schema) { | ||
remotes[url] = schema; | ||
schema.id = schema.id || url; | ||
remotes[url] = precompile(null, schema); | ||
}; | ||
const resolveRef = require("../resolveRef.withOverwrite"); | ||
const precompile = require("../precompileSchema"); | ||
@@ -15,4 +16,8 @@ /* eslint no-unused-vars: 0 no-empty-function: 0 */ | ||
set rootSchema(rootSchema) { | ||
if (rootSchema == null) { | ||
return; | ||
} | ||
// initially resolve any $refs on rootschema (in case this is a reference to a remote or self-referenced schema) | ||
this.__rootSchema = resolveRef(rootSchema, rootSchema); | ||
const schema = precompile(this, rootSchema); | ||
this.__rootSchema = resolveRef(schema, schema); | ||
} | ||
@@ -19,0 +24,0 @@ |
@@ -9,2 +9,3 @@ const CoreInterface = require("./CoreInterface"); | ||
const each = require("../each"); | ||
const precompile = require("../precompileSchema"); | ||
@@ -32,2 +33,3 @@ | ||
validate(schema, data, pointer = "#") { | ||
schema = precompile(this, schema); | ||
return validate(this, schema, data, pointer); | ||
@@ -34,0 +36,0 @@ } |
@@ -9,2 +9,3 @@ const CoreInterface = require("./CoreInterface"); | ||
const each = require("../each"); | ||
const precompile = require("../precompileSchema"); | ||
@@ -36,2 +37,3 @@ | ||
validate(schema, data, pointer = "#") { | ||
schema = precompile(this, schema); | ||
return validate(this, schema, data, pointer); | ||
@@ -38,0 +40,0 @@ } |
@@ -16,3 +16,3 @@ const getTypeOf = require("./getTypeOf"); | ||
const targetPointer = `${pointer}/property`; | ||
const targetPointer = `${pointer}/properties`; | ||
if (type === "array") { | ||
@@ -19,0 +19,0 @@ target.forEach((ema, index) => iterateSchema(ema, callback, `${targetPointer}/${index}`)); |
{ | ||
"name": "json-schema-library", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,6 +12,7 @@ # json-schema-library | ||
This package is tested on node v6.9.1. | ||
- This package is tested on node v6.9.1 and in latest Browsers. | ||
- This library currently supports all draft4 features | ||
1. [Draft4](#draft4) | ||
1. [Overview](#overview) | ||
@@ -23,13 +24,2 @@ 1. [Core](#core) | ||
## Draft4 | ||
This library currently supports all draft4 features, **excluding:** | ||
- _$ref_ remote resolution | ||
- scope changes via _id_ | ||
Theese feature would totally change the api for the worse and may not be required in most cases. An decision to | ||
add supporting theese features is currently _pending_. | ||
## Overview | ||
@@ -36,0 +26,0 @@ |
module.exports = { | ||
"http://json-schema.org/draft-04/schema": require("./draft04.json") | ||
"http://json-schema.org/draft-04/schema": require("./draft04.json"), | ||
reset() { | ||
Object.keys(this).forEach((key) => { | ||
if (typeof this[key] !== "function" && key !== "http://json-schema.org/draft-04/schema") { | ||
delete this[key]; | ||
} | ||
}); | ||
} | ||
}; |
@@ -6,5 +6,2 @@ # Tasks | ||
- [ ] -- Decision to support remote references | ||
- [ ] -- Features -- dependencies (implications: remove default-required properties in Core.JsonEditor): https://spacetelescope.github.io/understanding-json-schema/reference/object.html#index-4 | ||
- [ ] iterate schema: resolve $ref and definitions | ||
@@ -23,2 +20,3 @@ - [ ] profile performance | ||
**Milestone** add remaining draft04 features | ||
- [✓] -- remote references | ||
- [✓] -- default format validations | ||
@@ -37,3 +35,3 @@ - [✓] -- definitions | ||
### unsupported optional draft04 features | ||
- [ ] -- bignum (optional) | ||
- [x] -- bignum (optional) | ||
@@ -40,0 +38,0 @@ |
@@ -29,2 +29,5 @@ const path = require("path"); | ||
const result = core.resolveRef({ $ref: "#/definitions/def", title: "a definition" }); | ||
// remove internatls (remoteRef precompilation) | ||
delete result.__compiled; | ||
delete result.id; | ||
expect(result).to.deep.equal({ type: "string" }); | ||
@@ -31,0 +34,0 @@ }); |
@@ -29,3 +29,3 @@ const path = require("path"); | ||
const result = core.resolveRef({ $ref: "#/definitions/def", title: "a definition" }); | ||
expect(result).to.deep.equal({ type: "string", title: "a definition" }); | ||
expect(result).to.deep.include({ type: "string", title: "a definition" }); | ||
}); | ||
@@ -32,0 +32,0 @@ }); |
const Draft04 = require("../../lib/cores/Draft04"); | ||
require("./testDraft04")(Draft04, [ | ||
// ignore theese tests | ||
"changed scope ref invalid", // not going to be supported (combination of id, folder (folder!!!), refs) | ||
"a float is not an integer even without fractional part", // will always fail within javascript | ||
// "ref valid, maxItems ignored", | ||
// TestCases: not required but complex logic | ||
"base URI change", | ||
"base URI change - change folder in subschema", | ||
"root ref in remote ref", | ||
"Recursive references between schemas", | ||
"base URI change - change folder" // weird stuff, totally inpractical | ||
"a float is not an integer even without fractional part" // will always fail within javascript | ||
]); |
const JsonEditor = require("../../lib/cores/JsonEditor"); | ||
require("./testDraft04")(JsonEditor, [ | ||
// ignore theese tests | ||
"changed scope ref invalid", // not going to be supported (combination of id, folder (folder!!!), refs) | ||
"a float is not an integer even without fractional part", // will always fail within javascript | ||
"ref valid, maxItems ignored", | ||
// TestCases: not required but complex logic | ||
"base URI change", | ||
"base URI change - change folder in subschema", | ||
"root ref in remote ref", | ||
"Recursive references between schemas", | ||
"base URI change - change folder" // weird stuff, totally inpractical | ||
"ref overrides any sibling keywords", // this is JsonEditor specific | ||
"a float is not an integer even without fractional part" // will always fail within javascript | ||
]); |
@@ -13,2 +13,3 @@ /* eslint max-len: 0 */ | ||
addSchema("http://localhost:1234/name.json", require("json-schema-test-suite/remotes/name.json")); | ||
addSchema("http://localhost:1234/folder/folderInteger.json", require("json-schema-test-suite/remotes/folder/folderInteger.json")); | ||
@@ -15,0 +16,0 @@ const globPattern = path.join(__dirname, "..", "..", "node_modules", "json-schema-test-suite", "tests", "draft4", "**", "*.json"); |
@@ -203,2 +203,20 @@ /* eslint quote-props: 0, no-unused-expressions: 0 */ | ||
}); | ||
it("should iterate over nested definitions", () => { | ||
const calls = []; | ||
const rootSchema = { | ||
definitions: { | ||
anotherScope: { | ||
definitions: { | ||
bar: { type: "array" } | ||
} | ||
} | ||
} | ||
}; | ||
iterateSchema(rootSchema, (schema) => calls.push(schema)); | ||
expect(calls).to.have.length(3); | ||
expect(calls[1]).to.eq(rootSchema.definitions.anotherScope.definitions.target); | ||
}); | ||
}); |
@@ -56,12 +56,36 @@ /* eslint quote-props: 0 max-len: 0 */ | ||
it("should resolve draft04 json", () => { | ||
const schema = { | ||
$ref: "http://json-schema.org/draft-04/schema#" | ||
}; | ||
const result = resolveRef(schema, schema); | ||
describe("remotes", () => { | ||
const remotes = require("../../remotes"); | ||
// const precompile = require("../../lib/precompileSchema"); | ||
expect(result).to.have.property("$schema"); | ||
expect(result.$schema).to.eq("http://json-schema.org/draft-04/schema#"); | ||
afterEach(() => remotes.reset()); | ||
it("should resolve draft04 json", () => { | ||
const schema = { | ||
$ref: "http://json-schema.org/draft-04/schema#" | ||
}; | ||
const result = resolveRef(schema, schema); | ||
expect(result).to.have.property("$schema"); | ||
expect(result.$schema).to.eq("http://json-schema.org/draft-04/schema#"); | ||
}); | ||
// it.only("should correctly resolve remote", () => { | ||
// remotes["http://localhost:1234/name.json"] = require("json-schema-test-suite/remotes/name.json"); | ||
// const schema = precompile(null, { | ||
// id: "http://localhost:1234/object", | ||
// type: "object", | ||
// properties: { | ||
// name: { $ref: "name.json#/definitions/orNull" } | ||
// } | ||
// }); | ||
// console.log(schema); | ||
// // "http://localhost:1234/name.json#/definitions/orNull" | ||
// const result = resolveRef(schema.properties.name, schema); | ||
// expect(result).to.deep.eq({ anyOf: [{ type: "null" }, { $ref: "#" }] }); | ||
// }); | ||
}); | ||
}); |
@@ -59,3 +59,3 @@ const expect = require("chai").expect; | ||
describe("anyOf", () => { | ||
describe.only("anyOf", () => { | ||
@@ -72,2 +72,22 @@ it("should validate if one schemas in anyOf validates", () => { | ||
}); | ||
it("should validate null", () => { | ||
const errors = validate(core, { anyOf: [{ type: "null" }] }, null); | ||
expect(errors).to.have.length(0); | ||
}); | ||
it("should return error if invalid null", () => { | ||
const errors = validate(core, { anyOf: [{ type: "null" }] }, 3); | ||
expect(errors).to.have.length(1); | ||
expect(errors[0].name).to.eq("AnyOfError"); | ||
}); | ||
it("should resolve references", () => { | ||
core.rootSchema = { | ||
definitions: { integer: { type: "integer" } } | ||
}; | ||
const errors = validate(core, { anyOf: [{ type: "null" }, { $ref: "#/definitions/integer" }] }, 3); | ||
expect(errors).to.have.length(0); | ||
}); | ||
}); | ||
@@ -74,0 +94,0 @@ }); |
749530
84
6811
268