Repository navigation
Conversation
The generator already supports copying arbitrary JSDoc tags into the output schema via `--validation-keywords` (CLI) / `extraTags` (config), with tag values parsed as JSON5. This was not documented, so users kept asking for an escape hatch to inject keywords like `oneOf`. Add a README section describing the mechanism with the `oneOf` example from the issue, and add a test that covers a multi-line `@oneOf` tag on an interface, including validation of valid and invalid samples. Fixes vega#795.
arthurfiorette
left a comment
There was a problem hiding this comment.
Writing pass. #2564 also documents --validation-keywords / extraTags (last paragraph of its "Keyword tags" section). Decide which PR owns that text and link from the other. This section has no programmatic example, so extraTags is only mentioned in passing.
|
|
||
| ### 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. |
There was a problem hiding this comment.
"JSON schema" should be "JSON Schema". "Escape hatch" is jargon from the issue title and "JSON (JSON5)" says the same thing twice.
| 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. |
| npx ts-json-schema-generator --path 'my/file.ts' --type 'MyObject' --validation-keywords oneOf | ||
| ``` | ||
|
|
||
| generates |
There was a problem hiding this comment.
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.
| generates | |
| Output (abridged): |
| } | ||
| ``` | ||
|
|
||
| The same mechanism can be used for non-standard keywords consumed by other tools (e.g. `--validation-keywords faker`). |
There was a problem hiding this comment.
| 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`. |
Apply review wording suggestions, mark the sample output as abridged and show how to pass extraTags through the programmatic config.
|
Thanks for the review, @arthurfiorette!
This change only touches docs. Ran |
What
The escape hatch requested in #795 already exists: any JSDoc tag passed via
--validation-keywords(CLI) orextraTags(programmatic config) is copied into the generated schema, with its value parsed as JSON5 (falling back to a string). It was never documented, though, which is why the issue is still being asked about.This PR:
oneOfexample from the issue (and mentioning non-standard keywords such asfakerfrom the comments);valid-data/annotation-custom-keywordtest with a multi-line@oneOftag on an interface with optionalx/y/zproperties. Besides comparing against the expected schema, it validates samples so that exactly one of the properties must be present.No source code changes are needed.
Fixes #795
Testing
Ran
npm testin a clean Linux container: passed.This PR was prepared with AI assistance (Claude) and reviewed by me before submitting.