Skip to content

Commit

Permalink
Cleanup for #998.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Oct 22, 2016
1 parent 2eed6a8 commit 3d31dc4
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 160 deletions.
27 changes: 10 additions & 17 deletions lib/boolean.js
Expand Up @@ -18,8 +18,8 @@ internals.Boolean = class extends Any {

super();
this._type = 'boolean';
this._inner._truthySet = new internals.Set();
this._inner._falsySet = new internals.Set();
this._inner.truthySet = new internals.Set();
this._inner.falsySet = new internals.Set();
}

_base(value, state, options) {
Expand All @@ -28,8 +28,8 @@ internals.Boolean = class extends Any {
value
};

result.value = (this._inner._truthySet.has(value) ? true
: (this._inner._falsySet.has(value) ? false : value));
result.value = (this._inner.truthySet.has(value) ? true
: (this._inner.falsySet.has(value) ? false : value));

result.errors = (typeof result.value === 'boolean') ? null : this.createError('boolean.base', null, state, options);
return result;
Expand All @@ -42,8 +42,8 @@ internals.Boolean = class extends Any {
for (let i = 0; i < values.length; ++i) {
const value = values[i];

Hoek.assert(value !== undefined, 'Cannot call truthy/falsy with undefined');
obj._inner._truthySet.add(value);
Hoek.assert(value !== undefined, 'Cannot call truthy with undefined');
obj._inner.truthySet.add(value);
}
return obj;
}
Expand All @@ -55,24 +55,17 @@ internals.Boolean = class extends Any {
for (let i = 0; i < values.length; ++i) {
const value = values[i];

Hoek.assert(value !== undefined, 'Cannot call truthy/falsy with undefined');
obj._inner._falsySet.add(value);
Hoek.assert(value !== undefined, 'Cannot call falsy with undefined');
obj._inner.falsySet.add(value);
}
return obj;
}

describe() {

const description = Any.prototype.describe.call(this);

if (this._inner._truthySet.values().length) {
description.truthyValues = this._inner._truthySet.values();
}

if (this._inner._falsySet.values().length) {
description.falsyValues = this._inner._falsySet.values();
}

description.truthy = [true].concat(this._inner.truthySet.values());
description.falsy = [false].concat(this._inner.falsySet.values());
return description;
}
};
Expand Down
4 changes: 4 additions & 0 deletions lib/set.js
Expand Up @@ -21,6 +21,7 @@ module.exports = class Set {
}

this._set.push(value);
return this;
}

merge(add, remove) {
Expand All @@ -32,11 +33,14 @@ module.exports = class Set {
for (let i = 0; i < remove._set.length; ++i) {
this.remove(remove._set[i]);
}

return this;
}

remove(value) {

this._set = this._set.filter((item) => value !== item);
return this;
}

has(value, state, options, insensitive) {
Expand Down
196 changes: 58 additions & 138 deletions test/any.js
Expand Up @@ -1674,178 +1674,98 @@ describe('any', () => {
});
});

