Skip to content
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| inferStringEnumKeysFromValues | boolean | `false` | Create enums from JSON enums with eponymous keys |
| format | boolean | `true` | Format code? Set this to `false` to improve performance. |
| ignoreMinAndMaxItems | boolean | `false` | Ignore maxItems and minItems for `array` types, preventing tuples being generated. |
| maxItems | number | `20` | Maximum number of unioned tuples to emit when representing bounded-size array types, before falling back to emitting unbounded arrays. Increase this to improve precision of emitted types, decrease it to improve performance, or set it to `-1` to ignore `maxItems`.
| maxItems | number | `20` | Maximum number of unioned tuples to emit when representing bounded-size array types, before falling back to emitting unbounded arrays. Increase this to improve precision of emitted types, decrease it to improve performance, or set it to `-1` to ignore `maxItems`. |
| removeOptionalIfDefaultExists | boolean | `false` | Remove the optional modifier when a property has a default value. |
| strictIndexSignatures | boolean | `false` | Append all index signatures with `\| undefined` so that they are strictly typed. |
| 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 |
| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"test": "npm run pre-test && ava --timeout=300s --verbose",
"stresstest": "seq 1 10 | xargs -I{} npm test",
"prepublishOnly": "npm test",
"prepare": "npm run build",
"pre-test": "npm run clean && npm run format-check && npm run build:server",
"watch": "tsc -w",
"watch:test": "ava -w"
Expand Down
3 changes: 3 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ main(
'enableConstEnums',
'format',
'ignoreMinAndMaxItems',
'removeOptionalIfDefaultExists',
'strictIndexSignatures',
'unknownAny',
'unreachableDefinitions',
Expand Down Expand Up @@ -190,6 +191,8 @@ Boolean values can be set to false using the 'no-' prefix.
array types, before falling back to emitting unbounded arrays. Increase
this to improve precision of emitted types, decrease it to improve
performance, or set it to -1 to ignore minItems and maxItems.
--removeOptionalIfDefaultExists
Remove the optional modifier when a property has a default value
--style.XXX=YYY
Prettier configuration
--unknownAny
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export interface Options {
* `minItems` and `maxItems`.
*/
maxItems: number
/**
* Remove the optional modifier when a property has a default value.
*/
removeOptionalIfDefaultExists: boolean
/**
* Append all index signatures with `| undefined` so that they are strictly typed.
*
Expand Down Expand Up @@ -103,6 +107,7 @@ export const DEFAULT_OPTIONS: Options = {
format: true,
ignoreMinAndMaxItems: false,
maxItems: 20,
removeOptionalIfDefaultExists: false,
strictIndexSignatures: false,
style: {
bracketSpacing: false,
Expand Down
21 changes: 21 additions & 0 deletions src/normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ rules.set('Remove maxItems if it is big enough to likely cause OOMs', (schema, _
}
})

// NOTE: https://json-schema.org/draft/2020-12#introduction
//
// Keywords for Applying Subschemas to Arrays
// https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#rfc.section.9.3.1
// https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1
rules.set('Support the `prefixItems` key from draft 2020-12', schema => {
if (!isArrayType(schema)) {
return
}

if (schema.hasOwnProperty('prefixItems')) {
if (schema.hasOwnProperty('items')) {
schema.additionalItems = schema.items as any
delete schema.items
}

schema.items = schema.prefixItems as any
delete schema.prefixItems
}
})

rules.set('Normalize schema.items', (schema, _fileName, options) => {
if (options.ignoreMinAndMaxItems) {
return
Expand Down
10 changes: 7 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ function parseSchema(
let asts: TInterfaceParam[] = map(schema.properties, (value, key: string) => ({
ast: parse(value, options, key, processed, usedNames),
isPatternProperty: false,
isRequired: includes(schema.required || [], key),
isRequired: includes(schema.required || [], key) || (options.removeOptionalIfDefaultExists && 'default' in value),
isUnreachableDefinition: false,
keyName: key,
}))
Expand All @@ -404,7 +404,10 @@ via the \`patternProperty\` "${key.replace('*/', '*\\/')}".`
return {
ast,
isPatternProperty: !singlePatternProperty,
isRequired: singlePatternProperty || includes(schema.required || [], key),
isRequired:
singlePatternProperty ||
includes(schema.required || [], key) ||
(options.removeOptionalIfDefaultExists && 'default' in value),
isUnreachableDefinition: false,
keyName: singlePatternProperty ? '[k: string]' : key,
}
Expand All @@ -422,7 +425,8 @@ via the \`definition\` "${key}".`
return {
ast,
isPatternProperty: false,
isRequired: includes(schema.required || [], key),
isRequired:
includes(schema.required || [], key) || (options.removeOptionalIfDefaultExists && 'default' in value),
isUnreachableDefinition: true,
keyName: key,
}
Expand Down
Loading