Skip to content

Commit 74d6126

Browse files
committed
[chore] Clean up types
1 parent 31993de commit 74d6126

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

src/parser.ts

+23-13
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ import {
1616
TIntersection,
1717
} from './types/AST'
1818
import {
19+
EnumJSONSchema,
1920
getRootSchema,
2021
isBoolean,
2122
isPrimitive,
22-
JSONSchema as LinkedJSONSchema,
2323
JSONSchemaWithDefinitions,
24+
NormalizedJSONSchema,
25+
Parent,
2426
SchemaSchema,
2527
SchemaType,
2628
} from './types/JSONSchema'
2729
import {generateName, log, maybeStripDefault, maybeStripNameHints} from './utils'
2830

29-
export type Processed = Map<LinkedJSONSchema, Map<SchemaType, AST>>
31+
export type Processed = Map<NormalizedJSONSchema, Map<SchemaType, AST>>
3032

3133
export type UsedNames = Set<string>
3234

3335
export function parse(
34-
schema: LinkedJSONSchema | JSONSchema4Type,
36+
schema: NormalizedJSONSchema | JSONSchema4Type,
3537
options: Options,
3638
keyName?: string,
3739
processed: Processed = new Map(),
@@ -56,9 +58,12 @@ export function parse(
5658
// so that it gets first pick for standalone name.
5759
const ast = parseAsTypeWithCache(
5860
{
61+
[Parent]: schema[Parent],
5962
$id: schema.$id,
63+
additionalProperties: schema.additionalProperties,
6064
allOf: [],
6165
description: schema.description,
66+
required: schema.required,
6267
title: schema.title,
6368
},
6469
'ALL_OF',
@@ -79,7 +84,7 @@ export function parse(
7984
}
8085

8186
function parseAsTypeWithCache(
82-
schema: LinkedJSONSchema,
87+
schema: NormalizedJSONSchema,
8388
type: SchemaType,
8489
options: Options,
8590
keyName?: string,
@@ -131,7 +136,7 @@ function parseLiteral(schema: JSONSchema4Type, keyName: string | undefined): AST
131136
}
132137

133138
function parseNonLiteral(
134-
schema: LinkedJSONSchema,
139+
schema: NormalizedJSONSchema,
135140
type: SchemaType,
136141
options: Options,
137142
keyName: string | undefined,
@@ -191,7 +196,7 @@ function parseNonLiteral(
191196
deprecated: schema.deprecated,
192197
keyName,
193198
standaloneName: standaloneName(schema, keyNameFromDefinition ?? keyName, usedNames, options)!,
194-
params: schema.enum!.map((_, n) => ({
199+
params: (schema as EnumJSONSchema).enum!.map((_, n) => ({
195200
ast: parseLiteral(_, undefined),
196201
keyName: schema.tsEnumNames![n],
197202
})),
@@ -288,7 +293,12 @@ function parseNonLiteral(
288293
keyName,
289294
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames, options),
290295
params: (schema.type as JSONSchema4TypeName[]).map(type => {
291-
const member: LinkedJSONSchema = {...omit(schema, '$id', 'description', 'title'), type}
296+
const member: NormalizedJSONSchema = {
297+
...omit(schema, '$id', 'description', 'title'),
298+
type,
299+
additionalProperties: schema.additionalProperties,
300+
required: schema.required,
301+
}
292302
return parse(maybeStripDefault(member as any), options, undefined, processed, usedNames)
293303
}),
294304
type: 'UNION',
@@ -299,7 +309,7 @@ function parseNonLiteral(
299309
deprecated: schema.deprecated,
300310
keyName,
301311
standaloneName: standaloneName(schema, keyNameFromDefinition, usedNames, options),
302-
params: schema.enum!.map(_ => parseLiteral(_, undefined)),
312+
params: (schema as EnumJSONSchema).enum!.map(_ => parseLiteral(_, undefined)),
303313
type: 'UNION',
304314
}
305315
case 'UNNAMED_SCHEMA':
@@ -340,7 +350,7 @@ function parseNonLiteral(
340350
* Compute a schema name using a series of fallbacks
341351
*/
342352
function standaloneName(
343-
schema: LinkedJSONSchema,
353+
schema: NormalizedJSONSchema,
344354
keyNameFromDefinition: string | undefined,
345355
usedNames: UsedNames,
346356
options: Options,
@@ -478,12 +488,12 @@ via the \`definition\` "${key}".`
478488
}
479489
}
480490

481-
type Definitions = {[k: string]: LinkedJSONSchema}
491+
type Definitions = {[k: string]: NormalizedJSONSchema}
482492

483493
function getDefinitions(
484-
schema: LinkedJSONSchema,
494+
schema: NormalizedJSONSchema,
485495
isSchema = true,
486-
processed = new Set<LinkedJSONSchema>(),
496+
processed = new Set<NormalizedJSONSchema>(),
487497
): Definitions {
488498
if (processed.has(schema)) {
489499
return {}
@@ -518,6 +528,6 @@ const getDefinitionsMemoized = memoize(getDefinitions)
518528
/**
519529
* TODO: Reduce rate of false positives
520530
*/
521-
function hasDefinitions(schema: LinkedJSONSchema): schema is JSONSchemaWithDefinitions {
531+
function hasDefinitions(schema: NormalizedJSONSchema): schema is JSONSchemaWithDefinitions {
522532
return '$defs' in schema
523533
}

src/types/JSONSchema.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ export interface LinkedJSONSchema extends JSONSchema {
7070
not?: LinkedJSONSchema
7171
}
7272

73-
export interface NormalizedJSONSchema extends LinkedJSONSchema {
73+
/**
74+
* Normalized JSON schema.
75+
*
76+
* Note: `definitions` and `id` are removed by the normalizer. Use `$defs` and `$id` instead.
77+
*/
78+
export interface NormalizedJSONSchema extends Omit<LinkedJSONSchema, 'definitions' | 'id'> {
79+
[Parent]: NormalizedJSONSchema | null
80+
7481
additionalItems?: boolean | NormalizedJSONSchema
7582
additionalProperties: boolean | NormalizedJSONSchema
7683
extends?: string[]
@@ -92,14 +99,10 @@ export interface NormalizedJSONSchema extends LinkedJSONSchema {
9299
oneOf?: NormalizedJSONSchema[]
93100
not?: NormalizedJSONSchema
94101
required: string[]
95-
96-
// Removed by normalizer
97-
definitions: never
98-
id: never
99102
}
100103

101104
export interface EnumJSONSchema extends NormalizedJSONSchema {
102-
enum: any[]
105+
enum: JSONSchema4Type[]
103106
}
104107

105108
export interface NamedEnumJSONSchema extends NormalizedJSONSchema {
@@ -123,7 +126,7 @@ export interface CustomTypeJSONSchema extends NormalizedJSONSchema {
123126
tsType: string
124127
}
125128

126-
export const getRootSchema = memoize((schema: LinkedJSONSchema): LinkedJSONSchema => {
129+
export const getRootSchema = memoize((schema: NormalizedJSONSchema): NormalizedJSONSchema => {
127130
const parent = schema[Parent]
128131
if (!parent) {
129132
return schema

src/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {deburr, isPlainObject, trim, upperFirst} from 'lodash'
22
import {basename, dirname, extname, normalize, sep, posix} from 'path'
3-
import {JSONSchema, LinkedJSONSchema, Parent} from './types/JSONSchema'
3+
import {JSONSchema, LinkedJSONSchema, NormalizedJSONSchema, Parent} from './types/JSONSchema'
44
import {JSONSchema4} from 'json-schema'
55
import yaml from 'js-yaml'
66

@@ -338,7 +338,7 @@ export function maybeStripDefault(schema: LinkedJSONSchema): LinkedJSONSchema {
338338
*
339339
* Mutates `schema`.
340340
*/
341-
export function maybeStripNameHints(schema: JSONSchema): JSONSchema {
341+
export function maybeStripNameHints(schema: NormalizedJSONSchema): NormalizedJSONSchema {
342342
if ('$id' in schema) {
343343
delete schema.$id
344344
}

0 commit comments

Comments
 (0)