Skip to content

Commit

Permalink
Fixes any.valid with insensitive. Fixes #1191.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Aug 14, 2018
1 parent f225d91 commit 67ae968
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
25 changes: 18 additions & 7 deletions lib/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,29 @@ module.exports = class InternalSet {

has(value, state, options, insensitive) {

return !!this.get(value, state, options, insensitive);
}

get(value, state, options, insensitive) {

if (!this._set.size) {
return false;
}

const hasValue = this._set.has(value);
if (hasValue) {
return hasValue;
return { value };
}

const extendedCheck = internals.extendedCheckForValue(value, insensitive);
if (!extendedCheck) {
if (state && this._hasRef) {
for (let item of this._set) {
if (Ref.isRef(item)) {
item = item(state.reference || state.parent, options);
if (value === item || (Array.isArray(item) && item.includes(value))) {
return true;
item = [].concat(item(state.reference || state.parent, options));
const found = item.indexOf(value);
if (found >= 0) {
return { value: item[found] };
}
}
}
Expand Down Expand Up @@ -130,15 +136,20 @@ module.exports = class InternalSet {
item = item(state.reference || state.parent, options);

if (Array.isArray(item)) {
if (item.find(isReallyEqual)) {
return true;
const found = item.findIndex(isReallyEqual);
if (found >= 0) {
return {
value: item[found]
};
}
continue;
}
}

if (isReallyEqual(item)) {
return true;
return {
value: item
};
}
}

Expand Down
10 changes: 8 additions & 2 deletions lib/types/any/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,11 @@ module.exports = internals.Any = class {

// Check allowed and denied values using the original value

if (this._valids.has(value, state, options, this._flags.insensitive)) {
let match = this._valids.get(value, state, options, this._flags.insensitive);
if (match) {
if (options.convert) {
value = match.value;
}
return finish();
}

Expand Down Expand Up @@ -656,7 +660,9 @@ module.exports = internals.Any = class {

// Check allowed and denied values using the converted value

if (this._valids.has(value, state, options, this._flags.insensitive)) {
match = this._valids.get(value, state, options, this._flags.insensitive);
if (match) {
value = match.value;
return finish();
}

Expand Down
4 changes: 2 additions & 2 deletions test/types/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -1558,8 +1558,8 @@ describe('any', () => {
]);

Helper.validate(a.concat(b), [
['a', true],
['A', true],
['a', true, null, 'a'],
['A', true, null, 'a'],
['b', false, null, {
message: '"value" must be one of [a]',
details: [{
Expand Down
1 change: 1 addition & 0 deletions test/types/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ describe('date', () => {

const now = Date.now();
await Joi.date().valid(new Date(now)).validate(new Date(now));
await Joi.date().valid(new Date(now)).validate(new Date(now).toISOString());
});

it('errors on invalid input and convert disabled', async () => {
Expand Down
15 changes: 15 additions & 0 deletions test/types/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ describe('string', () => {
const schema = Joi.string().insensitive();
expect(schema.insensitive()).to.shallow.equal(schema);
});

it('sets right values with valid', () => {

const simpleSchema = Joi.string().insensitive().valid('A');
expect(simpleSchema.validate('a').value).to.equal('A');

const refSchema = Joi.string().insensitive().valid(Joi.ref('$v'));
expect(refSchema.validate('a', { context: { v: 'A' } }).value).to.equal('A');

const refArraySchema = Joi.string().insensitive().valid(Joi.ref('$v'));
expect(refArraySchema.validate('a', { context: { v: ['B', 'A'] } }).value).to.equal('A');

const strictSchema = Joi.string().insensitive().valid('A').strict();
expect(strictSchema.validate('a').value).to.equal('a');
});
});

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

0 comments on commit 67ae968

Please sign in to comment.