Skip to content

Commit

Permalink
Change compile. Closes #2046. Closes #2049
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Aug 16, 2019
1 parent 3f0795a commit 5cfde5b
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 42 deletions.
32 changes: 20 additions & 12 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,16 @@ internals.schema = function (Joi, config) {

Assert(config !== undefined, 'Invalid undefined schema');

if (config === null) {
return Joi.valid(null);
}

if (typeof config === 'string') {
return Joi.string().valid(config);
}
if (Array.isArray(config)) {
Assert(config.length, 'Invalid empty array schema');

if (typeof config === 'number') {
return Joi.number().valid(config);
if (config.length === 1) {
config = config[0];
}
}

if (typeof config === 'boolean') {
return Joi.boolean().valid(config);
if (internals.simple(config)) {
return Joi.valid(config);
}

Assert(typeof config === 'object', 'Invalid schema content:', typeof config);
Expand All @@ -59,7 +55,13 @@ internals.schema = function (Joi, config) {
}

if (Array.isArray(config)) {
return Joi.alternatives().try(config);
for (const valid of config) {
if (!internals.simple(valid)) {
return Joi.alternatives().try(config);
}
}

return Joi.valid(...config);
}

if (config instanceof RegExp) {
Expand Down Expand Up @@ -146,3 +148,9 @@ internals.walk = function (schema) {

return null;
};


internals.simple = function (value) {

return value === null || ['boolean', 'string', 'number'].includes(typeof value);
};
10 changes: 5 additions & 5 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ describe('any', () => {
flags: {
only: true,
empty: {
type: 'string',
type: 'any',
flags: { only: true },
allow: ['', ' ']
},
Expand Down Expand Up @@ -491,7 +491,7 @@ describe('any', () => {
flags: {
only: true,
empty: {
type: 'string',
type: 'any',
flags: { only: true },
allow: ['']
},
Expand Down Expand Up @@ -530,7 +530,7 @@ describe('any', () => {
flags: {
only: true,
empty: {
type: 'string',
type: 'any',
flags: { only: true },
allow: ['']
},
Expand Down Expand Up @@ -2150,7 +2150,7 @@ describe('any', () => {
matches: [{
ref: { path: ['a'] },
is: {
type: 'number',
type: 'any',
flags: {
only: true,
presence: 'required'
Expand Down Expand Up @@ -3082,7 +3082,7 @@ describe('any', () => {
matches: [{
ref: { path: ['a'] },
is: {
type: 'number',
type: 'any',
flags: {
only: true,
presence: 'required'
Expand Down
42 changes: 42 additions & 0 deletions test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,48 @@ describe('cast', () => {
}]
]);
});

it('compile [null]', () => {

const schema = Joi.compile([null]);
expect(schema).to.equal(Joi.valid(null));
});

it('compile [1]', () => {

const schema = Joi.compile([1]);
expect(schema).to.equal(Joi.valid(1));
});

it('compile ["a"]', () => {

const schema = Joi.compile(['a']);
expect(schema).to.equal(Joi.valid('a'));
});

it('compile [null, null, null]', () => {

const schema = Joi.compile([null]);
expect(schema).to.equal(Joi.valid(null));
});

it('compile [1, 2, 3]', () => {

const schema = Joi.compile([1, 2, 3]);
expect(schema).to.equal(Joi.valid(1, 2, 3));
});

it('compile ["a", "b", "c"]', () => {

const schema = Joi.compile(['a','b','c']);
expect(schema).to.equal(Joi.valid('a', 'b', 'c'));
});

it('compile [null, "a", 1, true]', () => {

const schema = Joi.compile([null, 'a', 1, true]);
expect(schema).to.equal(Joi.valid(null, 'a', 1, true));
});
});

describe('compile()', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,7 @@ describe('Joi', () => {
},
keys: {
foo: {
type: 'string',
type: 'any',
flags: {
description: 'defaulted',
presence: 'required',
Expand Down Expand Up @@ -1417,7 +1417,7 @@ describe('Joi', () => {
},
keys: {
foo: {
type: 'string',
type: 'any',
flags: {
presence: 'required',
description: 'defaulted',
Expand All @@ -1433,7 +1433,7 @@ describe('Joi', () => {
description: 'defaulted2',
presence: 'required'
},
type: 'string',
type: 'any',
allow: ['zorg']
}
},
Expand Down Expand Up @@ -1466,7 +1466,7 @@ describe('Joi', () => {
},
keys: {
foo: {
type: 'string',
type: 'any',
flags: {
description: 'defaulted',
presence: 'required',
Expand Down
2 changes: 1 addition & 1 deletion test/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('Manifest', () => {
type: 'string',
flags: {
empty: {
type: 'string',
type: 'any',
flags: {
only: true
},
Expand Down
36 changes: 18 additions & 18 deletions test/types/alternatives.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ describe('alternatives', () => {
{
ref: { path: ['b'] },
is: {
type: 'number',
type: 'any',
flags: {
only: true,
presence: 'required'
},
allow: [5]
},
then: {
type: 'string',
type: 'any',
flags: {
only: true
},
Expand All @@ -127,15 +127,15 @@ describe('alternatives', () => {
{
ref: { path: ['b'] },
is: {
type: 'number',
type: 'any',
flags: {
only: true,
presence: 'required'
},
allow: [6]
},
otherwise: {
type: 'string',
type: 'any',
flags: {
only: true
},
Expand All @@ -144,7 +144,7 @@ describe('alternatives', () => {
},
{
schema: {
type: 'string',
type: 'any',
flags: {
only: true
},
Expand Down Expand Up @@ -178,15 +178,15 @@ describe('alternatives', () => {
{
ref: { path: ['b'] },
is: {
type: 'number',
type: 'any',
flags: {
only: true,
presence: 'required'
},
allow: [5]
},
then: {
type: 'string',
type: 'any',
flags: {
only: true
},
Expand All @@ -195,7 +195,7 @@ describe('alternatives', () => {
},
{
schema: {
type: 'string',
type: 'any',
flags: {
only: true
},
Expand Down Expand Up @@ -229,15 +229,15 @@ describe('alternatives', () => {
{
ref: { path: ['b'] },
is: {
type: 'number',
type: 'any',
flags: {
only: true,
presence: 'required'
},
allow: [5]
},
otherwise: {
type: 'string',
type: 'any',
flags: {
only: true
},
Expand All @@ -246,7 +246,7 @@ describe('alternatives', () => {
},
{
schema: {
type: 'string',
type: 'any',
flags: {
only: true
},
Expand Down Expand Up @@ -312,7 +312,7 @@ describe('alternatives', () => {
},
matches: [{
schema: {
type: 'string',
type: 'any',
flags: {
only: true
},
Expand Down Expand Up @@ -440,7 +440,7 @@ describe('alternatives', () => {
type: 'alternatives',
matches: [{
is: {
type: 'boolean',
type: 'any',
flags: { only: true, presence: 'required' },
allow: [true]
},
Expand All @@ -450,7 +450,7 @@ describe('alternatives', () => {
flags: {
empty: {
flags: { only: true },
type: 'string',
type: 'any',
allow: ['']
}
},
Expand All @@ -467,7 +467,7 @@ describe('alternatives', () => {
type: 'alternatives',
matches: [{
is: {
type: 'boolean',
type: 'any',
flags: { only: true, presence: 'required' },
allow: [true]
},
Expand All @@ -478,7 +478,7 @@ describe('alternatives', () => {
label: 'Label b',
empty: {
flags: { only: true },
type: 'string',
type: 'any',
allow: ['']
}
},
Expand Down Expand Up @@ -515,7 +515,7 @@ describe('alternatives', () => {
matches: [
{
is: {
type: 'boolean',
type: 'any',
allow: [true],
flags: {
only: true,
Expand Down Expand Up @@ -561,7 +561,7 @@ describe('alternatives', () => {
matches: [
{
is: {
type: 'boolean',
type: 'any',
allow: [true],
flags: {
only: true,
Expand Down
4 changes: 2 additions & 2 deletions test/types/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('object', () => {
it('retains skipped values', () => {

const schema = Joi.object({ b: 5 }).unknown(true);
expect(schema.validate({ b: '5', a: 5 })).to.equal({ value: { a: 5, b: 5 } });
expect(schema.validate({ b: 5, a: 5 })).to.equal({ value: { a: 5, b: 5 } });
});

it('retains symbols', () => {
Expand Down Expand Up @@ -1660,7 +1660,7 @@ describe('object', () => {

it('overrides existing keys', () => {

const a = Joi.object({ a: 1 });
const a = Joi.object({ a: Joi.number().valid(1) });
const b = a.keys({ a: Joi.string() });

Helper.validate(a, [
Expand Down

0 comments on commit 5cfde5b

Please sign in to comment.