Comparing version 0.1.0 to 0.1.1
// Load modules | ||
var NodeUtils = require('util'); | ||
var Errors = require('./errors'); | ||
var Utils = require('./utils'); | ||
@@ -23,34 +23,28 @@ var Types = require('./types'); | ||
exports.validate = function (object, config, options, next) { | ||
exports.validate = function (object, config, next) { | ||
if (options == null) { | ||
options = {}; | ||
} | ||
if (next == undefined && typeof options == 'function') { | ||
next = options; | ||
options = {}; | ||
} | ||
var self = this; | ||
var submitted = Utils.clone(object || {}); | ||
var errorMsg = null; | ||
var skipFunctions = this.settings.skipFunctions; | ||
var errors = new Errors(object); | ||
var placeholder = {}; | ||
placeholder.add = Types.Base.prototype.RequestErrorFactory(placeholder); | ||
placeholder._renamed = {}; | ||
var processConfig = function () { | ||
Object.keys(config || {}).forEach(function (key) { | ||
var unprocessedObject = Utils.clone(object || {}); | ||
var keys = Object.keys(config || {}); | ||
for (var i = 0, il = keys.length; i < il; ++ i) { | ||
var key = keys[i]; | ||
var keyConfig = config[key]; | ||
var value = object[key]; | ||
var value = object ? object[key] : object; | ||
delete unprocessedObject[key]; | ||
if (!validateKeyConfig(keyConfig, key, value)) { | ||
errorMsg = key + ' = ' + value; | ||
validateKeyConfig(keyConfig, key, value); | ||
} | ||
Object.keys(unprocessedObject).forEach(function (unprocessedKey) { | ||
if (!skipFunctions || typeof unprocessedObject[unprocessedKey] !== 'function') { | ||
errors.add('the key (' + unprocessedKey + ') is not allowed', unprocessedKey); | ||
} | ||
else { | ||
delete submitted[key]; | ||
} | ||
}); | ||
@@ -62,7 +56,13 @@ }; | ||
var converted = null; | ||
var errorFilter = function (error) { | ||
return error.path !== key; | ||
}; | ||
if (keyConfig instanceof Array) { | ||
for (var i = 0, il = keyConfig.length; i < il; ++i) { | ||
converted = convertType(keyConfig[i], key, value); | ||
converted = convertType(keyConfig[i], key, value); | ||
if (converted && keyConfig[i].validate(converted.value, key, object, placeholder)) { | ||
if (converted && keyConfig[i].validate(converted.value, key, object, errors, key)) { | ||
errors._values = errors._values.filter(errorFilter); | ||
return true; | ||
@@ -77,3 +77,3 @@ } | ||
return converted && keyConfig.validate(converted.value, key, object, placeholder); | ||
return converted && keyConfig.validate(converted.value, key, object, errors, key); | ||
}; | ||
@@ -103,44 +103,4 @@ | ||
processConfig(); | ||
delete placeholder._renamed; | ||
// Handle inputs that haven't been defined in config | ||
var processed = Object.keys(submitted); | ||
if (this.settings.skipFunctions) { | ||
for (var p in processed) { | ||
if (typeof submitted[processed[p]] === 'function') { | ||
delete submitted[processed[p]]; | ||
} | ||
} | ||
processed = Object.keys(submitted); | ||
} | ||
if (processed.length > 0) { | ||
var processedValues = processed.map(function (d) { | ||
var type = ' (\033[32m' + typeof submitted[d] + '\033[0m)'; | ||
var value = submitted[d].toString(); | ||
if (value.length > 20) { | ||
value = value.slice(0, 20) + '...'; | ||
} | ||
return ' \033[31m' + value + '\033[0m' + type; | ||
}); | ||
var plural = (processed.length > 1) ? 's' : ''; | ||
var verb = (processed.length > 1) ? 'are' : 'is'; | ||
errorMsg = 'the \033[32mkey' + plural + '\033[0m (\033[31m' + processed.join(', ') + '\033[0m) ' + verb + ' not allowed (\033[32mvalues\033[0m:' + processedValues + '\033[0m)'; | ||
} | ||
if (errorMsg) { | ||
if (placeholder.validationErrors && placeholder.validationErrors.length > 0) { | ||
return next(new Error(placeholder.validationErrors.join('.\n'))); | ||
} | ||
return next(new Error('Invalid parameter: ' + errorMsg)); | ||
} | ||
return next(); | ||
}; | ||
return next(errors.toError()); | ||
}; |
@@ -70,5 +70,10 @@ // Load modules | ||
return function (value, qs, key, errors) { | ||
return function (value, obj, key, errors, keyPath) { | ||
return value instanceof Array; | ||
var result = value instanceof Array; | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be an Array', keyPath); | ||
} | ||
return result; | ||
}; | ||
@@ -95,3 +100,2 @@ }; | ||
var self = this; | ||
var subElement = (new internals.ArrayType.super_()).getDataKey(); | ||
@@ -104,10 +108,6 @@ var args = Array.prototype.slice.call(arguments); | ||
return function (values, qs, key, req) { | ||
return function (values, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
// For each allowed Type | ||
var valueIsValid = true; | ||
// var intermediateInvalidValues = []; | ||
var invalidValues = []; | ||
@@ -121,18 +121,15 @@ var failedArg = null; | ||
for (var i = 0; i < allowedTypes.length; i++) { | ||
// For each validator specified | ||
// For each validator specified | ||
var currentType = args[i].type; | ||
var validators = allowedTypes[i]; | ||
var validatorIsValid = true; | ||
for (var j = 0; j < validators.length; j++) { | ||
// For each input supplied | ||
// For each input supplied | ||
for (m = 0; m < values.length; m++) { | ||
var value = values[m]; | ||
var result = validators[j](value, qs, key, req); | ||
if (result === false) { | ||
var result = validators[j](value, obj, key, errors); | ||
if (!result) { | ||
validatorIsValid = false; | ||
// intermediateInvalidValues.push(value); | ||
failedArg = i; | ||
@@ -143,3 +140,2 @@ failedIndex = j; | ||
else { | ||
// console.log(value, 'is valid', currentType) | ||
validatedValuesTable[m] = true; | ||
@@ -159,15 +155,11 @@ } | ||
// Only append error if the included types' validators return errors | ||
// if (valueIsValid === false && failedIndex >= 0 && args[0].__checks.indexOf('includes') < 0) { | ||
if (valueIsValid === false) { | ||
// if(self.options.debug === true) console.log('***', validatedValuesTable, values) | ||
if (!valueIsValid) { | ||
var validTypes = args.map(function (d) { return d.type; }); | ||
var vals = invalidValues.map(function (d) { return JSON.stringify(d) }).join(', ').replace(/[\']/g, '\'') | ||
var vals = invalidValues.map(function (d) { return JSON.stringify(d); }).join(', ').replace(/[\']/g, '\''); | ||
var msg = 'the value(s) `' + vals + '` in array `' + key + '` must be of the following type(s) ' + Sys.inspect(validTypes); | ||
req.add(msg); | ||
// console.log(msg) | ||
// console.log(args[failedIndex].type, args[failedArg].__checks[failedIndex], failedValue, values) | ||
errors.add(msg, keyPath); | ||
} | ||
return valueIsValid; | ||
} | ||
}; | ||
}; | ||
@@ -188,12 +180,5 @@ | ||
var args = Array.prototype.slice.call(arguments); | ||
var allowedTypes = args.map(function (d) { | ||
return d[subElement]; | ||
}); | ||
return function (value, obj, key, errors, keyPath) { | ||
return function (value, qs, key, req) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
var typeTable = { | ||
@@ -210,3 +195,3 @@ 'string': require('./string')(), | ||
delete typeTable[arg.type.toLowerCase()]; | ||
var result = self._includes(arg)(value, qs, key, req); | ||
var result = self._includes(arg)(value, obj, key, errors, keyPath); | ||
return result; | ||
@@ -217,3 +202,3 @@ }; | ||
if (components.indexOf(true) >= 0) { | ||
req.add('the values supplied to array `' + key + '` must contain ' + JSON.stringify(validTypes)); | ||
errors.add('the values supplied to array `' + key + '` must contain ' + JSON.stringify(validTypes), keyPath); | ||
return false; | ||
@@ -231,3 +216,4 @@ } | ||
req.add('the values supplied to array `' + key + '` must contain ' + JSON.stringify(validTypes)); | ||
errors.add('the values supplied to array `' + key + '` must contain ' + JSON.stringify(validTypes), keyPath); | ||
return false; | ||
@@ -243,48 +229,1 @@ }; | ||
}; | ||
/* | ||
internals.StringType.prototype._base = function () { | ||
return function (value, qs, key, req) { | ||
return typeof value === 'string'; | ||
// Deprecated code below should be moved to Array | ||
// var keyDoesNotExist = !qs.hasOwnProperty(key); | ||
// var valueIsMinLength = (qs[key] !== undefined && qs[key].length >= 1); | ||
// var valueIsNotNumber = isNaN(+value) || isNaN((new Date(+value)).getTime()); | ||
// // req = req || {}; | ||
// // req.add = req.add || function(){}; | ||
// // var valueIsNotArray = !(value instanceof Array); | ||
// var valueIsNotObject = true; | ||
// if (typeof value !== 'string') { | ||
// valueIsNotObject = typeof value !== 'object'; | ||
// } | ||
// else { | ||
// try { | ||
// var v = JSON.parse(value); | ||
// valueIsNotObject = (typeof v !== 'object' && !(v instanceof Array)) | ||
// } | ||
// catch (e) { | ||
// // console.log(e) | ||
// } | ||
// } | ||
// var valueIsDefined = (keyDoesNotExist || valueIsMinLength); | ||
// if (valueIsDefined === false) { | ||
// req.add('the value of `' + key + '` is not allowed to be the empty string (\'\')'); | ||
// } | ||
// var result = (valueIsDefined && | ||
// valueIsNotNumber && | ||
// valueIsNotObject); | ||
// return result; | ||
}; | ||
}; | ||
*/ |
@@ -178,3 +178,3 @@ // Load modules | ||
for (var i = unacceptable.length - 1; i >= 0; --i) { | ||
Utils.assert(this.validate(unacceptable[i]) == true, 'input to .invalid() must be valid ' + this.__name + '(' + unacceptable[i] + ')'); | ||
Utils.assert(this.validate(unacceptable[i]) === true, 'input to .invalid() must be valid ' + this.__name + '(' + unacceptable[i] + ')'); | ||
this.deny(unacceptable[i]); | ||
@@ -188,7 +188,7 @@ } | ||
return function (value, qstr, key) { | ||
return function (value, obj, key, errors, keyPath) { | ||
// TODO: make sure keys exists | ||
for (var i in peers) { | ||
if (!qstr.hasOwnProperty(peers[i]) || peers[i] === null) { | ||
if (!obj.hasOwnProperty(peers[i]) || peers[i] === null) { | ||
return false; | ||
@@ -212,5 +212,5 @@ } | ||
var self = this; | ||
return function (value, qstr) { | ||
return function (value, obj, key, errors, keyPath) { | ||
return !self._with(peers)(value, qstr); | ||
return !self._with(peers)(value, obj); | ||
} | ||
@@ -240,22 +240,22 @@ }; | ||
return function (value, qstr, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
var renamed = req._renamed || {}; | ||
errors = errors || {}; | ||
errors.add = errors.add || function () { }; | ||
var renamed = errors._renamed || {}; | ||
if (options.allowMult === false && to in renamed) { | ||
req.add('allowMult false and already renamed'); | ||
errors.add('allowMult false and already renamed', keyPath); | ||
return false; | ||
} | ||
if (options.allowOverwrite === false && qstr.hasOwnProperty(to)) { | ||
req.add('allowOverwrite false and target exists: ' + key + ', ' + to); | ||
if (options.allowOverwrite === false && obj.hasOwnProperty(to)) { | ||
errors.add('allowOverwrite false and target exists: ' + key + ', ' + to, keyPath); | ||
return false; | ||
} | ||
qstr[to] = value; | ||
obj[to] = value; | ||
if (options.deleteOrig === true) { | ||
delete qstr[key]; | ||
delete obj[key]; | ||
} | ||
@@ -316,3 +316,3 @@ | ||
internals.BaseType.prototype.validate = function (value, key, obj, errors) { | ||
internals.BaseType.prototype.validate = function (value, key, obj, errors, keyPath) { | ||
@@ -330,3 +330,3 @@ var status = true; | ||
status = false; | ||
errors.add('the value of `' + key + '` is not allowed to be ' + this.__invalids._values[this.__invalids._values.indexOf(value)]); | ||
errors.add('the value of `' + key + '` is not allowed to be ' + this.__invalids._values[this.__invalids._values.indexOf(value)], keyPath); | ||
if (this.options.shortCircuit === true) { | ||
@@ -346,3 +346,12 @@ return status; | ||
this.__valids._values.indexOf(value) < 0) { | ||
status = false; | ||
var valids = this.__valids._values.map(function (valid) { | ||
return typeof valid === 'undefined' ? 'undefined' : valid.toString(); | ||
}); | ||
errors.add('the value of `' + key + '` must be one of ' + valids.join(', '), keyPath); | ||
if (this.options.shortCircuit === true) { | ||
@@ -364,4 +373,4 @@ return status; | ||
var result = this.__validators[j](value, obj || { '0': value }, key || '0', errors); | ||
if (result === false) { | ||
var result = this.__validators[j](value, obj || { '0': value }, key || '0', errors, keyPath); | ||
if (!result) { | ||
status = false; | ||
@@ -375,4 +384,4 @@ if (this.options.shortCircuit === true) { | ||
for (var l in finalizeFns) { | ||
var result = this.__validators[finalizeFns[l]](value, obj || { '0': value }, key || '0', errors); | ||
if (result === false) { | ||
var resultFns = this.__validators[finalizeFns[l]](value, obj || { '0': value }, key || '0', errors, keyPath); | ||
if (!resultFns) { | ||
status = false; | ||
@@ -386,2 +395,2 @@ if (this.options.shortCircuit === true) { | ||
return status; | ||
}; | ||
}; |
@@ -44,11 +44,9 @@ // Load modules | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
var result = (value === null || typeof value === 'boolean'); | ||
if (!result) { | ||
req.add('the value of `' + key + '` must be a boolean'); | ||
errors.add('the value of `' + key + '` must be a boolean', keyPath); | ||
} | ||
return result; | ||
@@ -55,0 +53,0 @@ }; |
@@ -34,5 +34,11 @@ // Load modules | ||
return function(value) { | ||
return function(value, obj, key, errors, keyPath) { | ||
return (value === null || typeof value === "function"); | ||
var result = (value === null || typeof value === "function"); | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be a Function', keyPath); | ||
} | ||
return result; | ||
}; | ||
@@ -39,0 +45,0 @@ }; |
@@ -42,5 +42,7 @@ // Load modules | ||
internals.Types.prototype.validate = function (key, type, object, validator, placeholder) { | ||
internals.Types.prototype.validate = function (key, type, object, validator, errors) { | ||
var value = object[key]; | ||
errors = errors || {}; | ||
errors.add = errors.add || function () { }; | ||
@@ -59,11 +61,11 @@ // Convert value from String if necessary | ||
// errors stored as placeholder.validationErrors = [] | ||
if (placeholder) { | ||
placeholder.add = this.Base.prototype.RequestErrorFactory(placeholder); | ||
if (errors) { | ||
errors.add = errors.add || this.Base.prototype.RequestErrorFactory(errors); | ||
} | ||
var result = validator(value, object, key, placeholder); | ||
var result = validator(value, object, key, errors, key); | ||
if (placeholder) { | ||
if (errors) { | ||
// Remove from request object when finished | ||
delete placeholder.add; | ||
delete errors.add; | ||
} | ||
@@ -70,0 +72,0 @@ |
@@ -44,5 +44,6 @@ // Load modules | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
if (typeof value !== 'number' && typeof value !== 'string') { | ||
errors.add('the value of `' + key + '` must be a number', keyPath); | ||
return false; | ||
@@ -67,11 +68,9 @@ } | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
var result = (isNaN(value) || value >= n); | ||
if (result === false) { | ||
req.add('the value of `' + key + '` must be larger than (or equal to) ' + n); | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be larger than (or equal to) ' + n, keyPath); | ||
} | ||
return result; | ||
@@ -93,10 +92,7 @@ }; | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
var result = (value <= n); | ||
if (result === false) { | ||
req.add('the value of `' + key + '` must be less than (or equal to) ' + n); | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be less than (or equal to) ' + n, keyPath); | ||
} | ||
@@ -117,11 +113,9 @@ return result; | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
var result = (!isNaN(value) && ((value | 0) === parseFloat(value))); | ||
if (result === false) { | ||
req.add('the value of `' + key + '` must be an integer'); | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be an integer', keyPath); | ||
} | ||
return result; | ||
@@ -142,11 +136,9 @@ }; | ||
var isInt = this._integer(); | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
var result = (!isInt(value, obj, key, errors, keyPath)); | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be a float or double', keyPath); | ||
} | ||
var result = (!isInt(value)); | ||
if (result === false) { | ||
req.add('the value of `' + key + '` must be a float or double'); | ||
} | ||
return result; | ||
@@ -153,0 +145,0 @@ }; |
@@ -47,9 +47,10 @@ // Load modules | ||
return function (value) { | ||
return function (value, obj, key, errors, keyPath) { | ||
if (typeof value !== 'object') { | ||
errors.add('the value of `' + key + '` must be an object', keyPath); | ||
return false; | ||
} | ||
return self._traverse(value, self._config); | ||
return self._traverse(value, self._config, errors, keyPath); | ||
}; | ||
@@ -66,25 +67,31 @@ }; | ||
internals.ObjectType.prototype._traverse = function (obj, config) { | ||
internals.ObjectType.prototype._traverse = function (topObj, topConfig, errors, topKeyPath) { | ||
var self = this; | ||
var keys = Object.keys(config); | ||
var traverse = function (obj, config, errors, keyPath) { | ||
for (var i = 0, il = keys.length; i < il; ++i) { | ||
var key = keys[i]; | ||
var itemConfig = config[key]; | ||
var value = obj ? obj[key] : null; | ||
var keys = Object.keys(config); | ||
if (typeof itemConfig.validate === 'function' && | ||
itemConfig.validate(value) === false) { | ||
for (var i = 0, il = keys.length; i < il; ++i) { | ||
var key = keys[i]; | ||
var itemConfig = config[key]; | ||
var value = obj ? obj[key] : null; | ||
keyPath = keyPath ? topKeyPath + '.' + key : key; | ||
return false; | ||
} | ||
if (typeof itemConfig.validate === 'function' && | ||
itemConfig.validate(value, key, itemConfig, errors, keyPath) === false) { | ||
if (itemConfig.type === 'Object') { | ||
self._traverse(value, itemConfig._config); | ||
return false; | ||
} | ||
if (itemConfig.type === 'Object') { | ||
traverse(value, itemConfig._config, errors, topKeyPath); | ||
} | ||
} | ||
} | ||
return true; | ||
return true; | ||
}; | ||
return traverse(topObj, topConfig, errors, topKeyPath); | ||
}; |
@@ -37,5 +37,11 @@ // Load modules | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
return typeof value === 'string'; | ||
var result = typeof value === 'string'; | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be a string', keyPath); | ||
} | ||
return result; | ||
}; | ||
@@ -62,19 +68,13 @@ }; | ||
Utils.assert((!isNaN(n) && ((n | 0) == parseFloat(n))), 'In Types.String.min(n), the n must be an integer.'); | ||
Utils.assert((!isNaN(n) && ((n | 0) === parseFloat(n))), 'In Types.String.min(n), the n must be an integer.'); | ||
Utils.assert(n >= 0, 'In Types.String.min(n), the n must be a non-negative integer.'); | ||
this.__valids.remove(undefined); | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
if (value === null || typeof value === 'undefined') { | ||
return false; | ||
var result = value !== null && typeof value !== 'undefined' && (value.length >= n); | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be at least ' + n + ' characters long', keyPath); | ||
} | ||
var result = (value.length >= n); | ||
if (result === false) { | ||
req.add('the value of `' + key + '` must be at least ' + n + ' characters long'); | ||
} | ||
return result; | ||
@@ -98,11 +98,9 @@ }; | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
var result = value !== null && typeof value !== 'undefined' && (value.length <= n); | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be less than (or equal to) ' + n + ' characters long', keyPath); | ||
} | ||
var result = (value.length <= n); | ||
if (result === false) { | ||
req.add('the value of `' + key + '` must be less than (or equal to) ' + n + ' characters long'); | ||
} | ||
return result; | ||
@@ -124,10 +122,7 @@ }; | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
var result = (value.match(n) !== null); | ||
if (result === false) { | ||
req.add('the value of `' + key + '` must match the RegExp `' + n.toString() + '`'); | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must match the RegExp `' + n.toString() + '`', keyPath); | ||
} | ||
@@ -148,6 +143,6 @@ return result; | ||
return function (value, qs, key, req) { | ||
return function (value, obj, key, errors, keyPath) { | ||
req = req || {}; | ||
req.add = req.add || function () { }; | ||
errors = errors || {}; | ||
errors.add = errors.add || function () { }; | ||
value = (isNaN(Number(value)) === false) ? +value : value; | ||
@@ -157,7 +152,7 @@ var converted = new Date(value); | ||
var result = (!isNaN(converted.getTime())); | ||
if (result === false) { | ||
req.add('the value of `' + key + '` must be a valid JavaScript Date format'); | ||
if (!result) { | ||
errors.add('the value of `' + key + '` must be a valid JavaScript Date format', keyPath); | ||
} | ||
return result; | ||
} | ||
}; | ||
}; | ||
@@ -193,3 +188,2 @@ | ||
return this; | ||
}; | ||
}; |
// Load modules | ||
var Hoek = require('hoek'); | ||
var Util = require('util'); | ||
@@ -37,1 +38,3 @@ | ||
exports.inherits = Util.inherits; |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"author": "Van Nguyen <the.gol.effect@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -100,10 +100,9 @@ // Load modules | ||
it('should fail validation when config is an array and fails', function (done) { | ||
it('should fail validation when missing a required parameter', function (done) { | ||
var obj = { | ||
a: 10, | ||
b: 'a' | ||
c: 10 | ||
}; | ||
Joi.validate(obj, config2, function (err) { | ||
Joi.validate(obj, { a: Joi.types.String().required() }, function (err) { | ||
@@ -115,5 +114,19 @@ expect(err).to.exist; | ||
it('should fail validation when config is an array and fails along with conversion failing', function (done) { | ||
it('should fail validation when missing a required parameter within an object config', function (done) { | ||
var obj = { | ||
a: { } | ||
}; | ||
Joi.validate(obj, { a: Joi.types.Object({ b: Joi.types.String().required() }) }, function (err) { | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
}); | ||
it('should fail validation when config is an array and fails', function (done) { | ||
var obj = { | ||
d: 10, | ||
@@ -130,2 +143,16 @@ e: 'a' | ||
it('should fail validation when config is an array and fails with extra keys', function (done) { | ||
var obj = { | ||
a: 10, | ||
b: 'a' | ||
}; | ||
Joi.validate(obj, config2, function (err) { | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
}); | ||
it('should work when the skipFunctions setting is enabled', function (done) { | ||
@@ -196,3 +223,2 @@ | ||
expect(err).to.exist; | ||
expect(err.message).to.contain('keys'); | ||
Joi.settings.skipFunctions = false; | ||
@@ -203,11 +229,9 @@ done(); | ||
it('should work with null options', function (done) { | ||
it('should fail validation when a child object has an invalid string value but object traversal isn\'t complete', function (done) { | ||
var schema = { item: Joi.types.Number() }; | ||
var input = { item: '1' }; | ||
var input = { method: 'GET', path: '/', config: { payload: 'something' } }; | ||
Joi.validate(input, schema, null, function (err) { | ||
Joi.validate(input, internals.routeSchema, function (err) { | ||
expect(err).to.not.exist; | ||
expect(input.item).to.equal('1'); | ||
expect(err).to.exist; | ||
done(); | ||
@@ -217,3 +241,3 @@ }); | ||
it('should fail validation when a child object has an invalid string value but object traversal isn\'t complete', function (done) { | ||
it('validation errors should provide an annotated message when making the error annotated', function (done) { | ||
@@ -224,3 +248,4 @@ var input = { method: 'GET', path: '/', config: { payload: 'something' } }; | ||
expect(err).to.exist; | ||
err.annotated(); | ||
expect(err.message).to.contain('\u001b[0m'); | ||
done(); | ||
@@ -227,0 +252,0 @@ }); |
@@ -121,2 +121,18 @@ // Load modules | ||
it('should allow types to be excluded', function (done) { | ||
var validator = A().excludes(N()); | ||
var n = [1, 2, 'hippo']; | ||
var result = validator.validate(n); | ||
expect(result).to.equal(false); | ||
var m = ['x', 'y', 'z']; | ||
var result2 = validator.validate(m); | ||
expect(result2).to.equal(true); | ||
done(); | ||
}); | ||
it('should validate array of Numbers', function (done) { | ||
@@ -176,22 +192,3 @@ | ||
}); | ||
describe('#_exclude', function () { | ||
it('should work', function (done) { | ||
var validator = A()._excludes(N()); | ||
var n = [1, 2, 'hippo']; | ||
var result = validator(n); | ||
expect(result).to.equal(false); | ||
var m = ['x', 'y', 'z']; | ||
var result2 = validator(m); | ||
expect(result2).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -77,2 +77,3 @@ // Load modules | ||
var called = false; | ||
var object = { | ||
@@ -82,11 +83,16 @@ testme: '1' | ||
var placeholder = {}; | ||
var placeholder = { | ||
add: function () { | ||
called = true; | ||
} | ||
}; | ||
var validator = function (val) { | ||
var validator = function (value, object, key, errors) { | ||
return val; | ||
errors.add(); | ||
return true; | ||
}; | ||
expect(Joi.types.validate('testme', 'Number', object, validator, placeholder)).to.equal(1); | ||
expect(placeholder.validationErrors).to.exist; | ||
expect(Joi.types.validate('testme', 'Number', object, validator, placeholder)).to.equal(true); | ||
expect(called).to.be.true; | ||
done(); | ||
@@ -93,0 +99,0 @@ }); |
@@ -364,3 +364,3 @@ // Load modules | ||
}); | ||
}) | ||
}); | ||
@@ -379,17 +379,5 @@ describe('#_min', function () { | ||
done(); | ||
}) | ||
}); | ||
}); | ||
it('should invalidate on known invalid inputs', function (done) { | ||
var inputs = [0, 1, 2, 3, 4]; | ||
var validator = N()._min(5); | ||
for (var i in inputs) { | ||
var currentResult = validator(inputs[i]); | ||
expect(currentResult).to.exist; | ||
expect(currentResult).to.equal(false); | ||
} | ||
done(); | ||
}) | ||
}) | ||
describe('#max', function () { | ||
@@ -422,15 +410,3 @@ | ||
}); | ||
it('should invalidate on known invalid inputs', function (done) { | ||
var inputs = [5, 6, 7, 8]; | ||
var validator = N()._max(4); | ||
for (var i in inputs) { | ||
var currentResult = validator(inputs[i]); | ||
expect(currentResult).to.exist; | ||
expect(currentResult).to.equal(false); | ||
} | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -188,5 +188,6 @@ // Load modules | ||
var t = S()._min(3); | ||
expect(t(null)).to.equal(false); | ||
done(); | ||
var t = S().min(3); | ||
verifyBehavior(t, [ | ||
[null, false] | ||
], done); | ||
}); | ||
@@ -193,0 +194,0 @@ |
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
33
272606
2866