Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Commit

Permalink
#1 Fixing no Enum validation error raised if type string specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Gougeon committed Sep 2, 2015
1 parent ca3d57b commit 338e574
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/enjoi.js
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 @@ -180,6 +181,10 @@ 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':
Expand Down
15 changes: 14 additions & 1 deletion test/test-enjoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,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 @@ -384,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 338e574

Please sign in to comment.