describe('Set', () => {
describe('allow()', () => {

describe('has()', () => {
it('allows valid values to be set', (done) => {

it('compares date to null', (done) => {

const any = Joi.any().clone();
any._valids.add(null);
expect(any._valids.has(new Date())).to.equal(false);
done();
});

it('compares buffer to null', (done) => {

const any = Joi.any().clone();
any._valids.add(null);
expect(any._valids.has(new Buffer(''))).to.equal(false);
done();
});
expect(() => {

Joi.any().allow(true, 1, 'hello', new Date());
}).not.to.throw();
done();
});

describe('values()', () => {
it('throws when passed undefined', (done) => {

it('returns array', (done) => {

const a = Joi.any().valid('x').invalid('y');
const b = a.invalid('x');
expect(a._valids.values().length).to.equal(1);
expect(b._valids.values().length).to.equal(0);
expect(a._invalids.values().length).to.equal(1);
expect(b._invalids.values().length).to.equal(2);
done();
});

it('strips undefined', (done) => {
expect(() => {

const any = Joi.any().clone();
any._valids.add(undefined);
expect(any._valids.values({ stripUndefined: true })).to.not.include(undefined);
done();
});
Joi.any().allow(undefined);
}).to.throw(Error, 'Cannot call allow/valid/invalid with undefined');
done();
});
});

describe('allow()', () => {

it('allows valid values to be set', (done) => {
describe('valid()', () => {

expect(() => {
it('allows valid values to be set', (done) => {

Joi.any().allow(true, 1, 'hello', new Date());
}).not.to.throw();
done();
});

it('throws when passed undefined', (done) => {

expect(() => {
expect(() => {

Joi.any().allow(undefined);
}).to.throw(Error, 'Cannot call allow/valid/invalid with undefined');
done();
});
Joi.any().valid(true, 1, 'hello', new Date(), Symbol('foo'), () => {}, {});
}).not.to.throw();
done();
});

describe('valid()', () => {

it('allows valid values to be set', (done) => {
it('throws when passed undefined', (done) => {

expect(() => {

Joi.any().valid(true, 1, 'hello', new Date(), Symbol('foo'), () => {}, {});
}).not.to.throw();
done();
});

it('throws when passed undefined', (done) => {

expect(() => {
expect(() => {

Joi.any().valid(undefined);
}).to.throw(Error, 'Cannot call allow/valid/invalid with undefined');
done();
});
Joi.any().valid(undefined);
}).to.throw(Error, 'Cannot call allow/valid/invalid with undefined');
done();
});

it('validates differents types of values', (done) => {
it('validates differents types of values', (done) => {

expect(Joi.valid(1).validate(1).error).to.be.null();
expect(Joi.valid(1).validate(2).error).to.exist();
expect(Joi.valid(1).validate(1).error).to.be.null();
expect(Joi.valid(1).validate(2).error).to.exist();

const d = new Date();
expect(Joi.valid(d).validate(new Date(d.getTime())).error).to.be.null();
expect(Joi.valid(d).validate(new Date(d.getTime() + 1)).error).to.exist();
const d = new Date();
expect(Joi.valid(d).validate(new Date(d.getTime())).error).to.be.null();
expect(Joi.valid(d).validate(new Date(d.getTime() + 1)).error).to.exist();

const str = 'foo';
expect(Joi.valid(str).validate(str).error).to.be.null();
expect(Joi.valid(str).validate('foobar').error).to.exist();
const str = 'foo';
expect(Joi.valid(str).validate(str).error).to.be.null();
expect(Joi.valid(str).validate('foobar').error).to.exist();

const s = Symbol('foo');
expect(Joi.valid(s).validate(s).error).to.be.null();
expect(Joi.valid(s).validate(Symbol('foo')).error).to.exist();
const s = Symbol('foo');
expect(Joi.valid(s).validate(s).error).to.be.null();
expect(Joi.valid(s).validate(Symbol('foo')).error).to.exist();

const o = {};
expect(Joi.valid(o).validate(o).error).to.be.null();
expect(Joi.valid(o).validate({}).error).to.exist();
const o = {};
expect(Joi.valid(o).validate(o).error).to.be.null();
expect(Joi.valid(o).validate({}).error).to.exist();

const f = () => {};
expect(Joi.valid(f).validate(f).error).to.be.null();
expect(Joi.valid(f).validate(() => {}).error).to.exist();
const f = () => {};
expect(Joi.valid(f).validate(f).error).to.be.null();
expect(Joi.valid(f).validate(() => {}).error).to.exist();

const b = new Buffer('foo');
expect(Joi.valid(b).validate(b).error).to.be.null();
expect(Joi.valid(b).validate(new Buffer('foobar')).error).to.exist();
const b = new Buffer('foo');
expect(Joi.valid(b).validate(b).error).to.be.null();
expect(Joi.valid(b).validate(new Buffer('foobar')).error).to.exist();

done();
});
done();
});
});

describe('invalid()', () => {

it('allows invalid values to be set', (done) => {

expect(() => {

Joi.any().valid(true, 1, 'hello', new Date());
}).not.to.throw();
done();
});

it('throws when passed undefined', (done) => {

expect(() => {

Joi.any().invalid(undefined);
}).to.throw('Cannot call allow/valid/invalid with undefined');
done();
});
});
describe('invalid()', () => {

describe('slice', () => {
it('allows invalid values to be set', (done) => {

it('returns a new Set', (done) => {
expect(() => {

const any = Joi.any().clone();
any._valids.add(null);
const otherValids = any._valids.slice();
otherValids.add('null');
expect(any._valids.has(null)).to.equal(true);
expect(otherValids.has(null)).to.equal(true);
expect(any._valids.has('null')).to.equal(false);
expect(otherValids.has('null')).to.equal(true);
done();
});
Joi.any().valid(true, 1, 'hello', new Date(), Symbol('foo'));
}).not.to.throw();
done();
});

describe('concat', () => {
it('throws when passed undefined', (done) => {

it('merges _set into a new Set', (done) => {
expect(() => {

const any = Joi.any().clone();
const otherValids = any._valids.slice();
any._valids.add(null);
otherValids.add('null');
const thirdSet = otherValids.concat(any._valids);
expect(any._valids.has(null)).to.equal(true);
expect(otherValids.has(null)).to.equal(false);
expect(any._valids.has('null')).to.equal(false);
expect(otherValids.has('null')).to.equal(true);
expect(thirdSet.has(null)).to.equal(true);
expect(thirdSet.has('null')).to.equal(true);
done();
});
Joi.any().invalid(undefined);
}).to.throw('Cannot call allow/valid/invalid with undefined');
done();
});
});
});
12 changes: 10 additions & 2 deletions test/array.js
Expand Up @@ -642,8 +642,16 @@ describe('array', () => {
expect(desc).to.equal({
type: 'array',
flags: { sparse: false },
orderedItems: [{ type: 'number', invalids: [Infinity, -Infinity] }, { type: 'string', invalids: [''] }, { type: 'string', invalids: [''], flags: { presence: 'required' } }],
items: [{ type: 'number', invalids: [Infinity, -Infinity] }, { type: 'string', invalids: [''] }, { type: 'boolean', flags: { presence: 'forbidden' } }]
orderedItems: [
{ type: 'number', invalids: [Infinity, -Infinity] },
{ type: 'string', invalids: [''] },
{ type: 'string', invalids: [''], flags: { presence: 'required' } }
],
items: [
{ type: 'number', invalids: [Infinity, -Infinity] },
{ type: 'string', invalids: [''] },
{ type: 'boolean', flags: { presence: 'forbidden' }, truthy: [true], falsy: [false] }
]
});

done();
Expand Down
4 changes: 2 additions & 2 deletions test/boolean.js
Expand Up @@ -227,8 +227,8 @@ describe('boolean', () => {
flags: {
presence: 'required'
},
truthyValues: ['yes'],
falsyValues : ['no']
truthy: [true, 'yes'],
falsy : [false, 'no']
});
done();
});
Expand Down
4 changes: 3 additions & 1 deletion test/object.js
Expand Up @@ -849,7 +849,9 @@ describe('object', () => {
{
regex: '/\\w\\d/i',
rule: {
type: 'boolean'
type: 'boolean',
truthy: [true],
falsy: [false]
}
}
]
Expand Down

0 comments on commit 3d31dc4

Please sign in to comment.