Comparing version 3.16.1 to 3.17.0
{ | ||
"name": "z-schema", | ||
"version": "3.16.1", | ||
"version": "3.17.0", | ||
"description": "JSON schema validator", | ||
@@ -61,4 +61,4 @@ "homepage": "https://github.com/zaggino/z-schema", | ||
"dependencies": { | ||
"lodash.get": "^3.7.0", | ||
"validator": "^4.0.5" | ||
"lodash.get": "^4.1.2", | ||
"validator": "^5.0.0" | ||
}, | ||
@@ -71,3 +71,3 @@ "optionalDependencies": { | ||
"coveralls": "latest", | ||
"grunt": "latest", | ||
"grunt": "^0.4.5", | ||
"grunt-cli": "latest", | ||
@@ -74,0 +74,0 @@ "grunt-browserify": "~3.2.1", |
@@ -27,3 +27,3 @@ [![npm version](https://badge.fury.io/js/z-schema.svg)](http://badge.fury.io/js/z-schema) | ||
These repository has several submodules and should be cloned as follows: | ||
>git clone **--recursive** https://github.com/APIs-guru/z-schema.git | ||
>git clone **--recursive** https://github.com/zaggino/z-schema.git | ||
@@ -205,3 +205,3 @@ ##CLI: | ||
You can register any format of your own, sync validator function should always respond with boolean: | ||
You can register any format of your own. Your sync validator function should always respond with a boolean: | ||
@@ -479,2 +479,89 @@ ```javascript | ||
##customValidator | ||
**Warning**: Use only if know what you are doing. Always consider using [custom format](#register-a-custom-format) before using this option. | ||
Register function to be called as part of validation process on every subshema encounter during validation. | ||
Let's make a real-life example with this feature. | ||
Imagine you have number of transactions: | ||
```json | ||
{ | ||
"fromId": 1034834329, | ||
"toId": 1034834543, | ||
"amount": 200 | ||
} | ||
``` | ||
So you write the schema: | ||
```json | ||
{ | ||
"type": "object", | ||
"properties": { | ||
"fromId": { | ||
"type": "integer" | ||
}, | ||
"toId": { | ||
"type": "integer" | ||
}, | ||
"amount": { | ||
"type": "number" | ||
} | ||
} | ||
} | ||
``` | ||
But how to check that `fromId` and `toId` are never equal. | ||
In JSON Schema Draft4 there is no possibility to do this. | ||
Actually, it's easy to just write validation code for such simple payloads. | ||
But what if you have to do the same check for many objects in different places of JSON payload. | ||
One solution is to add custom keyword `uniqueProperties` with array of property names as a value. So in our schema we would need to add: | ||
```json | ||
"uniqueProperties": [ | ||
"fromId", | ||
"toId" | ||
] | ||
``` | ||
To teach `z-schema` about this new keyword we need to write handler for it: | ||
```js | ||
function customValidatorFn(report, schema, json) { | ||
// check if our custom property is present | ||
if (Array.isArray(schema.uniqueProperties)) { | ||
var seenValues = []; | ||
schema.uniqueProperties.forEach(function (prop) { | ||
var value = json[prop]; | ||
if (typeof value !== 'undefined') { | ||
if (seenValues.indexOf(value) !== -1) { | ||
// report error back to z-schema core | ||
report.addCustomError("NON_UNIQUE_PROPERTY_VALUE", | ||
"Property \"{0}\" has non-unique value: {1}", | ||
[prop, value], null, schema.description); | ||
} | ||
seenValues.push(value) | ||
} | ||
}); | ||
} | ||
} | ||
var validator = new ZSchema({ | ||
// register our custom validator inside z-schema | ||
customValidator: customValidatorFn | ||
}); | ||
``` | ||
Let's test it: | ||
```js | ||
var data = { | ||
fromId: 1034834346, | ||
toId: 1034834346, | ||
amount: 50 | ||
}; | ||
validator.validate(data, schema); | ||
console.log(validator.getLastErrors()) | ||
//[ { code: 'NON_UNIQUE_PROPERTY_VALUE', | ||
// params: [ 'toId', 1034834346 ], | ||
// message: 'Property "toId" has non-unique value: 1034834346', | ||
// path: '#/', | ||
// schemaId: undefined } ] | ||
``` | ||
**Note:** before creating your own keywords you should consider all compatibility issues. | ||
#Benchmarks | ||
@@ -481,0 +568,0 @@ |
@@ -529,2 +529,6 @@ "use strict"; | ||
if (typeof this.options.customValidator === "function") { | ||
this.options.customValidator(report, schema, json); | ||
} | ||
// we don't need the root pointer anymore | ||
@@ -531,0 +535,0 @@ if (isRoot) { |
@@ -80,3 +80,3 @@ "use strict"; | ||
Report.prototype.getPath = function () { | ||
Report.prototype.getPath = function (returnPathAsString) { | ||
var path = []; | ||
@@ -88,3 +88,3 @@ if (this.parentReport) { | ||
if (this.options.reportPathAsArray !== true) { | ||
if (returnPathAsString !== true) { | ||
// Sanitize the path segments (http://tools.ietf.org/html/rfc6901#section-4) | ||
@@ -150,2 +150,8 @@ path = "#/" + path.map(function (segment) { | ||
Report.prototype.addError = function (errorCode, params, subReports, schemaDescription) { | ||
if (!errorCode) { throw new Error("No errorCode passed into addError()"); } | ||
this.addCustomError(errorCode, Errors[errorCode], params, subReports, schemaDescription); | ||
}; | ||
Report.prototype.addCustomError = function (errorCode, errorMessage, params, subReports, schemaDescription) { | ||
if (this.errors.length >= this.reportOptions.maxErrors) { | ||
@@ -155,9 +161,7 @@ return; | ||
if (!errorCode) { throw new Error("No errorCode passed into addError()"); } | ||
if (!Errors[errorCode]) { throw new Error("No errorMessage known for code " + errorCode); } | ||
if (!errorMessage) { throw new Error("No errorMessage known for code " + errorCode); } | ||
params = params || []; | ||
var idx = params.length, | ||
errorMessage = Errors[errorCode]; | ||
var idx = params.length; | ||
while (idx--) { | ||
@@ -173,3 +177,3 @@ var whatIs = Utils.whatIs(params[idx]); | ||
message: errorMessage, | ||
path: this.getPath(), | ||
path: this.getPath(this.options.reportPathAsArray), | ||
schemaId: this.getSchemaId() | ||
@@ -176,0 +180,0 @@ }; |
@@ -58,3 +58,5 @@ "use strict"; | ||
// ignore unknown formats (do not report them as an error) | ||
ignoreUnknownFormats: false | ||
ignoreUnknownFormats: false, | ||
// function to be called on every schema | ||
customValidator: null | ||
}; | ||
@@ -61,0 +63,0 @@ |
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 too big to display
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
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
2793234
75402
580
2
+ Addedlodash.get@4.4.2(transitive)
+ Addedvalidator@5.7.0(transitive)
- Removeddepd@1.1.0(transitive)
- Removedlodash._baseget@3.7.2(transitive)
- Removedlodash._topath@3.8.1(transitive)
- Removedlodash.get@3.7.0(transitive)
- Removedlodash.isarray@3.0.4(transitive)
- Removedvalidator@4.9.0(transitive)
Updatedlodash.get@^4.1.2
Updatedvalidator@^5.0.0