json-schema-library
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -47,2 +47,3 @@ /* eslint quote-props: 0 */ | ||
"number": (core, schema, data) => getDefault(schema, data, 0), | ||
"integer": (core, schema, data) => getDefault(schema, data, 0), | ||
"boolean": (core, schema, data) => getDefault(schema, data, false), | ||
@@ -65,3 +66,5 @@ "object": (core, schema, data) => { | ||
schema.minItems = schema.minItems || 0; | ||
if (schema.items.oneOf && data.length === 0) { | ||
if (schema.items == null) { | ||
return data; | ||
} else if (schema.items.oneOf && data.length === 0) { | ||
for (let i = 0; i < schema.minItems; i += 1) { | ||
@@ -99,2 +102,4 @@ data[i] = core.getTemplate(schema.items.oneOf[0], data[i] || template[i]); | ||
return templateValue; | ||
} else if (schema.default === undefined && Array.isArray(schema.enum)) { | ||
return schema.enum[0]; | ||
} else if (schema.default === undefined) { | ||
@@ -101,0 +106,0 @@ return initValue; |
@@ -42,3 +42,8 @@ const getTypeOf = require("./getTypeOf"); | ||
// an additional `oneOfProperty` on the schema will exactly determine the oneOf value (if set in data) | ||
if (schema.oneOfProperty) { | ||
// @fixme | ||
// abort if no data is given an oneOfProperty is set (used by getChildSchemaSelection) | ||
// this case (data != null) should not be necessary | ||
if (data != null && schema.oneOfProperty) { | ||
const oneOfProperty = schema.oneOfProperty; | ||
@@ -45,0 +50,0 @@ const oneOfValue = data[schema.oneOfProperty]; |
@@ -7,2 +7,7 @@ const filter = require("./utils/filter"); | ||
return function notifyError(error) { | ||
if (Array.isArray(error)) { | ||
error = flattenArray(error); | ||
error.forEach(notifyError); | ||
return error; | ||
} | ||
if (filter.isError(error)) { | ||
@@ -27,5 +32,6 @@ onError(error); | ||
module.exports = function validateAsync(core, schema, value, pointer = "#", onError) { | ||
const errors = core.validate(schema, value, pointer); | ||
let errors = core.validate(schema, value, pointer); | ||
if (onError) { | ||
errors = flattenArray(errors); | ||
const notifyError = createErrorNotification(onError); | ||
@@ -32,0 +38,0 @@ for (let i = 0; i < errors.length; i += 1) { |
@@ -20,3 +20,3 @@ const getTypeOf = require("../getTypeOf"); | ||
const errors = []; | ||
const receivedProperties = Object.keys(value); | ||
const receivedProperties = Object.keys(value).filter((prop) => prop !== "_id"); | ||
const expectedProperties = Object.keys(schema.properties || {}); | ||
@@ -23,0 +23,0 @@ |
{ | ||
"name": "json-schema-library", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Customizable and hackable json-validator and json-schema utilities for traversal, data generation and validation", | ||
"main": "lib/index.js", | ||
"main": "index.js", | ||
"scripts": { | ||
@@ -7,0 +7,0 @@ "test": "npm run test-unit; npm run test-integration", |
@@ -5,2 +5,3 @@ # Tasks | ||
- [ ] -- 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 | ||
@@ -18,3 +19,2 @@ - [ ] profile performance | ||
**Milestone** add remaining draft04 features | ||
- [ ] -- Features -- dependencies (implications: remove default-required properties in Core.JsonEditor): https://spacetelescope.github.io/understanding-json-schema/reference/object.html#index-4 | ||
- [ ] -- Features -- allOf | ||
@@ -21,0 +21,0 @@ - [ ] -- Features -- anyOf |
@@ -102,2 +102,13 @@ const expect = require("chai").expect; | ||
}); | ||
it("should return an error if schema could not be resolved", () => { | ||
core.rootSchema = { | ||
type: "object", | ||
properties: { coffee: { type: "string" } }, | ||
patternProperties: { "^tee$": { type: "string" } }, | ||
additionalProperties: false | ||
}; | ||
const schema = getSchema(core, core.rootSchema, undefined, "#/beer"); | ||
expect(schema.name).to.equal("UnknownPropertyError"); | ||
}); | ||
}); | ||
@@ -104,0 +115,0 @@ |
@@ -13,2 +13,16 @@ /* eslint quote-props: 0 */ | ||
it("should set an empty string if no default value is given", () => { | ||
core.rootSchema = { type: "string" }; | ||
const res = getTemplate(core, core.rootSchema); | ||
expect(res).to.deep.equal(""); | ||
}); | ||
it("should set the first enum option for a missing default", () => { | ||
core.rootSchema = { type: "string", enum: ["first", "second"] }; | ||
const res = getTemplate(core, core.rootSchema); | ||
expect(res).to.deep.equal("first"); | ||
}); | ||
describe("object", () => { | ||
@@ -68,3 +82,2 @@ | ||
describe("$ref", () => { | ||
@@ -71,0 +84,0 @@ |
509924
67
5340