Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ jobs:
for k in totals: totals[k]+=int(r.get(k,'0'))
except Exception:
pass
exp_tests=611
exp_skipped=0
exp_tests=639
exp_skipped=5
if totals['tests']!=exp_tests or totals['skipped']!=exp_skipped:
print(f"Unexpected test totals: {totals} != expected tests={exp_tests}, skipped={exp_skipped}")
sys.exit(1)
Expand Down
142 changes: 142 additions & 0 deletions .github/workflows/jtd-esm-codegen-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
name: JTD-ESM-Codegen Nightly

on:
schedule:
- cron: '0 3 * * *' # 3 AM UTC daily
workflow_dispatch:

permissions:
contents: write

jobs:
build-uber-jar:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '21'
cache: 'maven'
- name: Build uber JAR
run: |
./mvnw -pl jtd-esm-codegen -am package
cp jtd-esm-codegen/target/jtd-esm-codegen.jar jtd-esm-codegen.jar
- uses: actions/upload-artifact@v4
with:
name: uber-jar
path: jtd-esm-codegen.jar

native-linux:
needs: build-uber-jar
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: uber-jar
path: .
- uses: graalvm/setup-graalvm@v1
with:
java-version: '21'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
components: 'native-image'
cache: 'maven'
- name: Build native image
run: |
native-image --no-fallback -jar jtd-esm-codegen.jar jtd-esm-codegen-linux-amd64
chmod +x jtd-esm-codegen-linux-amd64
- uses: actions/upload-artifact@v4
with:
name: native-linux-amd64
path: jtd-esm-codegen-linux-amd64

native-windows:
needs: build-uber-jar
runs-on: windows-latest
steps:
- uses: actions/download-artifact@v4
with:
name: uber-jar
path: .
- uses: graalvm/setup-graalvm@v1
with:
java-version: '21'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
components: 'native-image'
cache: 'maven'
- name: Build native image
run: |
native-image --no-fallback -jar jtd-esm-codegen.jar jtd-esm-codegen-windows-amd64
- uses: actions/upload-artifact@v4
with:
name: native-windows-amd64
path: jtd-esm-codegen-windows-amd64.exe

native-macos-intel:
needs: build-uber-jar
runs-on: macos-13 # Intel
steps:
- uses: actions/download-artifact@v4
with:
name: uber-jar
path: .
- uses: graalvm/setup-graalvm@v1
with:
java-version: '21'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
components: 'native-image'
cache: 'maven'
- name: Build native image
run: |
native-image --no-fallback -jar jtd-esm-codegen.jar jtd-esm-codegen-macos-amd64
chmod +x jtd-esm-codegen-macos-amd64
- uses: actions/upload-artifact@v4
with:
name: native-macos-amd64
path: jtd-esm-codegen-macos-amd64

native-macos-arm:
needs: build-uber-jar
runs-on: macos-14 # Apple Silicon
steps:
- uses: actions/download-artifact@v4
with:
name: uber-jar
path: .
- uses: graalvm/setup-graalvm@v1
with:
java-version: '21'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
components: 'native-image'
cache: 'maven'
- name: Build native image
run: |
native-image --no-fallback -jar jtd-esm-codegen.jar jtd-esm-codegen-macos-arm64
chmod +x jtd-esm-codegen-macos-arm64
- uses: actions/upload-artifact@v4
with:
name: native-macos-arm64
path: jtd-esm-codegen-macos-arm64

release:
needs: [native-linux, native-windows, native-macos-intel, native-macos-arm]
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
- name: Create nightly release
uses: softprops/action-gh-release@v1
with:
tag_name: nightly-${{ github.run_number }}
name: Nightly Build ${{ github.run_number }}
prerelease: true
files: |
native-linux-amd64/jtd-esm-codegen-linux-amd64
native-windows-amd64/jtd-esm-codegen-windows-amd64.exe
native-macos-amd64/jtd-esm-codegen-macos-amd64
native-macos-arm64/jtd-esm-codegen-macos-arm64
uber-jar/jtd-esm-codegen.jar

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

We welcome contributions to these incubating modules.

Expand Down Expand Up @@ -388,6 +389,38 @@ Features:
- ✅ Discriminator tag exemption from additional properties
- ✅ Stack-based validation preventing StackOverflowError

## JTD to ESM Validator Codegen (Experimental)

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.

### Supported JTD subset (flat schemas only)

This tool deliberately supports only:
- `properties` (required properties)
- `optionalProperties`
- `type` primitives (`string`, `boolean`, `timestamp`, `int8`, `int16`, `int32`, `uint8`, `uint16`, `uint32`, `float32`, `float64`)
- `enum`
- `metadata.id` (used for the output filename prefix)

It rejects other JTD features (`elements`, `values`, `discriminator`/`mapping`, `ref`/`definitions`) and also rejects **nested `properties`** (object schemas inside properties).

When rejected, the error message is:

`Unsupported JTD feature: <feature>. This experimental tool only supports flat schemas with properties, optionalProperties, type, and enum.`

### Build and run

```bash
./mvnw -pl jtd-esm-codegen -am package
java -jar ./jtd-esm-codegen/target/jtd-esm-codegen.jar schema.jtd.json
```

The output file is written to the current directory as:

`<metadata.id>-<sha256_prefix_8>.js`

Where `<sha256_prefix_8>` is the first 8 characters of the SHA-256 hash of the input schema file bytes.

## Building

Requires JDK 21 or later. Build with Maven:
Expand Down
Loading
Loading