Skip to content

Added ignoreMinAndMaxItems option #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| cwd | string | `process.cwd()` | Root directory for resolving [`$ref`](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html)s |
| declareExternallyReferenced | boolean | `true` | Declare external schemas referenced via `$ref`? |
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
| ignoreMinAndMaxItems | boolean | `false` | Ignore maxItems and minItems for `array` types, preventing tuples being generated. |
| style | object | `{ bracketSpacing: false, printWidth: 120, semi: true, singleQuote: false, tabWidth: 2, trailingComma: 'none', useTabs: false }` | A [Prettier](https://prettier.io/docs/en/options.html) configuration |
| unreachableDefinitions | boolean | `false` | Generates code for `definitions` that aren't referenced by the schema. |
| strictIndexSignatures | boolean | `false` | Append all index signatures with `| undefined` so that they are strictly typed. |
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export interface Options {
* Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)?
*/
enableConstEnums: boolean
/**
* Ignore maxItems and minItems for `array` types, preventing tuples being generated.
*/
ignoreMinAndMaxItems: boolean
/**
* Append all index signatures with `| undefined` so that they are strictly typed.
*
Expand Down Expand Up @@ -63,6 +67,7 @@ export const DEFAULT_OPTIONS: Options = {
cwd: process.cwd(),
declareExternallyReferenced: true,
enableConstEnums: true, // by default, avoid generating code
ignoreMinAndMaxItems: false,
strictIndexSignatures: false,
style: {
bracketSpacing: false,
Expand Down Expand Up @@ -107,7 +112,7 @@ export async function compile(schema: JSONSchema4, name: string, options: Partia
}

return format(
generate(optimize(parse(await dereference(normalize(schema, name), _options), _options)), _options),
generate(optimize(parse(await dereference(normalize(schema, name, _options), _options), _options)), _options),
_options
)
}
Expand Down
28 changes: 23 additions & 5 deletions src/normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import stringify = require('json-stringify-safe')
import {cloneDeep} from 'lodash'
import {JSONSchema, JSONSchemaTypeName, NormalizedJSONSchema} from './types/JSONSchema'
import {escapeBlockComment, justName, log, toSafeString, traverse} from './utils'
import {Options} from './'

type Rule = (schema: JSONSchema, rootSchema: JSONSchema, fileName?: string) => void
type Rule = (schema: JSONSchema, rootSchema: JSONSchema, fileName: string, options: Options) => void
const rules = new Map<string, Rule>()

function hasType(schema: JSONSchema, type: JSONSchemaTypeName) {
Expand Down Expand Up @@ -63,7 +64,21 @@ rules.set('Escape closing JSDoc Comment', schema => {
escapeBlockComment(schema)
})

rules.set('Normalise schema.minItems', schema => {
rules.set('Optionally remove maxItems and minItems', (schema, _rootSchema, _fileName, options) => {
if (options.ignoreMinAndMaxItems) {
if ('maxItems' in schema) {
delete schema.maxItems
}
if ('minItems' in schema) {
delete schema.minItems
}
}
})

rules.set('Normalise schema.minItems', (schema, _rootSchema, _fileName, options) => {
if (options.ignoreMinAndMaxItems) {
return
}
// make sure we only add the props onto array types
if (isArrayType(schema)) {
const {minItems} = schema
Expand All @@ -72,7 +87,10 @@ rules.set('Normalise schema.minItems', schema => {
// cannot normalise maxItems because maxItems = 0 has an actual meaning
})

rules.set('Normalize schema.items', schema => {
rules.set('Normalize schema.items', (schema, _rootSchema, _fileName, options) => {
if (options.ignoreMinAndMaxItems) {
return
}
const {maxItems, minItems} = schema
const hasMaxItems = typeof maxItems === 'number' && maxItems >= 0
const hasMinItems = typeof minItems === 'number' && minItems > 0
Expand All @@ -97,10 +115,10 @@ rules.set('Normalize schema.items', schema => {
return schema
})

export function normalize(schema: JSONSchema, filename?: string): NormalizedJSONSchema {
export function normalize(schema: JSONSchema, filename: string, options: Options): NormalizedJSONSchema {
const _schema = cloneDeep(schema) as NormalizedJSONSchema
rules.forEach((rule, key) => {
traverse(_schema, schema => rule(schema, _schema, filename))
traverse(_schema, schema => rule(schema, _schema, filename, options))
log(whiteBright.bgYellow('normalizer'), `Applied rule: "${key}"`)
})
return _schema
Expand Down
256 changes: 256 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,153 @@ Generated by [AVA](https://ava.li).
}␊
`

## Normalise ignoreMinAndMaxItems

> Snapshot 1

`{␊
"id": "foo",␊
"type": "object",␊
"properties": {␊
"untyped": {␊
"type": "object",␊
"properties": {␊
"unbounded": {␊
"type": "array"␊
},␊
"minOnly": {␊
"type": "array"␊
},␊
"maxOnly": {␊
"type": "array"␊
},␊
"minAndMax": {␊
"type": "array"␊
}␊
},␊
"additionalProperties": false,␊
"required": []␊
},␊
"untypedArray": {␊
"type": "object",␊
"properties": {␊
"unbounded": {␊
"type": [␊
"array",␊
"string"␊
]␊
},␊
"minOnly": {␊
"type": [␊
"array",␊
"string"␊
]␊
},␊
"maxOnly": {␊
"type": [␊
"array",␊
"string"␊
]␊
},␊
"minAndMax": {␊
"type": [␊
"array",␊
"string"␊
]␊
}␊
},␊
"additionalProperties": false,␊
"required": []␊
},␊
"typed": {␊
"type": "object",␊
"properties": {␊
"unbounded": {␊
"items": {␊
"type": "string"␊
}␊
},␊
"minOnly": {␊
"items": {␊
"type": "string"␊
},␊
"additionalItems": {␊
"type": "string"␊
}␊
},␊
"maxOnly": {␊
"items": {␊
"type": "string"␊
}␊
},␊
"minAndMax": {␊
"items": {␊
"type": "string"␊
}␊
}␊
},␊
"additionalProperties": false,␊
"required": []␊
},␊
"anyOf": {␊
"type": "object",␊
"properties": {␊
"unbounded": {␊
"anyOf": [␊
{␊
"items": {␊
"type": "string"␊
}␊
}␊
],␊
"additionalProperties": false,␊
"required": []␊
},␊
"minOnly": {␊
"anyOf": [␊
{␊
"items": {␊
"type": "string"␊
},␊
"additionalItems": {␊
"type": "string"␊
}␊
}␊
],␊
"additionalProperties": false,␊
"required": []␊
},␊
"maxOnly": {␊
"anyOf": [␊
{␊
"items": {␊
"type": "string"␊
}␊
}␊
],␊
"additionalProperties": false,␊
"required": []␊
},␊
"minAndMax": {␊
"anyOf": [␊
{␊
"items": {␊
"type": "string"␊
}␊
}␊
],␊
"additionalProperties": false,␊
"required": []␊
}␊
},␊
"additionalProperties": false,␊
"required": []␊
}␊
},␊
"additionalProperties": false,␊
"required": []␊
}`

## Normalise schema.minItems

> Snapshot 1
Expand Down Expand Up @@ -802,6 +949,115 @@ Generated by [AVA](https://ava.li).
}␊
`

## arrayIgnoreMaxMinItems.js

> Snapshot 1

`/* tslint:disable */␊
/**␊
* This file was automatically generated by json-schema-to-typescript.␊
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface ArrayMaxMinItems {␊
array?: {␊
/**␊
* minItems = 3␊
*/␊
withMinItems?: string[];␊
/**␊
* maxItems = 3␊
*/␊
withMaxItems?: string[];␊
/**␊
* minItems = 3, maxItems = 8␊
*/␊
withMinMaxItems?: string[];␊
/**␊
* maxItems = 0␊
*/␊
withMaxItems0?: string[];␊
/**␊
* minItems = 0␊
*/␊
withMinItems0?: string[];␊
/**␊
* minItems = 0, maxItems = 0␊
*/␊
withMinMaxItems0?: string[];␊
};␊
untyped?: {␊
/**␊
* minItems = 3␊
*/␊
withMinItems?: any[];␊
/**␊
* maxItems = 3␊
*/␊
withMaxItems?: any[];␊
/**␊
* minItems = 3, maxItems = 8␊
*/␊
withMinMaxItems?: any[];␊
/**␊
* maxItems = 0␊
*/␊
withMaxItems0?: any[];␊
/**␊
* minItems = 0␊
*/␊
withMinItems0?: any[];␊
/**␊
* minItems = 0, maxItems = 0␊
*/␊
withMinMaxItems0?: any[];␊
};␊
tuple?: {␊
/**␊
* minItems = 2␊
*/␊
withMinItemsLessThanItemLength?: [1, 2, 3, 4, 5, 6];␊
/**␊
* minItems = 8␊
*/␊
withMinItemsGreaterThanItemLength?: [1, 2, 3, 4, 5, 6];␊
/**␊
* maxItems = 2␊
*/␊
withMaxItemsLessThanItemLength?: [1, 2, 3, 4, 5, 6];␊
/**␊
* maxItems = 8␊
*/␊
withMaxItemsGreaterThanItemLength?: [1, 2, 3, 4, 5, 6];␊
/**␊
* minItems = 4, maxItems = 8␊
*/␊
withMinItemsLessThanItemLength_and_MaxItemsGreaterThanItemLength?: [1, 2, 3, 4, 5, 6];␊
/**␊
* minItems = 2, maxItems = 4␊
*/␊
withMinItemsLessThanItemLength_and_MaxItemsLessThanItemLength?: [1, 2, 3, 4, 5, 6];␊
/**␊
* minItems = 8, maxItems = 10␊
*/␊
withMinItemsGreaterThanItemLength_and_MaxItemsGreaterThanItemLength?: [1, 2, 3, 4, 5, 6];␊
/**␊
* maxItems = 0␊
*/␊
withMaxItems0?: [1, 2, 3, 4, 5, 6];␊
/**␊
* minItems = 0␊
*/␊
withMinItems0?: [1, 2, 3, 4, 5, 6];␊
/**␊
* minItems = 0, maxItems = 0␊
*/␊
withMinMaxItems0?: [1, 2, 3, 4, 5, 6];␊
};␊
}␊
`

## arrayMaxMinItems.js

> Snapshot 1
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
Loading