@@ -16,22 +16,24 @@ import {
16
16
TIntersection ,
17
17
} from './types/AST'
18
18
import {
19
+ EnumJSONSchema ,
19
20
getRootSchema ,
20
21
isBoolean ,
21
22
isPrimitive ,
22
- JSONSchema as LinkedJSONSchema ,
23
23
JSONSchemaWithDefinitions ,
24
+ NormalizedJSONSchema ,
25
+ Parent ,
24
26
SchemaSchema ,
25
27
SchemaType ,
26
28
} from './types/JSONSchema'
27
29
import { generateName , log , maybeStripDefault , maybeStripNameHints } from './utils'
28
30
29
- export type Processed = Map < LinkedJSONSchema , Map < SchemaType , AST > >
31
+ export type Processed = Map < NormalizedJSONSchema , Map < SchemaType , AST > >
30
32
31
33
export type UsedNames = Set < string >
32
34
33
35
export function parse (
34
- schema : LinkedJSONSchema | JSONSchema4Type ,
36
+ schema : NormalizedJSONSchema | JSONSchema4Type ,
35
37
options : Options ,
36
38
keyName ?: string ,
37
39
processed : Processed = new Map ( ) ,
@@ -56,9 +58,12 @@ export function parse(
56
58
// so that it gets first pick for standalone name.
57
59
const ast = parseAsTypeWithCache (
58
60
{
61
+ [ Parent ] : schema [ Parent ] ,
59
62
$id : schema . $id ,
63
+ additionalProperties : schema . additionalProperties ,
60
64
allOf : [ ] ,
61
65
description : schema . description ,
66
+ required : schema . required ,
62
67
title : schema . title ,
63
68
} ,
64
69
'ALL_OF' ,
@@ -79,7 +84,7 @@ export function parse(
79
84
}
80
85
81
86
function parseAsTypeWithCache (
82
- schema : LinkedJSONSchema ,
87
+ schema : NormalizedJSONSchema ,
83
88
type : SchemaType ,
84
89
options : Options ,
85
90
keyName ?: string ,
@@ -131,7 +136,7 @@ function parseLiteral(schema: JSONSchema4Type, keyName: string | undefined): AST
131
136
}
132
137
133
138
function parseNonLiteral (
134
- schema : LinkedJSONSchema ,
139
+ schema : NormalizedJSONSchema ,
135
140
type : SchemaType ,
136
141
options : Options ,
137
142
keyName : string | undefined ,
@@ -191,7 +196,7 @@ function parseNonLiteral(
191
196
deprecated : schema . deprecated ,
192
197
keyName,
193
198
standaloneName : standaloneName ( schema , keyNameFromDefinition ?? keyName , usedNames , options ) ! ,
194
- params : schema . enum ! . map ( ( _ , n ) => ( {
199
+ params : ( schema as EnumJSONSchema ) . enum ! . map ( ( _ , n ) => ( {
195
200
ast : parseLiteral ( _ , undefined ) ,
196
201
keyName : schema . tsEnumNames ! [ n ] ,
197
202
} ) ) ,
@@ -288,7 +293,12 @@ function parseNonLiteral(
288
293
keyName,
289
294
standaloneName : standaloneName ( schema , keyNameFromDefinition , usedNames , options ) ,
290
295
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
+ }
292
302
return parse ( maybeStripDefault ( member as any ) , options , undefined , processed , usedNames )
293
303
} ) ,
294
304
type : 'UNION' ,
@@ -299,7 +309,7 @@ function parseNonLiteral(
299
309
deprecated : schema . deprecated ,
300
310
keyName,
301
311
standaloneName : standaloneName ( schema , keyNameFromDefinition , usedNames , options ) ,
302
- params : schema . enum ! . map ( _ => parseLiteral ( _ , undefined ) ) ,
312
+ params : ( schema as EnumJSONSchema ) . enum ! . map ( _ => parseLiteral ( _ , undefined ) ) ,
303
313
type : 'UNION' ,
304
314
}
305
315
case 'UNNAMED_SCHEMA' :
@@ -340,7 +350,7 @@ function parseNonLiteral(
340
350
* Compute a schema name using a series of fallbacks
341
351
*/
342
352
function standaloneName (
343
- schema : LinkedJSONSchema ,
353
+ schema : NormalizedJSONSchema ,
344
354
keyNameFromDefinition : string | undefined ,
345
355
usedNames : UsedNames ,
346
356
options : Options ,
@@ -478,12 +488,12 @@ via the \`definition\` "${key}".`
478
488
}
479
489
}
480
490
481
- type Definitions = { [ k : string ] : LinkedJSONSchema }
491
+ type Definitions = { [ k : string ] : NormalizedJSONSchema }
482
492
483
493
function getDefinitions (
484
- schema : LinkedJSONSchema ,
494
+ schema : NormalizedJSONSchema ,
485
495
isSchema = true ,
486
- processed = new Set < LinkedJSONSchema > ( ) ,
496
+ processed = new Set < NormalizedJSONSchema > ( ) ,
487
497
) : Definitions {
488
498
if ( processed . has ( schema ) ) {
489
499
return { }
@@ -518,6 +528,6 @@ const getDefinitionsMemoized = memoize(getDefinitions)
518
528
/**
519
529
* TODO: Reduce rate of false positives
520
530
*/
521
- function hasDefinitions ( schema : LinkedJSONSchema ) : schema is JSONSchemaWithDefinitions {
531
+ function hasDefinitions ( schema : NormalizedJSONSchema ) : schema is JSONSchemaWithDefinitions {
522
532
return '$defs' in schema
523
533
}
0 commit comments