Skip to content

Commit 3faec69

Browse files
Issue #136 Document experimental JTD ESM codegen CLI
Co-authored-by: Simon Massey <simbo1905@users.noreply.github.com>
1 parent d943174 commit 3faec69

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ In addition to the core backport, this repo includes implementations of more adv
1515
| --- | --- | --- |
1616
| `json-java21-jtd` | JSON Type Definition (JTD) validator implementing RFC 8927 | [JTD validator](#json-type-definition-jtd-validator) |
1717
| `json-java21-jsonpath` | JsonPath query engine over `java.util.json` values | [JsonPath](#jsonpath) |
18+
| `jtd-esm-codegen` | Experimental JTD → ES2020 ESM validator code generator | [JTD → ESM codegen](#jtd-to-esm-validator-codegen-experimental) |
1819

1920
We welcome contributions to these incubating modules.
2021

@@ -388,6 +389,38 @@ Features:
388389
- ✅ Discriminator tag exemption from additional properties
389390
- ✅ Stack-based validation preventing StackOverflowError
390391

392+
## JTD to ESM Validator Codegen (Experimental)
393+
394+
This repo also contains an **experimental** CLI tool that reads a JTD schema (RFC 8927) and generates a **vanilla ES2020 module** exporting a `validate(instance)` function. The intended use case is validating JSON event payloads in the browser (for example, across tabs using `BroadcastChannel`) without a build step.
395+
396+
### Supported JTD subset (flat schemas only)
397+
398+
This tool deliberately supports only:
399+
- `properties` (required properties)
400+
- `optionalProperties`
401+
- `type` primitives (`string`, `boolean`, `timestamp`, `int8`, `int16`, `int32`, `uint8`, `uint16`, `uint32`, `float32`, `float64`)
402+
- `enum`
403+
- `metadata.id` (used for the output filename prefix)
404+
405+
It rejects other JTD features (`elements`, `values`, `discriminator`/`mapping`, `ref`/`definitions`) and also rejects **nested `properties`** (object schemas inside properties).
406+
407+
When rejected, the error message is:
408+
409+
`Unsupported JTD feature: <feature>. This experimental tool only supports flat schemas with properties, optionalProperties, type, and enum.`
410+
411+
### Build and run
412+
413+
```bash
414+
./mvnw -pl jtd-esm-codegen -am package
415+
java -jar ./jtd-esm-codegen/target/jtd-esm-codegen.jar schema.jtd.json
416+
```
417+
418+
The output file is written to the current directory as:
419+
420+
`<metadata.id>-<sha256_prefix_8>.js`
421+
422+
Where `<sha256_prefix_8>` is the first 8 characters of the SHA-256 hash of the input schema file bytes.
423+
391424
## Building
392425

393426
Requires JDK 21 or later. Build with Maven:

0 commit comments

Comments
 (0)