Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to create string enums based on JSON enums #262

Closed
wants to merge 3 commits into from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| cwd | string | `process.cwd()` | Root directory for resolving [`$ref`](https://tools.ietf.org/id/draft-pbryan-zyp-json-ref-03.html)s |
| declareExternallyReferenced | boolean | `true` | Declare external schemas referenced via `$ref`? |
| enableConstEnums | boolean | `true` | Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)? |
| enableStringEnums | boolean | `false` | Create enums from JSON enums with eponymous keys |
| style | object | `{ bracketSpacing: false, printWidth: 120, semi: true, singleQuote: false, tabWidth: 2, trailingComma: 'none', useTabs: false }` | A [Prettier](https://prettier.io/docs/en/options.html) configuration |
| unreachableDefinitions | boolean | `false` | Generates code for `definitions` that aren't referenced by the schema. |
| strictIndexSignatures | boolean | `false` | Append all index signatures with `| undefined` so that they are strictly typed. |
Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ Boolean values can be set to false using the 'no-' prefix.
Declare external schemas referenced via '$ref'?
--enableConstEnums
Prepend enums with 'const'?
--enableStringEnums
Create enums from JSON enums instead of union types
--style.XXX=YYY
Prettier configuration
--unreachableDefinitions
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export interface Options {
* Prepend enums with [`const`](https://www.typescriptlang.org/docs/handbook/enums.html#computed-and-constant-members)?
*/
enableConstEnums: boolean
/**
* Generate string enums instead of string union types?
*/
enableStringEnums: boolean
/**
* Append all index signatures with `| undefined` so that they are strictly typed.
*
Expand Down Expand Up @@ -63,6 +67,7 @@ export const DEFAULT_OPTIONS: Options = {
cwd: process.cwd(),
declareExternallyReferenced: true,
enableConstEnums: true, // by default, avoid generating code
enableStringEnums: false,
strictIndexSignatures: false,
style: {
bracketSpacing: false,
Expand Down
12 changes: 12 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ function parseNonLiteral(
type: 'UNION'
})
case 'UNNAMED_ENUM':
if (options.enableStringEnums && schema.type === "string") {
return set({
comment: schema.description,
keyName,
params: schema.enum!.map((_) => ({
ast: parse(_, options, rootSchema, undefined, false, processed, usedNames),
keyName: _ as string
})),
standaloneName: standaloneName(schema, keyName, usedNames)!,
type: 'ENUM'
})
}
return set({
comment: schema.description,
keyName,
Expand Down
22 changes: 22 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -9442,3 +9442,25 @@ Generated by [AVA](https://ava.li).
[k: string]: string | undefined;␊
}␊
`

## enumString.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 EnumStringTest {␊
stringEnum: StringEnum;␊
}␊
export const enum StringEnum {␊
a = "a",␊
b = "b",␊
c = "c"␊
}␊
`
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
16 changes: 16 additions & 0 deletions test/e2e/enumString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const input = {
"title": "enumStringTest",
"type": "object",
"properties": {
"stringEnum": {
"type": "string",
"enum": ["a", "b", "c"]
}
},
required: ['stringEnum'],
additionalProperties: false
}

export const options = {
enableStringEnums: true
}