rust-wasm-validator is a project showing how Rust validation
logic can be compiled to WebAssembly and called from JavaScript.
This project demonstrates an architecture where:
- Validation logic is implemented in Rust.
- Rust code is compiled to WASM.
- JavaScript calls the WASM module instead of implementing validation itself.
The approach is similar in spirit to Concerto's Rust runtime direction, where runtime logic can move from JavaScript into Rust for consistency and reuse.
The validator supports a small subset of schema rules:
typechecks:string,number,objectrequiredfields for objects- numeric
minconstraints - nested object validation through
properties
- Input boundary: JavaScript sends schema/data JSON as strings.
- WASM boundary:
validate_jsonin Rust is exported viawasm-bindgen. - Runtime boundary: Rust performs all validation decisions.
- Output boundary: Rust returns a structured JSON result string to JavaScript.
Validation errors return a structured payload with a precise dot path like
user.address.zip.
Example output:
{
"valid": false,
"error": {
"path": "age",
"message": "value must be >= 18"
}
}Rust exposes the WASM function:
validate_json(schema_json: &str, instance_json: &str) -> StringIt:
- Parses both JSON strings.
- Runs validation recursively.
- Returns the result as JSON text.
Install wasm-pack if needed, then run:
wasm-pack build --target webThis generates the pkg/ files consumed by JavaScript.
cargo testAfter generating pkg/ with wasm-pack, run:
node js-demo/index.jsThe demo loads:
js-demo/example-schema.jsonjs-demo/example-data.json
and prints the validator result returned by Rust/WASM.
This project illustrates a practical migration path:
- keep JS orchestration thin
- move deterministic runtime logic into Rust
- reuse the same Rust behavior in any JS environment that can load WASM
That architecture helps replace duplicated JavaScript runtime logic with a single Rust implementation.