Repository navigation
docs: document custom JSDoc tags as an escape hatch for arbitrary schema keywords #2565
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
base: next
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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. | ||||||
|
|
||||||
| 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 | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||
|
|
||||||
| ```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`). | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ## Programmatic Usage | ||||||
|
|
||||||
| ```js | ||||||
|
|
||||||
| 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 }], | ||
| }, | ||
| ), | ||
| ); |
| 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; | ||
| } |
| 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" | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
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.