another-json-schema
Advanced tools
Comparing version 3.4.0 to 3.5.0
@@ -0,1 +1,5 @@ | ||
## 3.5.0/2018-04-11 | ||
- add `_customErrorMsg` | ||
## 3.4.0/2018-01-09 | ||
@@ -2,0 +6,0 @@ |
@@ -50,3 +50,3 @@ const toString = Object.prototype.toString | ||
exports.default = function (actual, expected, key, parent) { | ||
parent[key] = actual || expected | ||
parent[key] = actual != null ? actual : expected | ||
return true | ||
@@ -53,0 +53,0 @@ } |
12
index.js
@@ -216,3 +216,3 @@ const helpersFuncs = require('./helpers') | ||
for (let helper in ctx._children) { | ||
if (['type', 'default', 'required'].indexOf(helper) !== -1 || (opts[helper] != null && !opts[helper])) { | ||
if (['type', 'default', 'required', '_customErrorMsg'].indexOf(helper) !== -1 || (opts[helper] != null && !opts[helper])) { | ||
continue | ||
@@ -242,2 +242,3 @@ } | ||
let error | ||
const _customErrorMsg = ctx._schema._customErrorMsg || {} | ||
if (!type) { | ||
@@ -247,16 +248,17 @@ if (helper) { | ||
if (typeof helperEntry === 'function') { | ||
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry.name + ')') | ||
error = new Error('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry.name + ')') | ||
} else { | ||
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry + ')') | ||
error = new Error('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + helper + ': ' + helperEntry + ')') | ||
} | ||
error.validator = helper | ||
} else { | ||
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + JSON.stringify(ctx._children) + ')') | ||
error = new Error('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (' + JSON.stringify(ctx._children) + ')') | ||
error.validator = 'type' | ||
} | ||
} else { | ||
error = new TypeError('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (type: ' + type + ')') | ||
error = new Error('(' + ctx._path + ': ' + JSON.stringify(value) + ') ✖ (type: ' + type + ')') | ||
error.validator = 'type' | ||
} | ||
error.message = _customErrorMsg[helper] || error.message | ||
error.path = ctx._path | ||
@@ -263,0 +265,0 @@ error.actual = value |
{ | ||
"name": "another-json-schema", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"description": "Another JSON Schema, simple & flexible & intuitive.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,3 +35,3 @@ ### another-json-schema | ||
error: | ||
{ TypeError: ($.name: undefined) ✖ (required: true) | ||
{ Error: ($.name: undefined) ✖ (required: true) | ||
validator: 'required', | ||
@@ -61,3 +61,3 @@ actual: undefined, | ||
error: | ||
{ TypeError: ($.gender: "lalala") ✖ (enum: male,female) | ||
{ Error: ($.gender: "lalala") ✖ (enum: male,female) | ||
validator: 'enum', | ||
@@ -76,3 +76,3 @@ actual: 'lalala', | ||
error: | ||
{ TypeError: ($.age: 17) ✖ (gte: 18) | ||
{ Error: ($.age: 17) ✖ (gte: 18) | ||
validator: 'gte', | ||
@@ -156,2 +156,37 @@ actual: 17, | ||
### Custom Error Message | ||
```js | ||
const AJS = require('another-json-schema') | ||
const userSchema = AJS('userSchema', { | ||
name: { type: 'string', required: true }, | ||
age: { | ||
type: 'number', | ||
gte: 18, | ||
_customErrorMsg: { | ||
gte: '您未满 18 岁' | ||
} | ||
} | ||
}) | ||
// test `_customErrorMsg` | ||
console.log(userSchema.validate({ | ||
name: 'nswbmw', | ||
age: 17 | ||
})) | ||
/* | ||
{ valid: false, | ||
error: | ||
{ Error: 您未满 18 岁 | ||
validator: 'gte', | ||
path: '$.age', | ||
actual: 17, | ||
expected: { type: 'number', gte: 18, _customErrorMsg: [Object] }, | ||
schema: 'userSchema' }, | ||
result: { name: 'nswbmw', age: 17 } } | ||
*/ | ||
``` | ||
### Register validator | ||
@@ -173,3 +208,3 @@ | ||
error: | ||
{ TypeError: ($: 17) ✖ (adult: true) | ||
{ Error: ($: 17) ✖ (adult: true) | ||
validator: 'adult', | ||
@@ -224,3 +259,3 @@ actual: 17, | ||
error: | ||
{ TypeError: ($.commentIds[]: "lalala") ✖ (type: ObjectId) | ||
{ Error: ($.commentIds[]: "lalala") ✖ (type: ObjectId) | ||
validator: 'type', | ||
@@ -252,3 +287,3 @@ path: '$.commentIds[]', | ||
error: | ||
{ TypeError: ($._id: 0) ✖ (range: 1,100) | ||
{ Error: ($._id: 0) ✖ (range: 1,100) | ||
validator: 'range', | ||
@@ -255,0 +290,0 @@ actual: 0, |
@@ -14,2 +14,20 @@ const AJS = require('..') | ||
it('_customErrorMsg', function () { | ||
const userSchema = AJS('userSchema', { | ||
name: { type: 'string', required: true }, | ||
age: { | ||
type: 'number', | ||
gte: 18, | ||
_customErrorMsg: { | ||
gte: '您未满 18 岁' | ||
} | ||
} | ||
}) | ||
assert.deepEqual(userSchema.validate({ | ||
name: 'nswbmw', | ||
age: 17 | ||
}).error.message, '您未满 18 岁') | ||
}) | ||
it('additionalProperties', function () { | ||
@@ -16,0 +34,0 @@ var userSchema = AJS('userSchema', { |
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
45742
1161
392