Skip to content

Commit 872a40a

Browse files
authored
Issue #168 port jtd-esm-codegen onto the incubator main
Closes #168
1 parent 6aaa7b9 commit 872a40a

24 files changed

Lines changed: 3577 additions & 2 deletions

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
for k in totals: totals[k]+=int(r.get(k,'0'))
4040
except Exception:
4141
pass
42-
exp_tests=1679
42+
exp_tests=2038
4343
exp_skipped=0
4444
if totals['tests']!=exp_tests or totals['skipped']!=exp_skipped:
4545
print(f"Unexpected test totals: {totals} != expected tests={exp_tests}, skipped={exp_skipped}")

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,3 @@ pom.xml.versionsBackup
4141
jtd*.log
4242
**/jtd*.log
4343
.DS_Store
44-
jtd-esm-codegen/

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ See `README.md` for user-facing commands. When running locally as an agent, use
152152
- `json-compatibility-suite`: JSON Test Suite compatibility validation.
153153
- `json-java21-jtd`: JSON Type Definition (JTD) validator based on RFC 8927.
154154
- `json-java21-jsonpath`: JsonPath query engine over `jdk.incubator.java.util.json` values.
155+
- `jtd-esm-codegen`: Experimental JTD (RFC 8927) to ES2020 ESM validator code generator for JS consumers; 316/316 official validation.json conformance. See its README and `JTD_CODEGEN_SPEC.md` before working on it.
155156

156157
Only when you are asked to work on a specific module, start by reading that module's `README.md`, then its `AGENTS.md`.
157158

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ This repo is organized into the following modules:
3636
| `json-java21-jsonpath` | JsonPath query engine over `jdk.incubator.java.util.json` values (Goessner-style: filters, slices, recursive descent, unions) | 21+ |
3737
| `json-compatibility-suite` | JSON Test Suite conformance reporter (tests against [nst/JSONTestSuite](https://github.com/nst/JSONTestSuite)) | 21+ |
3838
| `json-java21-api-tracker` | Daily upstream API drift detector — fetches OpenJDK sandbox sources, compares public API signatures, reports differences | 25+ |
39+
| `jtd-esm-codegen` | Experimental JTD → ES2020 ESM validator code generator for JS/browser consumers | 21+ |
3940

4041
We welcome contributions to the incubating modules.
4142

@@ -413,6 +414,40 @@ An optional `jtd2jar` CLI tool and distroless Docker image are available for pre
413414

414415
See [`jtd2jar/README.md`](jtd2jar/README.md) for build instructions, container usage, and the pre-built image on GitHub Container Registry (`ghcr.io`).
415416

417+
## JTD to ESM Validator Codegen (Experimental)
418+
419+
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. The generator passes the full official JTD conformance suite (316/316 cases from `validation.json`) and its output is executed in GraalJS during the test run.
420+
421+
### Supported JTD subset (flat schemas only)
422+
423+
This tool deliberately supports only:
424+
- `properties` (required properties)
425+
- `optionalProperties`
426+
- `type` primitives (`string`, `boolean`, `timestamp`, `int8`, `int16`, `int32`, `uint8`, `uint16`, `uint32`, `float32`, `float64`)
427+
- `enum`
428+
- `metadata.id` (used for the output filename prefix)
429+
430+
It rejects other JTD features (`elements`, `values`, `discriminator`/`mapping`, `ref`/`definitions`) and also rejects **nested `properties`** (object schemas inside properties).
431+
432+
When rejected, the error message is:
433+
434+
`Unsupported JTD feature: <feature>. This experimental tool only supports flat schemas with properties, optionalProperties, type, and enum.`
435+
436+
### Build and run
437+
438+
```bash
439+
./mvnw -pl jtd-esm-codegen -am package
440+
java -jar ./jtd-esm-codegen/target/jtd-esm-codegen.jar schema.jtd.json
441+
```
442+
443+
The output file is written to the current directory as:
444+
445+
`<metadata.id>-<sha256_prefix_8>.js`
446+
447+
Where `<sha256_prefix_8>` is the first 8 characters of the SHA-256 hash of the input schema file bytes.
448+
449+
See [`jtd-esm-codegen/JTD_CODEGEN_SPEC.md`](jtd-esm-codegen/JTD_CODEGEN_SPEC.md) for the generated-code specification.
450+
416451
## Building
417452

418453
Requires JDK 21 or later. Build with Maven:

0 commit comments

Comments
 (0)