diff --git a/packages/schema-typescript/__tests__/__snapshots__/numeric-enum.test.ts.snap b/packages/schema-typescript/__tests__/__snapshots__/numeric-enum.test.ts.snap new file mode 100644 index 00000000..e9dcd29f --- /dev/null +++ b/packages/schema-typescript/__tests__/__snapshots__/numeric-enum.test.ts.snap @@ -0,0 +1,34 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`boolean const value 1`] = ` +"export interface AlwaysEnabled { + enabled?: true; + disabled?: false; +}" +`; + +exports[`boolean enum values 1`] = ` +"export interface ToggleConfig { + enabled?: true | false; +}" +`; + +exports[`mixed enum values 1`] = ` +"export interface MixedConfig { + status?: "active" | "inactive" | 0 | 1 | true; +}" +`; + +exports[`numeric const value 1`] = ` +"export interface FixedDimension { + dimension: 3; + label: "3D"; +}" +`; + +exports[`numeric enum values 1`] = ` +"export interface PostGISConfig { + dimension: 2 | 3 | 4; + name?: "point" | "linestring" | "polygon"; +}" +`; diff --git a/packages/schema-typescript/__tests__/numeric-enum.test.ts b/packages/schema-typescript/__tests__/numeric-enum.test.ts new file mode 100644 index 00000000..b7da3ef1 --- /dev/null +++ b/packages/schema-typescript/__tests__/numeric-enum.test.ts @@ -0,0 +1,85 @@ +import { generateTypeScript } from '../src'; + +it('numeric enum values', () => { + expect( + generateTypeScript({ + title: 'PostGISConfig', + type: 'object', + properties: { + dimension: { + type: 'integer', + enum: [2, 3, 4], + description: 'Coordinate dimensionality', + }, + name: { + type: 'string', + enum: ['point', 'linestring', 'polygon'], + }, + }, + required: ['dimension'], + }) + ).toMatchSnapshot(); +}); + +it('boolean enum values', () => { + expect( + generateTypeScript({ + title: 'ToggleConfig', + type: 'object', + properties: { + enabled: { + enum: [true, false], + }, + }, + }) + ).toMatchSnapshot(); +}); + +it('mixed enum values', () => { + expect( + generateTypeScript({ + title: 'MixedConfig', + type: 'object', + properties: { + status: { + enum: ['active', 'inactive', 0, 1, true], + }, + }, + }) + ).toMatchSnapshot(); +}); + +it('numeric const value', () => { + expect( + generateTypeScript({ + title: 'FixedDimension', + type: 'object', + properties: { + dimension: { + const: 3, + }, + label: { + const: '3D', + }, + }, + required: ['dimension', 'label'], + }) + ).toMatchSnapshot(); +}); + +it('boolean const value', () => { + expect( + generateTypeScript({ + title: 'AlwaysEnabled', + type: 'object', + properties: { + enabled: { + const: true, + }, + disabled: { + const: false, + }, + }, + }) + ).toMatchSnapshot(); +}); diff --git a/packages/schema-typescript/src/schema.ts b/packages/schema-typescript/src/schema.ts index b2e8bd4f..af5e6687 100644 --- a/packages/schema-typescript/src/schema.ts +++ b/packages/schema-typescript/src/schema.ts @@ -268,14 +268,26 @@ function getTypeForProp( } if (prop.enum) { - const enumType = prop.enum.map((enumValue) => - t.tsLiteralType(t.stringLiteral(enumValue)) - ); + const enumType = prop.enum.map((enumValue) => { + if (typeof enumValue === 'number') { + return t.tsLiteralType(t.numericLiteral(enumValue)); + } + if (typeof enumValue === 'boolean') { + return t.tsLiteralType(t.booleanLiteral(enumValue)); + } + return t.tsLiteralType(t.stringLiteral(String(enumValue))); + }); return t.tsUnionType(enumType); } - if (prop.const) { - return t.tsLiteralType(t.stringLiteral(prop.const)); + if (prop.const !== undefined) { + if (typeof prop.const === 'number') { + return t.tsLiteralType(t.numericLiteral(prop.const)); + } + if (typeof prop.const === 'boolean') { + return t.tsLiteralType(t.booleanLiteral(prop.const)); + } + return t.tsLiteralType(t.stringLiteral(String(prop.const))); } if (prop.type) { diff --git a/packages/schema-typescript/src/types.ts b/packages/schema-typescript/src/types.ts index ee844b26..49ffc5aa 100644 --- a/packages/schema-typescript/src/types.ts +++ b/packages/schema-typescript/src/types.ts @@ -5,8 +5,8 @@ export interface JSONSchema { properties?: { [key: string]: JSONSchema }; required?: string[]; type?: string; - const?: string; - enum?: string[]; + const?: string | number | boolean; + enum?: (string | number | boolean)[]; items?: JSONSchema; $defs?: { [key: string]: JSONSchema }; // (JSON Schema Draft 2019-09 and later) definitions?: { [key: string]: JSONSchema }; // (JSON Schema Draft-04 to Draft-07)