Skip to content

Commit 558a7e3

Browse files
authored
Merge pull request #1 from calleli/fix/bring-in-functionality-needed-from-open-prs
bring in functionality needed from two contributors
2 parents 43ba08b + 0a5bbb8 commit 558a7e3

File tree

9 files changed

+364
-277
lines changed

9 files changed

+364
-277
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
141141
| inferStringEnumKeysFromValues | boolean | `false` | Create enums from JSON enums with eponymous keys |
142142
| format | boolean | `true` | Format code? Set this to `false` to improve performance. |
143143
| ignoreMinAndMaxItems | boolean | `false` | Ignore maxItems and minItems for `array` types, preventing tuples being generated. |
144-
| 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`.
144+
| 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`. |
145+
| removeOptionalIfDefaultExists | boolean | `false` | Remove the optional modifier when a property has a default value. |
145146
| strictIndexSignatures | boolean | `false` | Append all index signatures with `\| undefined` so that they are strictly typed. |
146147
| 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 |
147148
| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"test": "npm run pre-test && ava --timeout=300s --verbose",
2323
"stresstest": "seq 1 10 | xargs -I{} npm test",
2424
"prepublishOnly": "npm test",
25+
"prepare": "npm run build",
2526
"pre-test": "npm run clean && npm run format-check && npm run build:server",
2627
"watch": "tsc -w",
2728
"watch:test": "ava -w"

src/cli.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ main(
2020
'enableConstEnums',
2121
'format',
2222
'ignoreMinAndMaxItems',
23+
'removeOptionalIfDefaultExists',
2324
'strictIndexSignatures',
2425
'unknownAny',
2526
'unreachableDefinitions',
@@ -190,6 +191,8 @@ Boolean values can be set to false using the 'no-' prefix.
190191
array types, before falling back to emitting unbounded arrays. Increase
191192
this to improve precision of emitted types, decrease it to improve
192193
performance, or set it to -1 to ignore minItems and maxItems.
194+
--removeOptionalIfDefaultExists
195+
Remove the optional modifier when a property has a default value
193196
--style.XXX=YYY
194197
Prettier configuration
195198
--unknownAny

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ export interface Options {
6767
* `minItems` and `maxItems`.
6868
*/
6969
maxItems: number
70+
/**
71+
* Remove the optional modifier when a property has a default value.
72+
*/
73+
removeOptionalIfDefaultExists: boolean
7074
/**
7175
* Append all index signatures with `| undefined` so that they are strictly typed.
7276
*
@@ -103,6 +107,7 @@ export const DEFAULT_OPTIONS: Options = {
103107
format: true,
104108
ignoreMinAndMaxItems: false,
105109
maxItems: 20,
110+
removeOptionalIfDefaultExists: false,
106111
strictIndexSignatures: false,
107112
style: {
108113
bracketSpacing: false,

src/normalizer.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@ rules.set('Remove maxItems if it is big enough to likely cause OOMs', (schema, _
161161
}
162162
})
163163

164+
// NOTE: https://json-schema.org/draft/2020-12#introduction
165+
//
166+
// Keywords for Applying Subschemas to Arrays
167+
// https://json-schema.org/draft/2019-09/draft-handrews-json-schema-02#rfc.section.9.3.1
168+
// https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1
169+
rules.set('Support the `prefixItems` key from draft 2020-12', schema => {
170+
if (!isArrayType(schema)) {
171+
return
172+
}
173+
174+
if (schema.hasOwnProperty('prefixItems')) {
175+
if (schema.hasOwnProperty('items')) {
176+
schema.additionalItems = schema.items as any
177+
delete schema.items
178+
}
179+
180+
schema.items = schema.prefixItems as any
181+
delete schema.prefixItems
182+
}
183+
})
184+
164185
rules.set('Normalize schema.items', (schema, _fileName, options) => {
165186
if (options.ignoreMinAndMaxItems) {
166187
return

src/parser.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ function parseSchema(
383383
let asts: TInterfaceParam[] = map(schema.properties, (value, key: string) => ({
384384
ast: parse(value, options, key, processed, usedNames),
385385
isPatternProperty: false,
386-
isRequired: includes(schema.required || [], key),
386+
isRequired: includes(schema.required || [], key) || (options.removeOptionalIfDefaultExists && 'default' in value),
387387
isUnreachableDefinition: false,
388388
keyName: key,
389389
}))
@@ -404,7 +404,10 @@ via the \`patternProperty\` "${key.replace('*/', '*\\/')}".`
404404
return {
405405
ast,
406406
isPatternProperty: !singlePatternProperty,
407-
isRequired: singlePatternProperty || includes(schema.required || [], key),
407+
isRequired:
408+
singlePatternProperty ||
409+
includes(schema.required || [], key) ||
410+
(options.removeOptionalIfDefaultExists && 'default' in value),
408411
isUnreachableDefinition: false,
409412
keyName: singlePatternProperty ? '[k: string]' : key,
410413
}
@@ -422,7 +425,8 @@ via the \`definition\` "${key}".`
422425
return {
423426
ast,
424427
isPatternProperty: false,
425-
isRequired: includes(schema.required || [], key),
428+
isRequired:
429+
includes(schema.required || [], key) || (options.removeOptionalIfDefaultExists && 'default' in value),
426430
isUnreachableDefinition: true,
427431
keyName: key,
428432
}

0 commit comments

Comments
 (0)