Skip to content

Commit

Permalink
Add validations for maximum and minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
bsonntag committed Nov 18, 2019
1 parent 00ea0ea commit ae0943a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/utils/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ function getErrorArgs(error) {
case 'maxItems':
case 'maxLength':
case 'maxProperties':
case 'maximum':
return { max: error.params.limit };

case 'minItems':
case 'minLength':
case 'minProperties':
case 'minimum':
return { min: error.params.limit };

default:
Expand Down
44 changes: 42 additions & 2 deletions test/src/utils/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('validate', () => {
});
});

it('should return errors with the `maxProperties` rule', () => {
it('should return errors with the `maxProperties` rule and `max` argument when there are too many properties', () => {
const result = validate({
properties: {
foo: {
Expand All @@ -172,7 +172,7 @@ describe('validate', () => {
});
});

it('should return errors with the `minProperties` rule', () => {
it('should return errors with the `minProperties` rule and `min` argument when there are not enough properties', () => {
const result = validate({
properties: {
foo: {
Expand All @@ -193,4 +193,44 @@ describe('validate', () => {
}
});
});

it('should return errors with the `maximum` rule and `max` argument when the value is too big', () => {
const result = validate({
properties: {
foo: {
maximum: 1,
type: 'number'
}
}
}, {
foo: 2
});

expect(result).toEqual({
foo: {
args: { max: 1 },
rule: 'maximum'
}
});
});

it('should return errors with the `minimum` rule and `min` argument when the value is too small', () => {
const result = validate({
properties: {
foo: {
minimum: 2,
type: 'number'
}
}
}, {
foo: 1
});

expect(result).toEqual({
foo: {
args: { min: 2 },
rule: 'minimum'
}
});
});
});

0 comments on commit ae0943a

Please sign in to comment.