Skip to content

Commit

Permalink
Add reason key
Browse files Browse the repository at this point in the history
  • Loading branch information
Atinux committed Nov 10, 2016
1 parent 6972426 commit f0ccc55
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
39 changes: 20 additions & 19 deletions lib/schema-inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
return;
}
if (typeof candidate === 'undefined') {
this.report('is missing and not optional');
this.report('is missing and not optional', null, 'optional');
}
},
type: function (schema, candidate) {
Expand All @@ -220,7 +220,7 @@
});
if (!typeIsValid) {
types = types.map(function (t) {return typeof t === 'function' ? 'and instance of ' + t.name : t; });
this.report('must be ' + types.join(' or ') + ', but is ' + _realType(candidate));
this.report('must be ' + types.join(' or ') + ', but is ' + _realType(candidate), null, 'type');
}
},
uniqueness: function (schema, candidate) {
Expand All @@ -236,7 +236,7 @@
var indexes = getIndexes(candidate, candidate[i]);
if (indexes.length > 1) {
reported.push(candidate[i]);
this.report('has value [' + candidate[i] + '] more than once at indexes [' + indexes.join(', ') + ']');
this.report('has value [' + candidate[i] + '] more than once at indexes [' + indexes.join(', ') + ']', null, 'uniqueness');
}
}
},
Expand All @@ -261,12 +261,12 @@
}
});
if (!matches) {
self.report('must match [' + regexs.join(' or ') + '], but is equal to "' + candidate + '"');
self.report('must match [' + regexs.join(' or ') + '], but is equal to "' + candidate + '"', null, 'pattern');
}
},
validDate: function (schema, candidate) {
if (String(schema.validDate) === 'true' && candidate instanceof Date && isNaN(candidate.getTime())) {
this.report('must be a valid date');
this.report('must be a valid date', null, 'validDate');
}
},
minLength: function (schema, candidate) {
Expand All @@ -278,7 +278,7 @@
return;
}
if (candidate.length < minLength) {
this.report('must be longer than ' + minLength + ' elements, but it has ' + candidate.length);
this.report('must be longer than ' + minLength + ' elements, but it has ' + candidate.length, null, 'minLength');
}
},
maxLength: function (schema, candidate) {
Expand All @@ -290,7 +290,7 @@
return;
}
if (candidate.length > maxLength) {
this.report('must be shorter than ' + maxLength + ' elements, but it has ' + candidate.length);
this.report('must be shorter than ' + maxLength + ' elements, but it has ' + candidate.length, null, 'maxLength');
}
},
exactLength: function (schema, candidate) {
Expand All @@ -302,7 +302,7 @@
return;
}
if (candidate.length !== exactLength) {
this.report('must have exactly ' + exactLength + ' elements, but it have ' + candidate.length);
this.report('must have exactly ' + exactLength + ' elements, but it have ' + candidate.length, null, 'exactLength');
}
},
lt: function (schema, candidate) {
Expand All @@ -311,7 +311,7 @@
return;
}
if (candidate >= limit) {
this.report('must be less than ' + limit + ', but is equal to "' + candidate + '"');
this.report('must be less than ' + limit + ', but is equal to "' + candidate + '"', null, 'lt');
}
},
lte: function (schema, candidate) {
Expand All @@ -320,7 +320,7 @@
return;
}
if (candidate > limit) {
this.report('must be less than or equal to ' + limit + ', but is equal to "' + candidate + '"');
this.report('must be less than or equal to ' + limit + ', but is equal to "' + candidate + '"', null, 'lte');
}
},
gt: function (schema, candidate) {
Expand All @@ -329,7 +329,7 @@
return;
}
if (candidate <= limit) {
this.report('must be greater than ' + limit + ', but is equal to "' + candidate + '"');
this.report('must be greater than ' + limit + ', but is equal to "' + candidate + '"', null, 'gt');
}
},
gte: function (schema, candidate) {
Expand All @@ -338,7 +338,7 @@
return;
}
if (candidate < limit) {
this.report('must be greater than or equal to ' + limit + ', but is equal to "' + candidate + '"');
this.report('must be greater than or equal to ' + limit + ', but is equal to "' + candidate + '"', null, 'gte');
}
},
eq: function (schema, candidate) {
Expand All @@ -357,11 +357,11 @@
}
this.report('must be equal to [' + limit.map(function (l) {
return '"' + l + '"';
}).join(' or ') + '], but is equal to "' + candidate + '"');
}).join(' or ') + '], but is equal to "' + candidate + '"', null, 'eq');
}
else {
if (candidate !== limit) {
this.report('must be equal to "' + limit + '", but is equal to "' + candidate + '"');
this.report('must be equal to "' + limit + '", but is equal to "' + candidate + '"', null, 'eq');
}
}
},
Expand All @@ -376,14 +376,14 @@
if (_typeIs.array(limit)) {
for (var i = 0; i < limit.length; i++) {
if (candidate === limit[i]) {
this.report('must not be equal to "' + limit[i] + '"');
this.report('must not be equal to "' + limit[i] + '"', null, 'ne');
return;
}
}
}
else {
if (candidate === limit) {
this.report('must not be equal to "' + limit + '"');
this.report('must not be equal to "' + limit + '"', null, 'ne');
}
}
},
Expand All @@ -398,7 +398,7 @@
if (!valid) {
this.report('must have at least key ' + _keys.map(function (i) {
return '"' + i + '"';
}).join(' or '));
}).join(' or '), null, 'someKeys');
}
},
strict: function (schema, candidate) {
Expand All @@ -414,7 +414,7 @@
if (intruder.length > 0) {
var msg = 'should not contains ' + (intruder.length > 1 ? 'properties' : 'property') +
' [' + intruder.map(function (i) { return '"' + i + '"'; }).join(', ') + ']';
self.report(msg);
self.report(msg, null, 'strict');
}
}
},
Expand Down Expand Up @@ -581,9 +581,10 @@
this._customFields = Object.keys(this._custom);
this.origin = null;

