diff --git a/README.md b/README.md index d9e50cdb..4ae9128e 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/package.json b/package.json index a73016b7..a1a88189 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/cli.ts b/src/cli.ts index 456f1447..85e3c0d7 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -20,6 +20,7 @@ main( 'enableConstEnums', 'format', 'ignoreMinAndMaxItems', + 'removeOptionalIfDefaultExists', 'strictIndexSignatures', 'unknownAny', 'unreachableDefinitions', @@ -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 diff --git a/src/index.ts b/src/index.ts index 1aa67be0..4e6fd567 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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. * @@ -103,6 +107,7 @@ export const DEFAULT_OPTIONS: Options = { format: true, ignoreMinAndMaxItems: false, maxItems: 20, + removeOptionalIfDefaultExists: false, strictIndexSignatures: false, style: { bracketSpacing: false, diff --git a/src/normalizer.ts b/src/normalizer.ts index 2d0d534d..01d63a86 100644 --- a/src/normalizer.ts +++ b/src/normalizer.ts @@ -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 diff --git a/src/parser.ts b/src/parser.ts index 92acdeb3..1f4799bc 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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, })) @@ -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, } @@ -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, } diff --git a/test/__snapshots__/test/test.ts.md b/test/__snapshots__/test/test.ts.md index 0d8b981b..d26a4981 100644 --- a/test/__snapshots__/test/test.ts.md +++ b/test/__snapshots__/test/test.ts.md @@ -4,106 +4,6 @@ The actual snapshot is saved in `test.ts.snap`. Generated by [AVA](https://avajs.dev). -## JSONSchema.js - -> Expected output to match snapshot for e2e test: JSONSchema.js - - `/* eslint-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 type PositiveInteger = number;␊ - export type PositiveIntegerDefault0 = PositiveInteger;␊ - /**␊ - * @minItems 1␊ - */␊ - export type SchemaArray = [HttpJsonSchemaOrgDraft04Schema, ...HttpJsonSchemaOrgDraft04Schema[]];␊ - /**␊ - * @minItems 1␊ - */␊ - export type StringArray = [string, ...string[]];␊ - export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string";␊ - ␊ - /**␊ - * Core schema meta-schema␊ - */␊ - export interface HttpJsonSchemaOrgDraft04Schema {␊ - id?: string;␊ - $schema?: string;␊ - title?: string;␊ - description?: string;␊ - default?: unknown;␊ - multipleOf?: number;␊ - maximum?: number;␊ - exclusiveMaximum?: boolean;␊ - minimum?: number;␊ - exclusiveMinimum?: boolean;␊ - maxLength?: PositiveInteger;␊ - minLength?: PositiveIntegerDefault0;␊ - pattern?: string;␊ - additionalItems?: boolean | HttpJsonSchemaOrgDraft04Schema;␊ - items?: HttpJsonSchemaOrgDraft04Schema | SchemaArray;␊ - maxItems?: PositiveInteger;␊ - minItems?: PositiveIntegerDefault0;␊ - uniqueItems?: boolean;␊ - maxProperties?: PositiveInteger;␊ - minProperties?: PositiveIntegerDefault0;␊ - required?: StringArray;␊ - additionalProperties?: boolean | HttpJsonSchemaOrgDraft04Schema;␊ - definitions?: {␊ - [k: string]: HttpJsonSchemaOrgDraft04Schema;␊ - };␊ - properties?: {␊ - [k: string]: HttpJsonSchemaOrgDraft04Schema;␊ - };␊ - patternProperties?: {␊ - [k: string]: HttpJsonSchemaOrgDraft04Schema;␊ - };␊ - dependencies?: {␊ - [k: string]: HttpJsonSchemaOrgDraft04Schema | StringArray;␊ - };␊ - /**␊ - * @minItems 1␊ - */␊ - enum?: [unknown, ...unknown[]];␊ - type?: SimpleTypes | [SimpleTypes, ...SimpleTypes[]];␊ - allOf?: SchemaArray;␊ - anyOf?: SchemaArray;␊ - oneOf?: SchemaArray;␊ - not?: HttpJsonSchemaOrgDraft04Schema;␊ - [k: string]: unknown;␊ - }␊ - ` - -## WithComment.js - -> Expected output to match snapshot for e2e test: WithComment.js - - `/* eslint-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 WithComment {␊ - /**␊ - * /* comment * /␊ - */␊ - a?: {␊ - /**␊ - * /* nested comment * /␊ - */␊ - b?: string;␊ - [k: string]: unknown;␊ - };␊ - [k: string]: unknown;␊ - }␊ - ` - ## additionalProperties.1.js > Expected output to match snapshot for e2e test: additionalProperties.1.js @@ -1993,6 +1893,80 @@ Generated by [AVA](https://avajs.dev). }␊ ` +## JSONSchema.js + +> Expected output to match snapshot for e2e test: JSONSchema.js + + `/* eslint-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 type PositiveInteger = number;␊ + export type PositiveIntegerDefault0 = PositiveInteger;␊ + /**␊ + * @minItems 1␊ + */␊ + export type SchemaArray = [HttpJsonSchemaOrgDraft04Schema, ...HttpJsonSchemaOrgDraft04Schema[]];␊ + /**␊ + * @minItems 1␊ + */␊ + export type StringArray = [string, ...string[]];␊ + export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string";␊ + ␊ + /**␊ + * Core schema meta-schema␊ + */␊ + export interface HttpJsonSchemaOrgDraft04Schema {␊ + id?: string;␊ + $schema?: string;␊ + title?: string;␊ + description?: string;␊ + default?: unknown;␊ + multipleOf?: number;␊ + maximum?: number;␊ + exclusiveMaximum?: boolean;␊ + minimum?: number;␊ + exclusiveMinimum?: boolean;␊ + maxLength?: PositiveInteger;␊ + minLength?: PositiveIntegerDefault0;␊ + pattern?: string;␊ + additionalItems?: boolean | HttpJsonSchemaOrgDraft04Schema;␊ + items?: HttpJsonSchemaOrgDraft04Schema | SchemaArray;␊ + maxItems?: PositiveInteger;␊ + minItems?: PositiveIntegerDefault0;␊ + uniqueItems?: boolean;␊ + maxProperties?: PositiveInteger;␊ + minProperties?: PositiveIntegerDefault0;␊ + required?: StringArray;␊ + additionalProperties?: boolean | HttpJsonSchemaOrgDraft04Schema;␊ + definitions?: {␊ + [k: string]: HttpJsonSchemaOrgDraft04Schema;␊ + };␊ + properties?: {␊ + [k: string]: HttpJsonSchemaOrgDraft04Schema;␊ + };␊ + patternProperties?: {␊ + [k: string]: HttpJsonSchemaOrgDraft04Schema;␊ + };␊ + dependencies?: {␊ + [k: string]: HttpJsonSchemaOrgDraft04Schema | StringArray;␊ + };␊ + /**␊ + * @minItems 1␊ + */␊ + enum?: [unknown, ...unknown[]];␊ + type?: SimpleTypes | [SimpleTypes, ...SimpleTypes[]];␊ + allOf?: SchemaArray;␊ + anyOf?: SchemaArray;␊ + oneOf?: SchemaArray;␊ + not?: HttpJsonSchemaOrgDraft04Schema;␊ + [k: string]: unknown;␊ + }␊ + ` + ## namedProperty.js > Expected output to match snapshot for e2e test: namedProperty.js @@ -2369,6 +2343,29 @@ Generated by [AVA](https://avajs.dev). }␊ ` +## options.removeOptionalIfDefaultExists.js + +> Expected output to match snapshot for e2e test: options.removeOptionalIfDefaultExists.js + + `/* eslint-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 Options {␊ + name: string;␊ + age: number;␊ + email: string | null;␊ + attributes: {␊ + [k: string]: unknown;␊ + };␊ + isActive?: boolean;␊ + [k: string]: unknown;␊ + }␊ + ` + ## options.strictIndexSignatures.js > Expected output to match snapshot for e2e test: options.strictIndexSignatures.js @@ -443783,6 +443780,149 @@ Generated by [AVA](https://avajs.dev). }␊ ` +## realWorld.jsonschema.js + +> Expected output to match snapshot for e2e test: realWorld.jsonschema.js + + `/* eslint-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 type CoreSchemaMetaSchema = {␊ + $id?: string;␊ + $schema?: string;␊ + $ref?: string;␊ + $comment?: string;␊ + title?: string;␊ + description?: string;␊ + default?: unknown;␊ + readOnly?: boolean;␊ + writeOnly?: boolean;␊ + examples?: unknown[];␊ + multipleOf?: number;␊ + maximum?: number;␊ + exclusiveMaximum?: number;␊ + minimum?: number;␊ + exclusiveMinimum?: number;␊ + maxLength?: NonNegativeInteger;␊ + minLength?: NonNegativeIntegerDefault0;␊ + pattern?: string;␊ + additionalItems?: CoreSchemaMetaSchema;␊ + items?: CoreSchemaMetaSchema | SchemaArray;␊ + maxItems?: NonNegativeInteger;␊ + minItems?: NonNegativeIntegerDefault0;␊ + uniqueItems?: boolean;␊ + contains?: CoreSchemaMetaSchema;␊ + maxProperties?: NonNegativeInteger;␊ + minProperties?: NonNegativeIntegerDefault0;␊ + required?: StringArray;␊ + additionalProperties?: CoreSchemaMetaSchema;␊ + definitions?: {␊ + [k: string]: CoreSchemaMetaSchema;␊ + };␊ + properties?: {␊ + [k: string]: CoreSchemaMetaSchema;␊ + };␊ + patternProperties?: {␊ + [k: string]: CoreSchemaMetaSchema;␊ + };␊ + dependencies?: {␊ + [k: string]: CoreSchemaMetaSchema | StringArray;␊ + };␊ + propertyNames?: CoreSchemaMetaSchema;␊ + const?: unknown;␊ + /**␊ + * @minItems 1␊ + */␊ + enum?: [unknown, ...unknown[]];␊ + type?: SimpleTypes | [SimpleTypes, ...SimpleTypes[]];␊ + format?: string;␊ + contentMediaType?: string;␊ + contentEncoding?: string;␊ + if?: CoreSchemaMetaSchema;␊ + then?: CoreSchemaMetaSchema;␊ + else?: CoreSchemaMetaSchema;␊ + allOf?: SchemaArray;␊ + anyOf?: SchemaArray;␊ + oneOf?: SchemaArray;␊ + not?: CoreSchemaMetaSchema;␊ + [k: string]: unknown;␊ + } & (␊ + | {␊ + $id?: string;␊ + $schema?: string;␊ + $ref?: string;␊ + $comment?: string;␊ + title?: string;␊ + description?: string;␊ + default?: unknown;␊ + readOnly?: boolean;␊ + writeOnly?: boolean;␊ + examples?: unknown[];␊ + multipleOf?: number;␊ + maximum?: number;␊ + exclusiveMaximum?: number;␊ + minimum?: number;␊ + exclusiveMinimum?: number;␊ + maxLength?: NonNegativeInteger;␊ + minLength?: NonNegativeIntegerDefault0;␊ + pattern?: string;␊ + additionalItems?: CoreSchemaMetaSchema;␊ + items?: CoreSchemaMetaSchema | SchemaArray;␊ + maxItems?: NonNegativeInteger;␊ + minItems?: NonNegativeIntegerDefault0;␊ + uniqueItems?: boolean;␊ + contains?: CoreSchemaMetaSchema;␊ + maxProperties?: NonNegativeInteger;␊ + minProperties?: NonNegativeIntegerDefault0;␊ + required?: StringArray;␊ + additionalProperties?: CoreSchemaMetaSchema;␊ + definitions?: {␊ + [k: string]: CoreSchemaMetaSchema;␊ + };␊ + properties?: {␊ + [k: string]: CoreSchemaMetaSchema;␊ + };␊ + patternProperties?: {␊ + [k: string]: CoreSchemaMetaSchema;␊ + };␊ + dependencies?: {␊ + [k: string]: CoreSchemaMetaSchema | StringArray;␊ + };␊ + propertyNames?: CoreSchemaMetaSchema;␊ + const?: unknown;␊ + /**␊ + * @minItems 1␊ + */␊ + enum?: [unknown, ...unknown[]];␊ + type?: SimpleTypes | [SimpleTypes, ...SimpleTypes[]];␊ + format?: string;␊ + contentMediaType?: string;␊ + contentEncoding?: string;␊ + if?: CoreSchemaMetaSchema;␊ + then?: CoreSchemaMetaSchema;␊ + else?: CoreSchemaMetaSchema;␊ + allOf?: SchemaArray;␊ + anyOf?: SchemaArray;␊ + oneOf?: SchemaArray;␊ + not?: CoreSchemaMetaSchema;␊ + [k: string]: unknown;␊ + }␊ + | boolean␊ + );␊ + export type NonNegativeInteger = number;␊ + export type NonNegativeIntegerDefault0 = NonNegativeInteger;␊ + /**␊ + * @minItems 1␊ + */␊ + export type SchemaArray = [CoreSchemaMetaSchema, ...CoreSchemaMetaSchema[]];␊ + export type StringArray = string[];␊ + export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string";␊ + ` + ## realWorld.jsonStat.js > Expected output to match snapshot for e2e test: realWorld.jsonStat.js @@ -443990,149 +444130,6 @@ Generated by [AVA](https://avajs.dev). }␊ ` -## realWorld.jsonschema.js - -> Expected output to match snapshot for e2e test: realWorld.jsonschema.js - - `/* eslint-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 type CoreSchemaMetaSchema = {␊ - $id?: string;␊ - $schema?: string;␊ - $ref?: string;␊ - $comment?: string;␊ - title?: string;␊ - description?: string;␊ - default?: unknown;␊ - readOnly?: boolean;␊ - writeOnly?: boolean;␊ - examples?: unknown[];␊ - multipleOf?: number;␊ - maximum?: number;␊ - exclusiveMaximum?: number;␊ - minimum?: number;␊ - exclusiveMinimum?: number;␊ - maxLength?: NonNegativeInteger;␊ - minLength?: NonNegativeIntegerDefault0;␊ - pattern?: string;␊ - additionalItems?: CoreSchemaMetaSchema;␊ - items?: CoreSchemaMetaSchema | SchemaArray;␊ - maxItems?: NonNegativeInteger;␊ - minItems?: NonNegativeIntegerDefault0;␊ - uniqueItems?: boolean;␊ - contains?: CoreSchemaMetaSchema;␊ - maxProperties?: NonNegativeInteger;␊ - minProperties?: NonNegativeIntegerDefault0;␊ - required?: StringArray;␊ - additionalProperties?: CoreSchemaMetaSchema;␊ - definitions?: {␊ - [k: string]: CoreSchemaMetaSchema;␊ - };␊ - properties?: {␊ - [k: string]: CoreSchemaMetaSchema;␊ - };␊ - patternProperties?: {␊ - [k: string]: CoreSchemaMetaSchema;␊ - };␊ - dependencies?: {␊ - [k: string]: CoreSchemaMetaSchema | StringArray;␊ - };␊ - propertyNames?: CoreSchemaMetaSchema;␊ - const?: unknown;␊ - /**␊ - * @minItems 1␊ - */␊ - enum?: [unknown, ...unknown[]];␊ - type?: SimpleTypes | [SimpleTypes, ...SimpleTypes[]];␊ - format?: string;␊ - contentMediaType?: string;␊ - contentEncoding?: string;␊ - if?: CoreSchemaMetaSchema;␊ - then?: CoreSchemaMetaSchema;␊ - else?: CoreSchemaMetaSchema;␊ - allOf?: SchemaArray;␊ - anyOf?: SchemaArray;␊ - oneOf?: SchemaArray;␊ - not?: CoreSchemaMetaSchema;␊ - [k: string]: unknown;␊ - } & (␊ - | {␊ - $id?: string;␊ - $schema?: string;␊ - $ref?: string;␊ - $comment?: string;␊ - title?: string;␊ - description?: string;␊ - default?: unknown;␊ - readOnly?: boolean;␊ - writeOnly?: boolean;␊ - examples?: unknown[];␊ - multipleOf?: number;␊ - maximum?: number;␊ - exclusiveMaximum?: number;␊ - minimum?: number;␊ - exclusiveMinimum?: number;␊ - maxLength?: NonNegativeInteger;␊ - minLength?: NonNegativeIntegerDefault0;␊ - pattern?: string;␊ - additionalItems?: CoreSchemaMetaSchema;␊ - items?: CoreSchemaMetaSchema | SchemaArray;␊ - maxItems?: NonNegativeInteger;␊ - minItems?: NonNegativeIntegerDefault0;␊ - uniqueItems?: boolean;␊ - contains?: CoreSchemaMetaSchema;␊ - maxProperties?: NonNegativeInteger;␊ - minProperties?: NonNegativeIntegerDefault0;␊ - required?: StringArray;␊ - additionalProperties?: CoreSchemaMetaSchema;␊ - definitions?: {␊ - [k: string]: CoreSchemaMetaSchema;␊ - };␊ - properties?: {␊ - [k: string]: CoreSchemaMetaSchema;␊ - };␊ - patternProperties?: {␊ - [k: string]: CoreSchemaMetaSchema;␊ - };␊ - dependencies?: {␊ - [k: string]: CoreSchemaMetaSchema | StringArray;␊ - };␊ - propertyNames?: CoreSchemaMetaSchema;␊ - const?: unknown;␊ - /**␊ - * @minItems 1␊ - */␊ - enum?: [unknown, ...unknown[]];␊ - type?: SimpleTypes | [SimpleTypes, ...SimpleTypes[]];␊ - format?: string;␊ - contentMediaType?: string;␊ - contentEncoding?: string;␊ - if?: CoreSchemaMetaSchema;␊ - then?: CoreSchemaMetaSchema;␊ - else?: CoreSchemaMetaSchema;␊ - allOf?: SchemaArray;␊ - anyOf?: SchemaArray;␊ - oneOf?: SchemaArray;␊ - not?: CoreSchemaMetaSchema;␊ - [k: string]: unknown;␊ - }␊ - | boolean␊ - );␊ - export type NonNegativeInteger = number;␊ - export type NonNegativeIntegerDefault0 = NonNegativeInteger;␊ - /**␊ - * @minItems 1␊ - */␊ - export type SchemaArray = [CoreSchemaMetaSchema, ...CoreSchemaMetaSchema[]];␊ - export type StringArray = string[];␊ - export type SimpleTypes = "array" | "boolean" | "integer" | "null" | "number" | "object" | "string";␊ - ` - ## realWorld.openapi.js > Expected output to match snapshot for e2e test: realWorld.openapi.js @@ -448303,6 +448300,36 @@ Generated by [AVA](https://avajs.dev). }␊ ` +## referencesShouldBeNormalized.js + +> Expected output to match snapshot for e2e test: referencesShouldBeNormalized.js + + `/* eslint-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 Referencing {␊ + a: ExampleSchema;␊ + [k: string]: unknown;␊ + }␊ + export interface ExampleSchema {␊ + /**␊ + * @maxItems 5␊ + */␊ + b?:␊ + | []␊ + | [number]␊ + | [number, number]␊ + | [number, number, number]␊ + | [number, number, number, number]␊ + | [number, number, number, number, number];␊ + [k: string]: unknown;␊ + }␊ + ` + ## refWithCycle.1.js > Expected output to match snapshot for e2e test: refWithCycle.1.js @@ -448385,36 +448412,6 @@ Generated by [AVA](https://avajs.dev). }␊ ` -## referencesShouldBeNormalized.js - -> Expected output to match snapshot for e2e test: referencesShouldBeNormalized.js - - `/* eslint-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 Referencing {␊ - a: ExampleSchema;␊ - [k: string]: unknown;␊ - }␊ - export interface ExampleSchema {␊ - /**␊ - * @maxItems 5␊ - */␊ - b?:␊ - | []␊ - | [number]␊ - | [number, number]␊ - | [number, number, number]␊ - | [number, number, number, number]␊ - | [number, number, number, number, number];␊ - [k: string]: unknown;␊ - }␊ - ` - ## required.js > Expected output to match snapshot for e2e test: required.js @@ -449070,6 +449067,32 @@ Generated by [AVA](https://avajs.dev). }␊ ` +## WithComment.js + +> Expected output to match snapshot for e2e test: WithComment.js + + `/* eslint-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 WithComment {␊ + /**␊ + * /* comment * /␊ + */␊ + a?: {␊ + /**␊ + * /* nested comment * /␊ + */␊ + b?: string;␊ + [k: string]: unknown;␊ + };␊ + [k: string]: unknown;␊ + }␊ + ` + ## withDescription.js > Expected output to match snapshot for e2e test: withDescription.js diff --git a/test/__snapshots__/test/test.ts.snap b/test/__snapshots__/test/test.ts.snap index 79cd4741..c6c76adb 100644 Binary files a/test/__snapshots__/test/test.ts.snap and b/test/__snapshots__/test/test.ts.snap differ diff --git a/test/e2e/options.removeOptionalIfDefaultExists.ts b/test/e2e/options.removeOptionalIfDefaultExists.ts new file mode 100644 index 00000000..1b85d109 --- /dev/null +++ b/test/e2e/options.removeOptionalIfDefaultExists.ts @@ -0,0 +1,29 @@ +export const input = { + type: 'object', + properties: { + name: { + type: 'string', + default: 'John Doe', + }, + age: { + type: 'number', + default: 0, + }, + email: { + type: ['string', 'null'], + default: null, + }, + attributes: { + type: 'object', + properties: {}, + default: {}, + }, + isActive: { + type: 'boolean', + }, + }, +} + +export const options = { + removeOptionalIfDefaultExists: true, +}