diff --git a/src/generator.ts b/src/generator.ts index 9b9d3f1e..c5506ff5 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -118,6 +118,7 @@ function declareNamedTypes( hasStandaloneName(ast) ? generateStandaloneType(ast, options) : undefined ].filter(Boolean).join('\n') break + case 'CONST': case 'ENUM': type = '' break @@ -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 diff --git a/src/parser.ts b/src/parser.ts index ec3a8841..973619cc 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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': diff --git a/src/typeOfSchema.ts b/src/typeOfSchema.ts index f92a68d7..b1a07734 100644 --- a/src/typeOfSchema.ts +++ b/src/typeOfSchema.ts @@ -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' diff --git a/src/types/AST.ts b/src/types/AST.ts index 15099d7b..766e98f5 100644 --- a/src/types/AST.ts +++ b/src/types/AST.ts @@ -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 @@ -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 = { diff --git a/src/types/JSONSchema.ts b/src/types/JSONSchema.ts index 276ce4e6..f0ab6c5b 100644 --- a/src/types/JSONSchema.ts +++ b/src/types/JSONSchema.ts @@ -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 diff --git a/test/__snapshots__/test/test.ts.md b/test/__snapshots__/test/test.ts.md index 129ea76a..f05d7620 100644 --- a/test/__snapshots__/test/test.ts.md +++ b/test/__snapshots__/test/test.ts.md @@ -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 @@ -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 @@ -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;␊ - }␊ - ` diff --git a/test/__snapshots__/test/test.ts.snap b/test/__snapshots__/test/test.ts.snap index ab1bf00c..d8dcce4a 100644 Binary files a/test/__snapshots__/test/test.ts.snap and b/test/__snapshots__/test/test.ts.snap differ diff --git a/test/e2e/const.ts b/test/e2e/const.ts new file mode 100644 index 00000000..c14e7e6d --- /dev/null +++ b/test/e2e/const.ts @@ -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'] +}