Skip to content

Commit

Permalink
Merge pull request #839 from BitGo/DX-602-min-max-in-array
Browse files Browse the repository at this point in the history
feat: support `minItem` and `maxItem` for array types
  • Loading branch information
anshchaturvedi authored Jul 19, 2024
2 parents 497dc9f + 179bf7b commit e3ad312
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/openapi-generator/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ function schemaToOpenAPI(
return undefined;
}

const { arrayExample, ...rest } = defaultOpenAPIObject;
const { arrayExample, minItems, maxItems, ...rest } = defaultOpenAPIObject;

return {
type: 'array',
...(minItems ? { minItems } : {}),
...(maxItems ? { maxItems } : {}),
...(arrayExample ? { example: JSON.parse(arrayExample) } : {}), // Add example to array if it exists
items: { ...innerSchema, ...rest },
};
Expand Down
15 changes: 14 additions & 1 deletion packages/openapi-generator/test/openapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3588,6 +3588,11 @@ export const route = h.httpRoute({
* @arrayExample ["btc", "eth"]
*/
array2: t.array(t.string),
/**
* @minItems 1
* @maxItems 5
*/
array3: t.array(t.number),
objectWithArray: t.type({
/**
* @arrayExample ["btc", "eth"]
Expand Down Expand Up @@ -3632,6 +3637,14 @@ testCase("route with array examples", ROUTE_WITH_ARRAY_EXAMPLE, {
example: '"btc"'
},
},
array3: {
items: {
type: 'number'
},
maxItems: 5,
minItems: 1,
type: 'array'
},
objectWithArray: {
properties: {
nestedArray: {
Expand All @@ -3651,7 +3664,7 @@ testCase("route with array examples", ROUTE_WITH_ARRAY_EXAMPLE, {
type: 'object'
},
},
required: [ 'array1', 'array2', 'objectWithArray' ],
required: [ 'array1', 'array2', 'array3', 'objectWithArray' ],
},
}
}
Expand Down

0 comments on commit e3ad312

Please sign in to comment.