Socket
Socket
Sign inDemoInstall

joi

Package Overview
Dependencies
Maintainers
2
Versions
238
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

joi - npm Package Compare versions

Comparing version 0.0.8 to 0.0.9

96

lib/index.js

@@ -22,7 +22,15 @@ // Load modules

exports.validate = function (object, config, next) {
exports.validate = function (object, config, options, next) {
var elementKeys = Object.keys(config || {});
if (options == null) {
options = {};
}
if (next == undefined && typeof options == 'function') {
next = options;
options = {};
}
var self = this;
var submitted = Utils.clone(object || {});
var isInvalid = false;
var errorMsg = null;

@@ -34,32 +42,64 @@

for (var i in elementKeys) {
var finalizeFns = [];
var processConfig = function () {
if (elementKeys.hasOwnProperty(i)) {
var key = elementKeys[i];
Object.keys(config || {}).forEach(function (key) {
var keyConfig = config[key];
var value = object[key];
var validators = config[key].__validators;
var T = Types[config[key].type];
var converter = T().convert || null;
if (typeof converter === 'function') {
value = converter(value);
if (this.settings.saveConversions) {
if (!validateKeyConfig(keyConfig, key, value)) {
errorMsg = key + ' = ' + value;
}
else {
if(options.convert) {
object[key] = value;
}
delete submitted[key];
}
});
};
var result = config[key].validate(value, key, object, placeholder);
var validateKeyConfig = function (keyConfig, key, value) {
if (!result) {
isInvalid = true;
errorMsg = key + ' = ' + value;
var converted = null;
if (keyConfig instanceof Array) {
for (var i = 0, il = keyConfig.length; i < il; ++i) {
converted = convertType(keyConfig[i], key, value);
if (converted && keyConfig[i].validate(converted.value, key, object, placeholder)) {
return true;
}
}
else {
delete submitted[key];
return false;
}
converted = convertType(keyConfig, key, value);
return converted && keyConfig.validate(converted.value, key, object, placeholder);
};
var convertType = function (keyConfig, key, value) {
var T = Types[keyConfig.type];
var converter = T && T().convert || null;
if (typeof converter === 'function') {
try {
value = converter(value);
if (self.settings.saveConversions) {
object[key] = value;
}
return { value: value };
}
catch (err) {
return null;
}
}
}
return { value: value }; // If no convert function then just return the value
};
processConfig();
delete placeholder._renamed;

@@ -82,10 +122,2 @@

isInvalid = true;
var plural = '';
var verb = 'is';
if (processed.length > 1) {
plural = 's';
verb = 'are';
}
var processedValues = processed.map(function (d) {

@@ -102,7 +134,8 @@

var plural = (processed.length > 1 ? 's' : '');
var plural = (processed.length > 1) ? 's' : '';
var verb = (processed.length > 1) ? 'are' : 'is';
errorMsg = 'the key' + plural + ' (' + processed + ') ' + verb + ' not allowed (values: ' + processedValues + ')';
}
if (isInvalid) {
if (errorMsg) {
if (placeholder.validationErrors && placeholder.validationErrors.length > 0) {

@@ -117,2 +150,1 @@ return next(new Error(placeholder.validationErrors.join('.\n')));

};

@@ -38,2 +38,8 @@ // Load modules

// first check with isNaN, because JSON.parse convert number string to number!
if(!isNaN(value)) {
//given value is a string which could be converted to number, just return it.
return value;
}
// No need to try/catch, `Invalid JSON Body` exception already handled elsehwere

@@ -50,2 +56,5 @@ try {

catch (e) {
//JSON.parse failed, means that given string is a string
//which cannot be converted into JSON format. Just return it as an array.
return [value];
}

@@ -272,2 +281,2 @@ }

};
*/
*/

@@ -66,5 +66,5 @@ // Load modules

value[internals.DATA_KEY] = this[internals.DATA_KEY]
value[internals.KEY_LIST] = this[internals.KEY_LIST]
value[internals.ARGS_LIST] = this[internals.ARGS_LIST]
value[internals.DATA_KEY] = this[internals.DATA_KEY];
value[internals.KEY_LIST] = this[internals.KEY_LIST];
value[internals.ARGS_LIST] = this[internals.ARGS_LIST];

@@ -95,3 +95,3 @@ return value;

this[internals.DATA_KEY].push(value);
this[internals.KEY_LIST].push(key)
this[internals.KEY_LIST].push(key);
this[internals.ARGS_LIST].push(args);

@@ -134,2 +134,10 @@ }

internals.BaseType.prototype.optional = function () {
this.__invalids.remove(undefined);
this.__valids.add(undefined);
return this;
};
internals.BaseType.prototype.nullOk = function () {

@@ -151,5 +159,8 @@

internals.BaseType.prototype.valid = function () {
internals.BaseType.prototype.valid = function (acceptable) {
var acceptable = Array.prototype.slice.call(arguments);
if(!Array.isArray(acceptable)) {
acceptable = Array.prototype.slice.call(arguments);
}
for (var i = acceptable.length - 1; i >= 0; --i) {

@@ -164,5 +175,8 @@ Utils.assert(this.validate(acceptable[i]) == true, 'input to .valid() must be valid ' + this.__name + '(' + acceptable[i] + ')');

internals.BaseType.prototype.invalid = function () {
internals.BaseType.prototype.invalid = function (unacceptable) {
var unacceptable = Array.prototype.slice.call(arguments);
if(!Array.isArray(unacceptable)) {
unacceptable = Array.prototype.slice.call(arguments);
}
for (var i = unacceptable.length - 1; i >= 0; --i) {

@@ -169,0 +183,0 @@ Utils.assert(this.validate(unacceptable[i]) == true, 'input to .invalid() must be valid ' + this.__name + '(' + unacceptable[i] + ')');

@@ -39,3 +39,3 @@ // Load modules

return JSON.parse(value);
return (typeof value === 'object' || typeof value === 'undefined') ? value : JSON.parse(value);
};

@@ -42,0 +42,0 @@

{
"name": "joi",
"description": "Object schema validation",
"version": "0.0.8",
"version": "0.0.9",
"author": "Van Nguyen <the.gol.effect@gmail.com>",

@@ -6,0 +6,0 @@ "contributors": [

@@ -25,2 +25,18 @@ // Load modules

var config2 = {
d: [Joi.types.String(), Joi.types.Boolean()],
e: [Joi.types.Number(), Joi.types.Object()]
};
var config3 = {
f: [Joi.types.Number(), Joi.types.Boolean()],
g: [Joi.types.String(), Joi.types.Object()]
};
var config4 = {
h: Joi.types.Number(),
i: Joi.types.String(),
j: Joi.types.Object().optional()
};
it('should validate object successfully', function (done) {

@@ -41,2 +57,30 @@

it('should validate object successfully when config has an array of types', function (done) {
var obj = {
f: true,
g: 'test'
};
Joi.validate(obj, config3, function (err) {
expect(err).to.not.exist;
done();
});
});
it('should validate object successfully when config allows for optional key and key is missing', function (done) {
var obj = {
h: 12,
i: 'test'
};
Joi.validate(obj, config4, function (err) {
expect(err).to.not.exist;
done();
});
});
it('should fail validation', function (done) {

@@ -57,2 +101,30 @@

it('should fail validation when config is an array and fails', function (done) {
var obj = {
a: 10,
b: 'a'
};
Joi.validate(obj, config2, function (err) {
expect(err).to.exist;
done();
});
});
it('should fail validation when config is an array and fails along with conversion failing', function (done) {
var obj = {
d: 10,
e: 'a'
};
Joi.validate(obj, config2, function (err) {
expect(err).to.exist;
done();
});
});
it('should work when the skipFunctions setting is enabled', function (done) {

@@ -59,0 +131,0 @@

@@ -50,8 +50,17 @@ // Load modules

it('should convert a non-array string', function (done) {
it('should convert a non-array string with number type', function (done) {
var result = A().convert('3');
expect(result.length).to.equal(1);
expect(result[0]).to.equal('3');
done();
});
it('should convert a non-array string', function(done) {
var result = A().convert('asdf');
expect(result.length).to.equal(1);
expect(result[0]).to.equal('asdf');
done();
});
});

@@ -58,0 +67,0 @@

@@ -156,2 +156,12 @@ // Load modules

it('should handle array arguments correctly', function(done) {
var t = S().valid(['a', 'b', 'c']);
verifyBehavior(t, [
['x', false],
['a', true],
['c', true]
], done);
});
it('should validate minimum length when min is used', function (done) {

@@ -869,2 +879,2 @@

});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc