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

option to prepend "readonly" to all fields #638

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -139,6 +139,7 @@ See [server demo](example) and [browser demo](https://github.com/bcherny/json-sc
| 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)? |
| inferStringEnumKeysFromValues | boolean | `false` | Create enums from JSON enums with eponymous keys |
| readonly | boolean | false | Generate all properties and array types as readonly. When true, all properties in interfaces will be prefixed with 'readonly', array types will be generated as 'readonly T[]', and index signatures will be readonly.
| 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
5 changes: 5 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ function generateTypeUnmemoized(ast: AST, options: Options): string {
return `${type} | undefined`
}

if (options.readonly && ast.type === 'ARRAY') {
return `readonly ${type}`
}

return type
}
export const generateType = memoize(generateTypeUnmemoized)
Expand Down Expand Up @@ -305,6 +309,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
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export interface Options {
* Create enums from JSON enums with eponymous keys
*/
inferStringEnumKeysFromValues: boolean
/**
* Generate all properties and array types as readonly.
* When true, all properties in interfaces will be prefixed with 'readonly',
* array types will be generated as 'readonly T[]', and index signatures will be readonly.
*/
readonly: boolean
/**
* Format code? Set this to `false` to improve performance.
*/
Expand Down Expand Up @@ -100,6 +106,7 @@ export const DEFAULT_OPTIONS: Options = {
declareExternallyReferenced: true,
enableConstEnums: true,
inferStringEnumKeysFromValues: false,
readonly: false,
format: true,
ignoreMinAndMaxItems: false,
maxItems: 20,
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 @@ -4,6 +4,28 @@ The actual snapshot is saved in `test.ts.snap`.

Generated by [AVA](https://avajs.dev).

## readonly.js

> Expected output to match snapshot for e2e test: readonly.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 Readonly {␊
readonly user?: {␊
readonly name?: string;␊
readonly age?: number;␊
readonly hobbies?: readonly string[];␊
readonly [k: string]: unknown;␊
};␊
readonly [k: string]: unknown;␊
}␊
`

## JSONSchema.js

> Expected output to match snapshot for e2e test: JSONSchema.js
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
31 changes: 31 additions & 0 deletions test/e2e/readonly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const input = {
$schema: 'http://json-schema.org/draft-07/schema',
type: 'object',
properties: {
user: {
type: 'object',
properties: {
name: {type: 'string'},
age: {type: 'number'},
hobbies: {
type: 'array',
items: {type: 'string'},
},
},
},
},
}

export const options = {
readonly: true,
}

export const output = `export interface User {
readonly name?: string;
readonly age?: number;
readonly hobbies?: readonly string[];
}

export interface Root {
readonly user?: User;
}`