Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -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";
}"
`;
85 changes: 85 additions & 0 deletions packages/schema-typescript/__tests__/numeric-enum.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
22 changes: 17 additions & 5 deletions packages/schema-typescript/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/schema-typescript/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading