Skip to content

Commit

Permalink
fix: pick changes from #620
Browse files Browse the repository at this point in the history
  • Loading branch information
pateketrueke committed Dec 1, 2020
1 parent c5b54a4 commit 6e30bc8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/lib/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ function arrayType(value, path, resolve, traverseCallback) {

if (defaultMinItems) {
// fix boundaries
minItems = typeof maxItems === 'undefined'
minItems = typeof minItems === 'undefined'
? defaultMinItems
: Math.min(defaultMinItems, maxItems);
: Math.min(defaultMinItems, minItems);
}

if (defaultMaxItems) {
maxItems = typeof minItems === 'undefined'
maxItems = typeof maxItems === 'undefined'
? defaultMaxItems
: Math.min(defaultMaxItems, minItems);
: Math.min(defaultMaxItems, maxItems);

// Don't allow user to set max items above our maximum
if (maxItems && maxItems > defaultMaxItems) {
Expand Down
64 changes: 64 additions & 0 deletions tests/schema/core/issues/issue-620.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[
{
"description": "Fix again min and maxItem generation",
"tests": [
{
"description": "minItems is a valid boundary if set to zero",
"schema": {
"type": "array",
"items": {
"type": "integer",
"default": 1
},
"minItems": 0,
"maxItems": 5
},
"repeat": 100,
"valid": true,
"set": {
"minItems": 7,
"maxItems": 7,
"useDefaultValue": true
},
"lessThan": 6
},
{
"description": "ignore minItems and maxItems if already set",
"schema": {
"type": "array",
"items": {
"type": "integer",
"default": 1
},
"minItems": 3,
"maxItems": 5
},
"repeat": 100,
"valid": true,
"set": {
"maxItems": 7,
"useDefaultValue": true
},
"atLeast": 3
},
{
"description": "set minItems and/or maxItems if they are not defined",
"schema": {
"type": "array",
"items": {
"type": "integer",
"default": 1
}
},
"repeat": 100,
"valid": true,
"set": {
"minItems": 7,
"maxItems": 7,
"useDefaultValue": true
},
"length": 7
}
]
}
]
19 changes: 0 additions & 19 deletions tests/schema/core/types/array.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,6 @@
},
"equal": [1, 1, 1, 1, 1, 1, 1]
},
{
"description": "should handle minItems and maxItems passed as arguments to JSF, and minItems is 0",
"schema": {
"type": "array",
"items": {
"type": "integer",
"default": 1
},
"minItems": 0,
"maxItems": 5
},
"valid": true,
"set": {
"minItems": 7,
"maxItems": 7,
"useDefaultValue": true
},
"lessThan": 7
},
{
"description": "should handle uniqueItems",
"schema": {
Expand Down
4 changes: 4 additions & 0 deletions tests/schema/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ export function tryTest(nth, max, test, refs, schema) {
expect(sample.length).to.be.below(test.lessThan);
}

if (test.atLeast) {
expect(sample.length).to.be.above(test.atLeast - 1);
}

if (test.hasNot) {
expect(JSON.stringify(sample)).not.to.contain(test.hasNot);
}
Expand Down

0 comments on commit 6e30bc8

Please sign in to comment.