schema-inspector
Advanced tools
Comparing version 1.3.9 to 1.4.0
@@ -567,8 +567,9 @@ /* | ||
this.report = function (message) { | ||
this.report = function (message, code) { | ||
var newErr = { | ||
message: this.userError || message || 'is invalid', | ||
property: this.userAlias | ||
? (this.userAlias + ' (' + this._dumpStack() + ')') | ||
: this._dumpStack() | ||
code: code || this.userCode || null, | ||
message: this.userError || message || 'is invalid', | ||
property: this.userAlias | ||
? (this.userAlias + ' (' + this._dumpStack() + ')') | ||
: this._dumpStack() | ||
}; | ||
@@ -614,2 +615,3 @@ _error.push(newErr); | ||
Validation.prototype._validate = function (schema, candidate, callback) { | ||
this.userCode = schema.code || null; | ||
this.userError = schema.error || null; | ||
@@ -632,2 +634,3 @@ this.userAlias = schema.alias || null; | ||
var self = this; | ||
this.userCode = schema.code || null; | ||
this.userError = schema.error || null; | ||
@@ -634,0 +637,0 @@ this.userAlias = schema.alias || null; |
{ | ||
"name": "schema-inspector", | ||
"description": "Schema-Inspector is a powerful tool to sanitize and validate JS objects.", | ||
"version": "1.3.9", | ||
"version": "1.4.0", | ||
"main": "./lib/schema-inspector.js", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -145,2 +145,3 @@ [](http://atinux.github.io/schema-inspector/) | ||
* [error](#v_error) | ||
* [code](#v_code) | ||
@@ -490,3 +491,3 @@ ### Sanitization | ||
Custom checker =). "exec" functions take two three parameter | ||
(schema, post [, callback]). To report an error, use `this.report([message])`. | ||
(schema, post [, callback]). To report an error, use `this.report([message], [code])`. | ||
Very useful to make some custom validation. | ||
@@ -694,2 +695,39 @@ | ||
--------------------------------------- | ||
<a name="v_code" /> | ||
### code | ||
* **type**: string. | ||
* **usable on**: any. | ||
This field contains a user code for displaying a more uniform system to personnalize error message. | ||
__Example__ | ||
```javascript | ||
var inspector = require('schema-inspector'); | ||
var schema1 = { | ||
type: 'object', | ||
properties: { | ||
_id: { type: 'string' } | ||
} | ||
}; | ||
var schema2 = { | ||
type: 'object', | ||
properties: { | ||
_id: { type: 'string', code: 'id-format' } | ||
} | ||
}; | ||
var c1 = { _id: 1234567890 }; | ||
var r1 = inspector.validate(schema1, c1); | ||
var r2 = inspector.validate(schema2, c1); | ||
console.log(r1.error[0].code); // null | ||
console.log(r2.error[1].code); // 'id-format' | ||
``` | ||
## Sanitization | ||
@@ -696,0 +734,0 @@ |
@@ -2035,2 +2035,57 @@ var should = require('should'); | ||
}); // suite "schema #20.2" | ||
suite('schema #21 (field "code" testing)', function () { | ||
var schema = { | ||
type: 'object', | ||
properties: { | ||
id: { | ||
type: 'integer', | ||
gte: 10, | ||
lte: 20, | ||
code: 'id-format', | ||
exec: function (schema, post) { | ||
if (post === 15) | ||
this.report('Test error in report', 'test-code'); | ||
} | ||
}, | ||
array: { | ||
optional: true, | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
code: 'array-item-format' | ||
} | ||
} | ||
} | ||
}; | ||
test('candidate #1', function () { | ||
var candidate = { | ||
id: 25 | ||
}; | ||
var result = si.validate(schema, candidate); | ||
result.should.be.an.Object; | ||
result.should.have.property('valid').with.equal(false); | ||
result.should.have.property('error').with.be.an.instanceof(Array) | ||
.and.be.lengthOf(1); | ||
result.error[0].code.should.equal('id-format'); | ||
}); | ||
test('candidate #2', function () { | ||
var candidate = { | ||
id: 15, | ||
array: ['NikitaJS', 'Atinux', 1234] | ||
}; | ||
var result = si.validate(schema, candidate); | ||
result.should.be.an.Object; | ||
result.should.have.property('valid').with.equal(false); | ||
result.should.have.property('error').with.be.an.instanceof(Array) | ||
.and.be.lengthOf(2); | ||
result.error[0].code.should.equal('test-code'); | ||
result.error[1].code.should.equal('array-item-format'); | ||
}); | ||
}); // suite "schema #14" | ||
}; |
296111
5254
1258