Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ function declareNamedTypes(
hasStandaloneName(ast) ? generateStandaloneType(ast, options) : undefined
].filter(Boolean).join('\n')
break
case 'CONST':
case 'ENUM':
type = ''
break
Expand Down Expand Up @@ -176,6 +177,7 @@ function generateRawType(ast: AST, options: Options): string {
case 'OBJECT': return 'object'
case 'REFERENCE': return ast.params
case 'STRING': return 'string'
case 'CONST': return JSON.stringify(ast.params)
case 'TUPLE': return (() => {
const minItems = ast.minItems
const maxItems = ast.maxItems || -1
Expand Down
8 changes: 8 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ function parseNonLiteral(
standaloneName: standaloneName(schema, keyName, usedNames)!,
type: 'ENUM'
})
case 'CONST':
return set({
comment: schema.description,
keyName,
params: schema.const,
// standaloneName: standaloneName(schema, keyName, usedNames)!,
type: 'CONST'
})
case 'NAMED_SCHEMA':
return set(newInterface(schema as SchemaSchema, options, rootSchema, processed, usedNames, keyName))
case 'NULL':
Expand Down
1 change: 1 addition & 0 deletions src/typeOfSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function typeOfSchema(schema: JSONSchema): SCHEMA_TYPE {
if (schema.enum) return 'UNNAMED_ENUM'
if (schema.$ref) return 'REFERENCE'
if (Array.isArray(schema.type)) return 'UNION'
if (schema.const !== undefined) return 'CONST'
switch (schema.type) {
case 'string': return 'STRING'
case 'number': return 'NUMBER'
Expand Down
7 changes: 6 additions & 1 deletion src/types/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type AST_TYPE = AST['type']

export type AST = TAny | TArray | TBoolean | TEnum | TInterface | TNamedInterface
| TIntersection | TLiteral | TNumber | TNull | TObject | TReference
| TString | TTuple | TUnion | TCustomType
| TString | TTuple | TUnion | TCustomType | TConst

export interface AbstractAST {
comment?: string
Expand Down Expand Up @@ -121,6 +121,11 @@ export interface TCustomType extends AbstractAST {
params: string
}

export interface TConst extends AbstractAST {
type: 'CONST'
params: any
}

//////////////////////////////////////////// literals

export const T_ANY: TAny = {
Expand Down
2 changes: 1 addition & 1 deletion src/types/JSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JSONSchema4, JSONSchema4TypeName } from 'json-schema'
export type SCHEMA_TYPE = 'ALL_OF' | 'UNNAMED_SCHEMA' | 'ANY' | 'ANY_OF'
| 'BOOLEAN' | 'NAMED_ENUM' | 'NAMED_SCHEMA' | 'NULL' | 'NUMBER' | 'STRING'
| 'OBJECT' | 'ONE_OF' | 'TYPED_ARRAY' | 'REFERENCE' | 'UNION' | 'UNNAMED_ENUM'
| 'UNTYPED_ARRAY' | 'CUSTOM_TYPE'
| 'UNTYPED_ARRAY' | 'CUSTOM_TYPE' | 'CONST'

export type JSONSchemaTypeName = JSONSchema4TypeName

Expand Down
57 changes: 40 additions & 17 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,29 @@ Generated by [AVA](https://ava.li).
}␊
`

## const.js

> Snapshot 1

`/* tslint: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 ExampleSchema {␊
aString: "foo";␊
aNumber: 5;␊
aBoolean: true;␊
bString: "";␊
bNumber: 0;␊
bBoolean: false;␊
aNull: null;␊
[k: string]: any;␊
}␊
`

## customType.js

> Snapshot 1
Expand Down Expand Up @@ -9206,6 +9229,23 @@ Generated by [AVA](https://ava.li).
}␊
`

## strictIndexSignatures.js

> Snapshot 1

`/* tslint: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 StrictIndexSignatures {␊
maybe?: string;␊
[k: string]: string | undefined;␊
}␊
`

## subSchema.js

> Snapshot 1
Expand Down Expand Up @@ -9425,20 +9465,3 @@ Generated by [AVA](https://ava.li).
[k: string]: any;␊
}␊
`

## strictIndexSignatures.js

> Snapshot 1

`/* tslint: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 StrictIndexSignatures {␊
maybe?: string;␊
[k: string]: string | undefined;␊
}␊
`
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
31 changes: 31 additions & 0 deletions test/e2e/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const input = {
title: 'Example Schema',
type: 'object',
properties: {
aString: {
type: 'string',
const: 'foo'
},
aNumber: {
type: 'number',
const: 5
},
aBoolean: {
type: 'boolean',
const: true
},
bString: {
const: ''
},
bNumber: {
const: 0
},
bBoolean: {
const: false
},
aNull: {
const: null
}
},
required: ['aString', 'aNumber', 'aBoolean', 'bString', 'bNumber', 'bBoolean', 'aNull']
}