Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,48 @@ By default, the command-line generator will use the `tsconfig.json` file in the
-h, --help display help for command
```

### Custom JSDoc tags

Some JSON schema features cannot be expressed in TypeScript. As an escape hatch, any JSDoc tag listed with `--validation-keywords` (or `extraTags` in the programmatic config) is copied into the generated schema. The tag value is parsed as JSON (JSON5) and falls back to a plain string if it is not valid JSON.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"JSON schema" should be "JSON Schema". "Escape hatch" is jargon from the issue title and "JSON (JSON5)" says the same thing twice.

Suggested change
Some JSON schema features cannot be expressed in TypeScript. As an escape hatch, any JSDoc tag listed with `--validation-keywords` (or `extraTags` in the programmatic config) is copied into the generated schema. The tag value is parsed as JSON (JSON5) and falls back to a plain string if it is not valid JSON.
Any JSDoc tag listed with `--validation-keywords` (or `extraTags` in the programmatic config) is copied into the generated schema. This covers JSON Schema keywords that TypeScript cannot express. The tag value is parsed as JSON5 and falls back to a plain string if it is not valid JSON5.


For example, to require exactly one of several optional properties:

```ts
/**
* @oneOf [
* { "required": ["x"] },
* { "required": ["y"] },
* { "required": ["z"] }
* ]
*/
export interface MyObject {
x?: number;
y?: number;
z?: number;
}
```

```bash
npx ts-json-schema-generator --path 'my/file.ts' --type 'MyObject' --validation-keywords oneOf
```

generates

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bare lowercase "generates" after a code block reads as a fragment. The output below is also not the real output: the generator wraps it in $ref/definitions and sorts keys, as in the test's schema.json. Say it is abridged.

Suggested change
generates
Output (abridged):


```json
{
"type": "object",
"properties": {
"x": { "type": "number" },
"y": { "type": "number" },
"z": { "type": "number" }
},
"oneOf": [{ "required": ["x"] }, { "required": ["y"] }, { "required": ["z"] }],
"additionalProperties": false
}
```

The same mechanism can be used for non-standard keywords consumed by other tools (e.g. `--validation-keywords faker`).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The same mechanism can be used for non-standard keywords consumed by other tools (e.g. `--validation-keywords faker`).
Non-standard keywords for other tools work the same way, for example `--validation-keywords faker`.


## Programmatic Usage

```js
Expand Down
15 changes: 15 additions & 0 deletions test/valid-data/annotation-custom-keyword/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { assertValidSchema } from "../../utils";
import { test } from "node:test";

test(
"valid-data - annotation-custom-keyword",
assertValidSchema(
"annotation-custom-keyword",
"MyObject",
{ jsDoc: "basic", extraTags: ["oneOf"] },
{
validSamples: [{ x: 1 }, { y: 2 }, { z: 3 }],
invalidSamples: [{}, { x: 1, y: 2 }, { x: 1, y: 2, z: 3 }],
},
),
);
12 changes: 12 additions & 0 deletions test/valid-data/annotation-custom-keyword/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @oneOf [
* { "required": ["x"] },
* { "required": ["y"] },
* { "required": ["z"] }
* ]
*/
export interface MyObject {
x?: number;
y?: number;
z?: number;
}
38 changes: 38 additions & 0 deletions test/valid-data/annotation-custom-keyword/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$ref": "#/definitions/MyObject",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyObject": {
"additionalProperties": false,
"oneOf": [
{
"required": [
"x"
]
},
{
"required": [
"y"
]
},
{
"required": [
"z"
]
}
],
"properties": {
"x": {
"type": "number"
},
"y": {
"type": "number"
},
"z": {
"type": "number"
}
},
"type": "object"
}
}
}