this.report = function (message, code) {
this.report = function (message, code, reason) {
var newErr = {
code: code || this.userCode || null,
reason: reason || 'unkown',
message: this.userError || message || 'is invalid',
property: this.userAlias ? (this.userAlias + ' (' + this._dumpStack() + ')') : this._dumpStack()
};
Expand Down
36 changes: 18 additions & 18 deletions misc/node-test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
var inspector = require('../');

// var schema = {
// type: 'object',
// properties: {
// lorem: { type: 'string', eq: 'ipsum' },
// dolor: {
// type: 'array',
// items: { type: 'number' }
// }
// }
// };
var schema = {
type: 'object',
properties: {
lorem: { type: 'string', eq: 'ipsum' },
dolor: {
type: 'array',
items: { type: 'number' }
}
}
};

// var candidate = {
// lorem: 'not_ipsum',
// dolor: [ 12, 34, 'ERROR', 45, 'INVALID' ]
// };
// var result = inspector.validate(schema, candidate, function (err, result) {
// console.log(result.format());
// });
var candidate = {
lorem: 'not_ipsum',
dolor: [ 12, 34, 'ERROR', 45, 'INVALID' ]
};
var result = inspector.validate(schema, candidate, function (err, result) {
console.log(result);
});

// var schema = { type: 'object', properties: { lol: { someKeys: ['pouet'] } } };
// var subject = { lol: null };
// inspector.validate(schema, subject);
// inspector.validate(schema, subject);
26 changes: 26 additions & 0 deletions misc/vd_test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
if (typeof require === 'function') {
var SchemaInspector = require('../');
}

SchemaInspector.Validation.extend({
unique: function (schema, candidate, callback) {
console.log(this.origin._connection);
callback();
}
});

var schema = {
type: 'array',
items: {
type: 'number', $unique: true
}
};

var obj = [ 0, 5, 10, 15, 17, 20];

// -----------------------------------------------------------------------------
obj._connection = 'test';
SchemaInspector.validate(schema, obj, function (err, r) {
console.log(r);
console.log(r.format());
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "schema-inspector",
"description": "Schema-Inspector is a powerful tool to sanitize and validate JS objects.",
"version": "1.6.6",
"version": "1.6.7",
"main": "index.js",
"author": {
"name": "Sebastien Chopin",
Expand Down

0 comments on commit f0ccc55

Please sign in to comment.