@peculiar/json-schema
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -326,2 +326,5 @@ 'use strict'; | ||
const namedSchema = this.getSchemaByName(schema, schemaName); | ||
if (options.strictProperty && !Array.isArray(target)) { | ||
JsonParser.checkStrictProperty(target, namedSchema, targetSchema); | ||
} | ||
for (const key in namedSchema) { | ||
@@ -354,13 +357,12 @@ try { | ||
else { | ||
const newOptions = { | ||
...options, | ||
targetSchema: item.type, | ||
schemaName, | ||
}; | ||
if (item.repeated) { | ||
obj[key] = value.map((el) => this.fromJSON(el, { | ||
targetSchema: item.type, | ||
schemaName, | ||
})); | ||
obj[key] = value.map((el) => this.fromJSON(el, newOptions)); | ||
} | ||
else { | ||
obj[key] = this.fromJSON(value, { | ||
targetSchema: item.type, | ||
schemaName, | ||
}); | ||
obj[key] = this.fromJSON(value, newOptions); | ||
} | ||
@@ -380,2 +382,11 @@ } | ||
} | ||
static checkStrictProperty(target, namedSchema, targetSchema) { | ||
const jsonProps = Object.keys(target); | ||
const schemaProps = Object.keys(namedSchema); | ||
for (const key of jsonProps) { | ||
if (schemaProps.indexOf(key) === -1) { | ||
throw new ParserError(targetSchema.name, `Incoming JSON has odd property '${key}'`); | ||
} | ||
} | ||
} | ||
} | ||
@@ -382,0 +393,0 @@ |
declare namespace JsonSchema { | ||
interface IEmptyConstructor<T> { | ||
new(): T; | ||
} | ||
type IEmptyConstructor<T> = new() => T; | ||
@@ -96,2 +94,6 @@ interface IJsonConverter<T, S> { | ||
schemaName?: string; | ||
/** | ||
* Enable strict checking of properties. Throw exception if incoming JSON has odd fields | ||
*/ | ||
strictProperty?: boolean; | ||
} | ||
@@ -98,0 +100,0 @@ |
{ | ||
"name": "@peculiar/json-schema", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "This package uses ES2015 decorators to simplify JSON schema creation and use", | ||
@@ -51,5 +51,5 @@ "main": "build/index.js", | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.10.1", | ||
"@types/node": "^10.12.18", | ||
"mocha": "^5.2.0", | ||
"rollup": "^0.66.2", | ||
"rollup": "^0.66.6", | ||
"rollup-plugin-cleanup": "^3.0.0", | ||
@@ -59,3 +59,3 @@ "rollup-plugin-node-builtins": "^2.1.2", | ||
"ts-node": "^7.0.1", | ||
"typescript": "^3.0.3" | ||
"typescript": "^3.2.2" | ||
}, | ||
@@ -62,0 +62,0 @@ "dependencies": { |
26373
524