Skip to content

docs: document custom JSDoc tags as an escape hatch for arbitrary schema keywords - #2565

Open
Labu1 wants to merge 2 commits into
vega:nextfrom
Labu1:repodestek/issue-795
Open

Labu1 wants to merge 2 commits into
vega:nextfrom
Labu1:repodestek/issue-795

Conversation

@Labu1

@Labu1 Labu1 commented Nov 23, 2026

Copy link
Copy Markdown

What

The escape hatch requested in #795 already exists: any JSDoc tag passed via --validation-keywords (CLI) or extraTags (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:

  • adds a Custom JSDoc tags section to the README explaining the mechanism, using the oneOf example from the issue (and mentioning non-standard keywords such as faker from the comments);
  • adds a valid-data/annotation-custom-keyword test with a multi-line @oneOf tag on an interface with optional x/y/z properties. 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 test in a clean Linux container: passed.


This PR was prepared with AI assistance (Claude) and reviewed by me before submitting.

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 arthurfiorette left a comment

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.

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.

Comment thread README.md Outdated

### 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.

Comment thread README.md Outdated
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):

Comment thread README.md Outdated
}
```

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`.

Apply review wording suggestions, mark the sample output as abridged and
show how to pass extraTags through the programmatic config.
@Labu1

Labu1 commented Nov 23, 2026

Copy link
Copy Markdown
Author

Thanks for the review, @arthurfiorette!

  • Intro paragraph (README.md:68): I applied your suggestion. It now says "JSON Schema", drops "escape hatch" and just says "JSON5".
  • "generates" (README.md:91): I replaced it with "Output (abridged):", so it's clear the generator's real output also has the $ref/definitions wrapper and sorted keys (the full output is in test/valid-data/annotation-custom-keyword/schema.json).
  • Last sentence (README.md:106): I applied your suggestion.
  • Programmatic example: I added a short config snippet with extraTags: ["oneOf"] after the CLI command, so extraTags is no longer just mentioned in passing.
  • Overlap with docs: document the available JSDoc annotations #2564: I left this open for now. The author will decide which PR owns the --validation-keywords / extraTags text and will add a link from the other one.

This change only touches docs. Ran npm test in a clean Linux container: passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can we have an escape hatch via some jsdoc annotation to insert arbitrary json-schema at a particular place?

2 participants