Skip to content

Commit 539ec61

Browse files
committed
Convert json schema const values to typescript const values
Resolves bcherny#263
1 parent 85c3ab0 commit 539ec61

File tree

8 files changed

+73
-19
lines changed

8 files changed

+73
-19
lines changed

src/generator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ function declareNamedTypes(
118118
hasStandaloneName(ast) ? generateStandaloneType(ast, options) : undefined
119119
].filter(Boolean).join('\n')
120120
break
121+
case 'CONST':
121122
case 'ENUM':
122123
type = ''
123124
break
@@ -176,6 +177,7 @@ function generateRawType(ast: AST, options: Options): string {
176177
case 'OBJECT': return 'object'
177178
case 'REFERENCE': return ast.params
178179
case 'STRING': return 'string'
180+
case 'CONST': return JSON.stringify(ast.params)
179181
case 'TUPLE': return (() => {
180182
const minItems = ast.minItems
181183
const maxItems = ast.maxItems || -1

src/parser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ function parseNonLiteral(
119119
standaloneName: standaloneName(schema, keyName, usedNames)!,
120120
type: 'ENUM'
121121
})
122+
case 'CONST':
123+
return set({
124+
comment: schema.description,
125+
keyName,
126+
params: schema.const,
127+
// standaloneName: standaloneName(schema, keyName, usedNames)!,
128+
type: 'CONST'
129+
})
122130
case 'NAMED_SCHEMA':
123131
return set(newInterface(schema as SchemaSchema, options, rootSchema, processed, usedNames, keyName))
124132
case 'NULL':

src/typeOfSchema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function typeOfSchema(schema: JSONSchema): SCHEMA_TYPE {
1414
if (schema.enum) return 'UNNAMED_ENUM'
1515
if (schema.$ref) return 'REFERENCE'
1616
if (Array.isArray(schema.type)) return 'UNION'
17+
if (schema.const) return 'CONST'
1718
switch (schema.type) {
1819
case 'string': return 'STRING'
1920
case 'number': return 'NUMBER'

src/types/AST.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type AST_TYPE = AST['type']
44

55
export type AST = TAny | TArray | TBoolean | TEnum | TInterface | TNamedInterface
66
| TIntersection | TLiteral | TNumber | TNull | TObject | TReference
7-
| TString | TTuple | TUnion | TCustomType
7+
| TString | TTuple | TUnion | TCustomType | TConst
88

99
export interface AbstractAST {
1010
comment?: string
@@ -121,6 +121,11 @@ export interface TCustomType extends AbstractAST {
121121
params: string
122122
}
123123

124+
export interface TConst extends AbstractAST {
125+
type: 'CONST'
126+
params: any
127+
}
128+
124129
//////////////////////////////////////////// literals
125130

126131
export const T_ANY: TAny = {

src/types/JSONSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { JSONSchema4, JSONSchema4TypeName } from 'json-schema'
33
export type SCHEMA_TYPE = 'ALL_OF' | 'UNNAMED_SCHEMA' | 'ANY' | 'ANY_OF'
44
| 'BOOLEAN' | 'NAMED_ENUM' | 'NAMED_SCHEMA' | 'NULL' | 'NUMBER' | 'STRING'
55
| 'OBJECT' | 'ONE_OF' | 'TYPED_ARRAY' | 'REFERENCE' | 'UNION' | 'UNNAMED_ENUM'
6-
| 'UNTYPED_ARRAY' | 'CUSTOM_TYPE'
6+
| 'UNTYPED_ARRAY' | 'CUSTOM_TYPE' | 'CONST'
77

88
export type JSONSchemaTypeName = JSONSchema4TypeName
99

test/__snapshots__/test/test.ts.md

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,25 @@ Generated by [AVA](https://ava.li).
10721072
}␊
10731073
`
10741074

1075+
## const.js
1076+
1077+
> Snapshot 1
1078+
1079+
`/* tslint:disable */␊
1080+
/**␊
1081+
* This file was automatically generated by json-schema-to-typescript.␊
1082+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
1083+
* and run json-schema-to-typescript to regenerate this file.␊
1084+
*/␊
1085+
1086+
export interface ExampleSchema {␊
1087+
aString: "foo";␊
1088+
aNumber: 5;␊
1089+
aBoolean: true;␊
1090+
[k: string]: any;␊
1091+
}␊
1092+
`
1093+
10751094
## customType.js
10761095

10771096
> Snapshot 1
@@ -9206,6 +9225,23 @@ Generated by [AVA](https://ava.li).
92069225
}␊
92079226
`
92089227

9228+
## strictIndexSignatures.js
9229+
9230+
> Snapshot 1
9231+
9232+
`/* tslint:disable */␊
9233+
/**␊
9234+
* This file was automatically generated by json-schema-to-typescript.␊
9235+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
9236+
* and run json-schema-to-typescript to regenerate this file.␊
9237+
*/␊
9238+
9239+
export interface StrictIndexSignatures {␊
9240+
maybe?: string;␊
9241+
[k: string]: string | undefined;␊
9242+
}␊
9243+
`
9244+
92099245
## subSchema.js
92109246

92119247
> Snapshot 1
@@ -9425,20 +9461,3 @@ Generated by [AVA](https://ava.li).
94259461
[k: string]: any;␊
94269462
}␊
94279463
`
9428-
9429-
## strictIndexSignatures.js
9430-
9431-
> Snapshot 1
9432-
9433-
`/* tslint:disable */␊
9434-
/**␊
9435-
* This file was automatically generated by json-schema-to-typescript.␊
9436-
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
9437-
* and run json-schema-to-typescript to regenerate this file.␊
9438-
*/␊
9439-
9440-
export interface StrictIndexSignatures {␊
9441-
maybe?: string;␊
9442-
[k: string]: string | undefined;␊
9443-
}␊
9444-
`

test/__snapshots__/test/test.ts.snap

-125 Bytes
Binary file not shown.

test/e2e/const.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const input = {
2+
title: 'Example Schema',
3+
type: 'object',
4+
properties: {
5+
aString: {
6+
type: 'string',
7+
const: 'foo'
8+
},
9+
aNumber: {
10+
type: 'number',
11+
const: 5
12+
},
13+
aBoolean: {
14+
type: 'boolean',
15+
const: true
16+
}
17+
},
18+
required: ['aString', 'aNumber', 'aBoolean']
19+
}

0 commit comments

Comments
 (0)