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 readonly option #647

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| unknownAny | boolean | `true` | Use `unknown` instead of `any` where possible |
| unreachableDefinitions | boolean | `false` | Generates code for `$defs` that aren't referenced by the schema. |
| $refOptions | object | `{}` | [$RefParser](https://github.com/APIDevTools/json-schema-ref-parser) Options, used when resolving `$ref`s |

| readonly | boolean | `false` | Emit all types as `readonly` |
## Tests

```sh
Expand Down
3 changes: 3 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ main(
'strictIndexSignatures',
'unknownAny',
'unreachableDefinitions',
'readonly',
],
default: DEFAULT_OPTIONS,
string: ['bannerComment', 'cwd'],
Expand Down Expand Up @@ -197,6 +198,8 @@ Boolean values can be set to false using the 'no-' prefix.
Output unknown type instead of any type
--unreachableDefinitions
Generates code for definitions that aren't referenced by the schema
--readonly
Emits all types as readonly
`,
)
}
1 change: 1 addition & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ function generateInterface(ast: TInterface, options: Options): string {
.map(
([isRequired, keyName, ast, type]) =>
(hasComment(ast) && !ast.standaloneName ? generateComment(ast.comment, ast.deprecated) + '\n' : '') +
(options.readonly ? 'readonly ' : '') +
escapeKeyName(keyName) +
(isRequired ? '' : '?') +
': ' +
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export interface Options {
* Generate unknown type instead of any
*/
unknownAny: boolean
/**
* Emit all types as readonly
*/
readonly: boolean
}

export const DEFAULT_OPTIONS: Options = {
Expand Down Expand Up @@ -115,6 +119,7 @@ export const DEFAULT_OPTIONS: Options = {
},
unreachableDefinitions: false,
unknownAny: true,
readonly: false,
}

export function compileFromFile(filename: string, options: Partial<Options> = DEFAULT_OPTIONS): Promise<string> {
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 @@ -2610,6 +2610,28 @@ Generated by [AVA](https://avajs.dev).
}␊
`

## readonly.1.js

> Expected output to match snapshot for e2e test: readonly.1.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 ReadonlyConfiguredToTrue {␊
readonly a?: {␊
readonly [k: string]: unknown;␊
};␊
readonly b?: {␊
readonly [k: string]: unknown;␊
};␊
readonly [k: string]: unknown;␊
}␊
`

## realWorld.awsQuicksight.js

> Expected output to match snapshot for e2e test: realWorld.awsQuicksight.js
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
21 changes: 21 additions & 0 deletions test/e2e/readonly.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const input = {
title: 'Readonly (configured to true)',
type: 'object',
definitions: {
e: {
type: 'object',
},
},
properties: {
a: {
type: 'object',
},
b: {
type: 'object',
},
},
}

export const options = {
readonly: true,
}
Loading