joi
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -23,3 +23,3 @@ // Load modules | ||
exports.validate = function (object, config, next) { | ||
exports.validate = function (object, config) { | ||
@@ -102,3 +102,3 @@ var self = this; | ||
return next(errors.toError()); | ||
return errors.toError(); | ||
}; |
@@ -178,6 +178,19 @@ // Load modules | ||
internals.StringType.prototype._email = function (regex) { | ||
return function (value, obj, key, errors, keyPath) { | ||
var result = (value.match(regex) !== null); | ||
if (!result) { | ||
errors.add('The value of `' + key + '` must be a valid email', keyPath); | ||
} | ||
return result; | ||
}; | ||
}; | ||
internals.StringType.prototype.email = function () { | ||
this.regex(Email._regex); | ||
this.add('email', this._email(Email._regex), arguments); | ||
return this; | ||
}; |
{ | ||
"name": "joi", | ||
"description": "Object schema validation", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"author": "Van Nguyen <the.gol.effect@gmail.com>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -49,8 +49,6 @@ // Load modules | ||
}; | ||
var err = Joi.validate(obj, config); | ||
Joi.validate(obj, config, function (err) { | ||
expect(err).to.not.exist; | ||
done(); | ||
}); | ||
expect(err).to.not.exist; | ||
done(); | ||
}); | ||
@@ -64,8 +62,6 @@ | ||
}; | ||
var err = Joi.validate(obj, config3); | ||
Joi.validate(obj, config3, function (err) { | ||
expect(err).to.not.exist; | ||
done(); | ||
}); | ||
expect(err).to.not.exist; | ||
done(); | ||
}); | ||
@@ -79,8 +75,6 @@ | ||
}; | ||
var err = Joi.validate(obj, config4); | ||
Joi.validate(obj, config4, function (err) { | ||
expect(err).to.not.exist; | ||
done(); | ||
}); | ||
expect(err).to.not.exist; | ||
done(); | ||
}); | ||
@@ -95,8 +89,6 @@ | ||
}; | ||
var err = Joi.validate(obj, config); | ||
Joi.validate(obj, config, function (err) { | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
@@ -109,8 +101,6 @@ | ||
}; | ||
var err = Joi.validate(obj, { a: Joi.types.String().required() }); | ||
Joi.validate(obj, { a: Joi.types.String().required() }, function (err) { | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
@@ -123,8 +113,6 @@ | ||
}; | ||
var err = Joi.validate(obj, { a: Joi.types.Object({ b: Joi.types.String().required() }) }); | ||
Joi.validate(obj, { a: Joi.types.Object({ b: Joi.types.String().required() }) }, function (err) { | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
@@ -139,8 +127,6 @@ | ||
}; | ||
var err = Joi.validate(obj, config2); | ||
Joi.validate(obj, config2, function (err) { | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
@@ -154,8 +140,6 @@ | ||
}; | ||
var err = Joi.validate(obj, config2); | ||
Joi.validate(obj, config2, function (err) { | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
@@ -168,9 +152,7 @@ | ||
var input = { username: 'test', func: function() { } }; | ||
var err = Joi.validate(input, schema); | ||
Joi.validate(input, schema, function (err) { | ||
expect(err).to.not.exist; | ||
Joi.settings.skipFunctions = false; | ||
done(); | ||
}); | ||
expect(err).to.not.exist; | ||
Joi.settings.skipFunctions = false; | ||
done(); | ||
}); | ||
@@ -183,8 +165,6 @@ | ||
var input = { username: 'test', func: function() { } }; | ||
var err = Joi.validate(input, schema); | ||
Joi.validate(input, schema, function (err) { | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
@@ -197,10 +177,8 @@ | ||
var input = { item: '1' }; | ||
var err = Joi.validate(input, schema); | ||
Joi.validate(input, schema, function (err) { | ||
expect(err).to.not.exist; | ||
expect(input.item).to.equal(1); | ||
Joi.settings.saveConversions = false; | ||
done(); | ||
}); | ||
expect(err).to.not.exist; | ||
expect(input.item).to.equal(1); | ||
Joi.settings.saveConversions = false; | ||
done(); | ||
}); | ||
@@ -213,9 +191,7 @@ | ||
var input = { item: '1' }; | ||
var err = Joi.validate(input, schema); | ||
Joi.validate(input, schema, function (err) { | ||
expect(err).to.not.exist; | ||
expect(input.item).to.equal('1'); | ||
done(); | ||
}); | ||
expect(err).to.not.exist; | ||
expect(input.item).to.equal('1'); | ||
done(); | ||
}); | ||
@@ -228,9 +204,7 @@ | ||
var input = { username: 'test', item1: 'test', 'item2': 'test' }; | ||
var err = Joi.validate(input, schema); | ||
Joi.validate(input, schema, function (err) { | ||
expect(err).to.exist; | ||
Joi.settings.skipFunctions = false; | ||
done(); | ||
}); | ||
expect(err).to.exist; | ||
Joi.settings.skipFunctions = false; | ||
done(); | ||
}); | ||
@@ -241,8 +215,6 @@ | ||
var input = { method: 'GET', path: '/', config: { payload: 'something' } }; | ||
var err = Joi.validate(input, internals.routeSchema); | ||
Joi.validate(input, internals.routeSchema, function (err) { | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
expect(err).to.exist; | ||
done(); | ||
}); | ||
@@ -253,9 +225,7 @@ | ||
var input = { method: 'GET', path: '/', config: { payload: 'something' } }; | ||
var err = Joi.validate(input, internals.routeSchema); | ||
Joi.validate(input, internals.routeSchema, function (err) { | ||
err.annotated(); | ||
expect(err.message).to.contain('\u001b[0m'); | ||
done(); | ||
}); | ||
err.annotated(); | ||
expect(err.message).to.contain('\u001b[0m'); | ||
done(); | ||
}); | ||
@@ -262,0 +232,0 @@ |
@@ -249,2 +249,11 @@ // Load modules | ||
it('should validate email with a friendly error message', function (done) { | ||
var schema = { item: S().email() }; | ||
var err = Joi.validate({ item: 'something' }, schema); | ||
expect(err.message).to.contain('must be a valid email'); | ||
done(); | ||
}); | ||
it('should validate date', function (done) { | ||
@@ -251,0 +260,0 @@ |
272824