Skip to content

Commit

Permalink
feat: support contains and maxItems array validations (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored and RomanHotsiy committed May 25, 2021
1 parent 0ee8148 commit 2bdba33
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Tool for generation samples based on OpenAPI payload/response schema
- Supports `allOf`
- Supports `additionalProperties`
- Uses `default`, `const`, `enum` and `examples` where possible
- Full array support: supports `minItems`, and tuples (`items` as an array)
- Good array support: supports `contains`, `minItems`, `maxItems`, and tuples (`items` as an array)
- Supports `minLength`, `maxLength`, `min`, `max`, `exclusiveMinimum`, `exclusiveMaximum`
- Supports the next `string` formats:
- email
Expand Down
14 changes: 8 additions & 6 deletions src/samplers/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import { traverse } from '../traverse';
export function sampleArray(schema, options = {}, spec, context) {
const depth = (context && context.depth || 1);

let arrayLength = schema.minItems || 1;
if (Array.isArray(schema.items)) {
arrayLength = Math.max(arrayLength, schema.items.length);
let arrayLength = Math.min(schema.maxItems ?? Infinity, schema.minItems || 1);
// for the sake of simplicity, we're treating `contains` in a similar way to `items`
const items = schema.items || schema.contains;
if (Array.isArray(items)) {
arrayLength = Math.max(arrayLength, items.length);
}

let itemSchemaGetter = itemNumber => {
if (Array.isArray(schema.items)) {
return schema.items[itemNumber] || {};
return items[itemNumber] || {};
}
return schema.items || {};
return items || {};
};

let res = [];
if (!schema.items) return res;
if (!items) return res;

for (let i = 0; i < arrayLength; i++) {
let itemSchema = itemSchemaGetter(i);
Expand Down
9 changes: 8 additions & 1 deletion test/unit/array.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ describe('sampleArray', () => {
it('should return elements of correct type', () => {
res = sampleArray({items: {type: 'number'}});
expect(res).to.deep.equal([0]);
res = sampleArray({contains: {type: 'number'}});
expect(res).to.deep.equal([0]);
});

it('should return correct number of elements based on maxItems', () => {
res = sampleArray({items: {type: 'number'}, maxItems: 0});
expect(res).to.deep.equal([]);
});

it('should return correct number of elements based on minItems', () => {
res = sampleArray({items: {type: 'number'}, minItems: 3});
expect(res).to.deep.equal([0, 0, 0]);
});

it('should correcly sample tuples', () => {
it('should correctly sample tuples', () => {
res = sampleArray({items: [{type: 'number'}, {type: 'string'}, {}]});
expect(res).to.deep.equal([0, 'string', null]);
});
Expand Down

0 comments on commit 2bdba33

Please sign in to comment.