Skip to content

Commit

Permalink
Add ability to make default enums from json enums with option
Browse files Browse the repository at this point in the history
Just a fork and apply changes of bcherny#262 from @cdietschrun
  • Loading branch information
viplmad committed Oct 18, 2023
1 parent 3bb5d2b commit d8507ca
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,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 |
| format | boolean | `true` | Format code? Set this to `false` to improve performance. |
| ignoreMinAndMaxItems | boolean | `false` | Ignore maxItems and minItems for `array` types, preventing tuples being generated. |
| maxItems | number | `20` | Maximum number of unioned tuples to emit when representing bounded-size array types, before falling back to emitting unbounded arrays. Increase this to improve precision of emitted types, decrease it to improve performance, or set it to `-1` to ignore `maxItems`.
Expand Down
2 changes: 2 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,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
--format
Format code? Set this to false to improve performance.
--maxItems
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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
/**
* Format code? Set this to `false` to improve performance.
*/
Expand Down Expand Up @@ -90,6 +94,7 @@ export const DEFAULT_OPTIONS: Options = {
cwd: process.cwd(),
declareExternallyReferenced: true,
enableConstEnums: true,
enableStringEnums: false,
format: true,
ignoreMinAndMaxItems: false,
maxItems: 20,
Expand Down
14 changes: 14 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ function parseNonLiteral(
type: 'UNION',
}
case 'UNNAMED_ENUM':
if (options.enableStringEnums && schema.type === 'string') {
return {
comment: schema.description,
deprecated: schema.deprecated,
keyName,
standaloneName: standaloneName(schema, keyName, usedNames)!,
params: schema.enum!.map(_ => ({
ast: parseLiteral(_, undefined),
keyName: _ as string,
})),
type: 'ENUM',
}
}

return {
comment: schema.description,
deprecated: schema.deprecated,
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 @@ -1081,6 +1081,28 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## enumString.js

> Expected output to match snapshot for e2e test: enumString.js

`/* eslint-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"␊
}␊
`

## extends.1a.js

> Expected output to match snapshot for e2e test: extends.1a.js
Expand Down
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
}

0 comments on commit d8507ca

Please sign in to comment.