Skip to content

Commit

Permalink
Merge pull request #10 from worldline/stringFormatValidation
Browse files Browse the repository at this point in the history
#1 Datetime, date and email format validation, full enum support
  • Loading branch information
tlivings committed Oct 18, 2015
2 parents 932fa47 + 338e574 commit 98776b9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
34 changes: 34 additions & 0 deletions lib/enjoi.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = function enjoi(schema, options) {
return resolve(resolveref(current.$ref));
}

//if no type is specified, just enum
if (current.enum) {
return Joi.any().valid(current.enum);
}
Expand Down Expand Up @@ -179,7 +180,28 @@ module.exports = function enjoi(schema, options) {

function string(current) {
var joischema = Joi.string();

if (current.enum) {
return Joi.any().valid(current.enum);
}

switch (current.format) {
case 'date':
case 'date-time':
joischema = date(current);
break;
case 'email':
joischema = email(current);
break;
default:
joischema = regularString(current);
break;
}
return joischema;
}

function regularString(current) {
var joischema = Joi.string();
current.pattern && (joischema = joischema.regex(new RegExp(current.pattern)));

if (Thing.isNumber(current.minLength)) {
Expand All @@ -190,7 +212,19 @@ module.exports = function enjoi(schema, options) {
}

Thing.isNumber(current.maxLength) && (joischema = joischema.max(current.maxLength));
return joischema;
}

function email(current) {
var joischema = Joi.string().email();
Thing.isNumber(current.maxLength) && (joischema = joischema.max(current.maxLength));
return joischema;
}

function date(current) {
var joischema = Joi.date();
current.min && (joischema = joischema.min(current.min));
current.max && (joischema = joischema.max(current.max));
return joischema;
}

Expand Down
69 changes: 68 additions & 1 deletion test/test-enjoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,60 @@ Test('types', function (t) {
});
});

t.test('string email', function (t) {
t.plan(2);

var schema = Enjoi({
'type': 'string',
'format': 'email',
'maxLength': '20'
});

Joi.validate('wrongemail', schema, function (error, value) {
t.ok(error, "wrong email error.");
});

Joi.validate('right@email.com', schema, function (error, value) {
t.ok(!error, "good email.");
});


});

t.test('string date ISO 8601', function (t) {
t.plan(5);

var schema = Enjoi({
'type': 'string',
'format': 'date',
'min': '1-1-2000 UTC',
'max': Date.now()
});

Joi.validate('1akd2536', schema, function (error, value) {
t.ok(error, "wrong date format.");
});

Joi.validate('12-10-1900 UTC', schema, function (error, value) {
t.ok(error, "minimum date.");
});



Joi.validate(Date.now() + 1000000, schema, function (error, value) {
t.ok(error, "maximum date.");
});

Joi.validate('1-2-2015 UTC', schema, function (error, value) {
t.ok(!error, "good date.");
});

Joi.validate('2005-01-01', schema, function (error, value) {
t.ok(!error, "good date 2");
});

});

t.test('no type, ref, or enum validates anything.', function (t) {
t.plan(3);

Expand All @@ -313,7 +367,7 @@ Test('types', function (t) {
});

t.test('enum', function (t) {
t.plan(3);
t.plan(5);

var schema = Enjoi({
'enum': ['A', 'B']
Expand All @@ -330,6 +384,19 @@ Test('types', function (t) {
Joi.validate('C', schema, function (error, value) {
t.ok(error, 'error.');
});

schema = Enjoi({
type: 'string',
'enum': ['A', 'B']
});

Joi.validate('B', schema, function (error, value) {
t.ok(!error, 'no error.');
});

Joi.validate('C', schema, function (error, value) {
t.ok(error, 'error.');
});
});

t.test('unknown type fails', function (t) {
Expand Down

0 comments on commit 98776b9

Please sign in to comment.