schema-inspector
Advanced tools
Comparing version 1.5.9 to 1.6.0
@@ -136,3 +136,3 @@ /* | ||
"date": function (element) { | ||
return element != null && element.constructor === Date; | ||
return element != null && element instanceof Date; | ||
}, | ||
@@ -1068,2 +1068,3 @@ "object": function (element) { | ||
self._asyncSanitize(globing, post[i], function (err, res) { | ||
if (err) { /* Error can safely be ignored here */ } | ||
if (typeof res !== 'undefined') { | ||
@@ -1070,0 +1071,0 @@ post[i] = res; |
{ | ||
"name": "schema-inspector", | ||
"description": "Schema-Inspector is a powerful tool to sanitize and validate JS objects.", | ||
"version": "1.5.9", | ||
"version": "1.6.0", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -33,30 +33,59 @@ [![schema-inspector logo](https://raw.githubusercontent.com/Atinux/schema-inspector/master/misc/schema-inspector.png)](http://atinux.github.io/schema-inspector/) | ||
// Your object you want to validate (can be a JSON from your API) | ||
var candidate = { | ||
type: 'sms', | ||
to: [ 12, 'email@example.com', 'test'] | ||
// Data that we want to sanitize and validate | ||
var data = { | ||
firstname: 'sterling ', | ||
lastname: ' archer', | ||
jobs: 'Special agent, cocaine Dealer', | ||
email: 'NEVER!', | ||
}; | ||
// Your validation schema | ||
var schema = { | ||
// Sanitization Schema | ||
var sanitization = { | ||
type: 'object', | ||
properties: { | ||
type: { type: 'string', eq: 'email' }, | ||
to: { | ||
firstname: { type: 'string', rules: ['trim', 'title'] }, | ||
lastname: { type: 'string', rules: ['trim', 'title'] }, | ||
jobs: { | ||
type: 'array', | ||
items: { type: 'string', pattern: 'email' } | ||
} | ||
splitWith: ',', | ||
items: { type: 'string', rules: ['trim', 'title'] } | ||
}, | ||
email: { type: 'string', rules: ['trim', 'lower'] } | ||
} | ||
}; | ||
// Let's update the data | ||
inspector.sanitize(sanitization, data); | ||
/* | ||
data is now: | ||
{ | ||
firstname: 'Sterling', | ||
lastname: 'Archer', | ||
jobs: ['Special Agent', 'Cocaine Dealer'], | ||
email: 'never!' | ||
} | ||
*/ | ||
var result = inspector.validate(schema, candidate); // Candidate is not valid | ||
// Validation schema | ||
var validation = { | ||
type: 'object', | ||
properties: { | ||
firstname: { type: 'string', minLength: 1 }, | ||
lastname: { type: 'string', minLength: 1 }, | ||
jobs: { | ||
type: 'array', | ||
items: { type: 'string', minLength: 1 } | ||
}, | ||
email: { type: 'string', pattern: 'email' } | ||
} | ||
}; | ||
var result = inspector.validate(schema, candidate); | ||
if (!result.valid) | ||
console.log(result.format()); | ||
/* | ||
Property @.type: must be equal to "email", but is equal to "sms" | ||
Property @.to[0]: must be a string, but is number | ||
Property @.dolor[4]: must match [email], but is equal to "test" | ||
Property @.email: must match [email], but is equal to "never!" | ||
*/ | ||
``` | ||
**Tips:** it's recommended to use one schema for the sanitzation and another for the validation, | ||
## In the browser | ||
@@ -134,3 +163,3 @@ | ||
* `null` | ||
* `date` (constructor === Date) | ||
* `date` (instanceof Date) | ||
* `object` (constructor === Object) | ||
@@ -137,0 +166,0 @@ * `array` (constructor === Array) |
307743
5496
1287