Comparing version 0.0.1 to 0.0.2
101
lib/joi.js
// Load modules | ||
var Err = require('./error'); | ||
var Utils = require('./utils'); | ||
var Types = require('./types'); | ||
var Utils = require('./utils'); | ||
@@ -13,52 +12,23 @@ | ||
// Validate query | ||
exports.Types = Types; | ||
exports.query = function (request, next) { | ||
// true - anything allowed | ||
// false, null, {} - nothing allowed | ||
// {...} - ... allowed | ||
exports.validate = function (object, config, next) { | ||
if (request._route.config.query === true) { | ||
return next(); | ||
} | ||
return internals.validateRequestElement(request, 'query', request._route.config.query, next); | ||
}; | ||
// Validate payload schema | ||
exports.payload = function (request, next) { | ||
// null, undefined - anything allowed | ||
// false, {} - nothing allowed | ||
// {...} - ... allowed | ||
if (request._route.config.schema === null || | ||
request._route.config.schema === undefined) { // Value can be false | ||
return next(); | ||
} | ||
return internals.validateRequestElement(request, 'payload', request._route.config.schema, next); | ||
}; | ||
internals.validateRequestElement = function (request, elementKey, config, next) { | ||
var elementKeys = Object.keys(config || {}); | ||
var submitted = Utils.clone(request[elementKey] || {}); | ||
var submitted = Utils.clone(object || {}); | ||
var isInvalid = false; | ||
var errorMsg = null; | ||
var finalizeFns = []; | ||
request._renamed = {}; | ||
var placeholder = {}; | ||
placeholder._renamed = {}; | ||
for (var i in elementKeys) { | ||
if (elementKeys.hasOwnProperty(i)) { | ||
var key = elementKeys[i]; | ||
var validators = config[key]._validators; | ||
var validators = config[key].__validators; | ||
for (var j in validators) { | ||
if (validators.hasOwnProperty(j)) { | ||
var validatorName = config[key]._checks[j]; | ||
var validatorName = config[key].__checks[j]; | ||
@@ -70,6 +40,6 @@ if (validatorName in Types.mutatorMethods) { | ||
var result = exports.param(key, config[key].type, request, validators[j], elementKey); | ||
var result = Types.validate(key, config[key].type, object, validators[j], placeholder); | ||
if (result === false) { | ||
isInvalid = true; | ||
errorMsg = key + ' = ' + request[elementKey][key]; | ||
errorMsg = key + ' = ' + object[key]; | ||
break; | ||
@@ -83,6 +53,6 @@ } | ||
for (var l in finalizeFns) { | ||
var result = exports.param(key, config[key].type, request, validators[j], elementKey); | ||
var result = Types.validate(key, config[key].type, object, validators[j], placeholder); | ||
if (result === false) { | ||
isInvalid = true; | ||
errorMsg = 'error on renaming ' + key + ' = ' + request[elementKey][key]; | ||
errorMsg = 'error on renaming ' + key + ' = ' + object[key]; | ||
break; | ||
@@ -94,3 +64,3 @@ } | ||
delete request._renamed; | ||
delete placeholder._renamed; | ||
@@ -112,3 +82,3 @@ // Handle inputs that haven't been defined in config | ||
var plural = (processed.length > 1 ? 's' : ''); | ||
errorMsg = 'the key' + plural + ' (' + processed + ') ' + verb + ' not allowed in ' + elementKey + ' for ' + request.url; | ||
errorMsg = 'the key' + plural + ' (' + processed + ') ' + verb + ' not allowed'; | ||
} | ||
@@ -118,9 +88,9 @@ | ||
if (request.validationErrors && request.validationErrors.length > 0) { | ||
if (placeholder.validationErrors && placeholder.validationErrors.length > 0) { | ||
return next(Err.badRequest(request.validationErrors.join(".\n"))); | ||
return next(new Error(placeholder.validationErrors.join(".\n"))); | ||
} | ||
else { | ||
return next(Err.badRequest('Invalid ' + elementKey + ' parameter: ' + errorMsg)); | ||
return next(new Error('Invalid parameter: ' + errorMsg)); | ||
} | ||
@@ -134,36 +104,1 @@ } | ||
// Validate individual param | ||
exports.param = function (key, type, req, validator, mode) { | ||
mode = mode || 'query'; | ||
try { | ||
var value = req[mode][key]; | ||
} catch (e) { | ||
throw e; | ||
} | ||
// Convert value from String if necessary | ||
var Type = Types[type]; | ||
var converter = Type().convert || null; | ||
if (typeof converter !== 'undefined' && | ||
converter !== null) { | ||
value = converter(value); | ||
} | ||
// Set request-scoped error writer | ||
// errors stored as req.validationErrors = [] | ||
req.addValidationError = Types.Base.prototype.RequestErrorFactory(req); | ||
var result = validator(value, req[mode], key, req); | ||
// Remove from request object when finished | ||
delete req.addValidationError; | ||
return result; | ||
}; | ||
@@ -9,5 +9,5 @@ /** | ||
*/ | ||
var INTERNAL_DATA_KEY = '_validators'; | ||
var INTERNAL_KEY_LIST = '_checks'; | ||
var INTERNAL_ARGS_LIST = '_args'; | ||
var INTERNAL_DATA_KEY = '__validators'; | ||
var INTERNAL_KEY_LIST = '__checks'; | ||
var INTERNAL_ARGS_LIST = '__args'; | ||
@@ -23,2 +23,4 @@ /** | ||
this[INTERNAL_ARGS_LIST] = []; | ||
this.__valids = new Utils.Set(this.__defaultValids || []); | ||
this.__invalids = new Utils.Set(this.__defaultInvalids || []); | ||
@@ -69,2 +71,6 @@ if (typeof this.base !== 'undefined' && this.base !== null) { | ||
// BaseType.prototype._required = function() { | ||
// } | ||
BaseType.prototype._required = function(allowNull) { | ||
@@ -108,2 +114,11 @@ | ||
for(var i = acceptable.length; i >= 0; i--) { | ||
this._valids.add(acceptable[i]); | ||
this._invalids.remove(acceptable[i]); | ||
} | ||
} | ||
BaseType.prototype._validOld = function(acceptable) { | ||
return function(value) { | ||
@@ -125,3 +140,3 @@ | ||
this.add('valid', this._valid(Array.prototype.slice.call(arguments)), arguments); | ||
// this.add('valid', this._valid(Array.prototype.slice.call(arguments)), arguments); | ||
return this; | ||
@@ -132,12 +147,21 @@ } | ||
var self = this; | ||
return function(value) { | ||
for(var i = unacceptable.length; i >= 0; i--) { | ||
return !self._valid(unacceptable)(value); | ||
this._invalids.add(unacceptable[i]); | ||
this._valids.remove(unacceptable[i]); | ||
} | ||
} | ||
// BaseType.prototype._xinvalid = function(unacceptable) { | ||
// var self = this; | ||
// return function(value) { | ||
// return !self._valid(unacceptable)(value); | ||
// } | ||
// } | ||
BaseType.prototype.invalid = function() { | ||
this.add('invalid', this._invalid(Array.prototype.slice.call(arguments)), arguments); | ||
// this.add('invalid', this._invalid(Array.prototype.slice.call(arguments)), arguments); | ||
return this; | ||
@@ -258,2 +282,3 @@ } | ||
BaseType.prototype.RequestErrorFactory = function(request) { | ||
request.validationErrors = []; | ||
@@ -260,0 +285,0 @@ |
@@ -33,2 +33,40 @@ /** | ||
module.exports = new Types(); | ||
Types.prototype.validate = function(key, type, object, validator, placeholder) { | ||
try { | ||
var value = object[key]; | ||
} catch (e) { | ||
throw e; | ||
} | ||
// Convert value from String if necessary | ||
var T = this[type]; | ||
var converter = T().convert || null; | ||
if (typeof converter !== 'undefined' && | ||
converter !== null) { | ||
value = converter(value); | ||
} | ||
// Set request-scoped error writer | ||
// errors stored as placeholder.validationErrors = [] | ||
if (placeholder) { | ||
placeholder.addValidationError = this.Base.prototype.RequestErrorFactory(placeholder); | ||
} | ||
var result = validator(value, object, key, placeholder); | ||
if (placeholder) { | ||
// Remove from request object when finished | ||
delete placeholder.addValidationError; | ||
} | ||
return result; | ||
}; | ||
module.exports = new Types(); | ||
@@ -11,3 +11,3 @@ var util = require("util"); | ||
// this.init() | ||
Utils.mixin(this, BaseType); | ||
} | ||
@@ -39,3 +39,25 @@ | ||
// } | ||
StringType.prototype.__defaultValids = [undefined]; | ||
StringType.prototype._ibase = function() { | ||
var self = this; | ||
return function(value, qs, key, req) { | ||
// var valid = self.__valids.map(function(d){ | ||
// return qs[key] == d; | ||
// }).indexOf(true) >= 0; | ||
// var invalid = self.__invalids.map(function(d){ | ||
// return qs[key] == d; | ||
// }).indexOf(true) >= 0; | ||
// return valid && !invalid; | ||
// console.log(self.__invalids.map(function(d){return qs[key] == d;})); | ||
return false; | ||
} | ||
} | ||
StringType.prototype._base = function() { | ||
@@ -42,0 +64,0 @@ |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"author": "Van Nguyen <the.gol.effect@gmail.com>", | ||
@@ -19,2 +19,3 @@ "contributors":[ | ||
"dependencies": { | ||
"hoek": "0.0.x" | ||
}, | ||
@@ -21,0 +22,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
21
0
48810
1
1308
1
+ Addedhoek@0.0.x
+ Addedasync@0.1.22(transitive)
+ Addedhoek@0.0.21(transitive)