Skip to content

Commit

Permalink
Allow array min/max/length overrides. Fixes #1127.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsup committed Aug 8, 2018
1 parent 96d02a3 commit 674a59e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/types/any/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ module.exports = internals.Any = class {
return obj;
}

_testUnique(name, arg, func, options) {

const obj = this.clone();
obj._tests = obj._tests.filter((test) => test.name !== name);
obj._tests.push({ func, name, arg, options });
return obj;
}

options(options) {

Hoek.assert(!options.context, 'Cannot override context');
Expand Down
6 changes: 3 additions & 3 deletions lib/types/array/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ internals.Array = class extends Any {

Hoek.assert((Number.isSafeInteger(limit) && limit >= 0) || isRef, 'limit must be a positive integer or reference');

return this._test('min', limit, function (value, state, options) {
return this._testUnique('min', limit, function (value, state, options) {

let compareTo;
if (isRef) {
Expand Down Expand Up @@ -427,7 +427,7 @@ internals.Array = class extends Any {

Hoek.assert((Number.isSafeInteger(limit) && limit >= 0) || isRef, 'limit must be a positive integer or reference');

return this._test('max', limit, function (value, state, options) {
return this._testUnique('max', limit, function (value, state, options) {

let compareTo;
if (isRef) {
Expand Down Expand Up @@ -455,7 +455,7 @@ internals.Array = class extends Any {

Hoek.assert((Number.isSafeInteger(limit) && limit >= 0) || isRef, 'limit must be a positive integer or reference');

return this._test('length', limit, function (value, state, options) {
return this._testUnique('length', limit, function (value, state, options) {

let compareTo;
if (isRef) {
Expand Down
35 changes: 35 additions & 0 deletions test/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ describe('array', () => {
]);
});

it('overrides rule when called multiple times', () => {

const schema = Joi.array().min(2).min(1);
Helper.validate(schema, [
[[1, 2], true],
[[1], true]
]);
});

it('throws when limit is not a number', () => {

expect(() => {
Expand Down Expand Up @@ -481,6 +490,15 @@ describe('array', () => {
]);
});

it('overrides rule when called multiple times', () => {

const schema = Joi.array().max(1).max(2);
Helper.validate(schema, [
[[1, 2], true],
[[1], true]
]);
});

it('throws when limit is not a number', () => {

expect(() => {
Expand Down Expand Up @@ -607,6 +625,23 @@ describe('array', () => {
]);
});

it('overrides rule when called multiple times', () => {

const schema = Joi.array().length(2).length(1);
Helper.validate(schema, [
[[1], true],
[[1, 2], false, null, {
message: '"value" must contain 1 items',
details: [{
message: '"value" must contain 1 items',
path: [],
type: 'array.length',
context: { limit: 1, value: [1, 2], label: 'value', key: undefined }
}]
}]
]);
});

it('throws when limit is not a number', () => {

expect(() => {
Expand Down

0 comments on commit 674a59e

Please sign in to comment.