Skip to content

Shubh-Raj/rust-wasm-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rust-wasm-validator

rust-wasm-validator is a project showing how Rust validation logic can be compiled to WebAssembly and called from JavaScript.

Project Goal

This project demonstrates an architecture where:

  1. Validation logic is implemented in Rust.
  2. Rust code is compiled to WASM.
  3. 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.

What Is Implemented

The validator supports a small subset of schema rules:

  • type checks: string, number, object
  • required fields for objects
  • numeric min constraints
  • nested object validation through properties

Architecture Check

  • Input boundary: JavaScript sends schema/data JSON as strings.
  • WASM boundary: validate_json in Rust is exported via wasm-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 API

Rust exposes the WASM function:

validate_json(schema_json: &str, instance_json: &str) -> String

It:

  1. Parses both JSON strings.
  2. Runs validation recursively.
  3. Returns the result as JSON text.

Build WASM

Install wasm-pack if needed, then run:

wasm-pack build --target web

This generates the pkg/ files consumed by JavaScript.

Run Rust Tests

cargo test

Run JavaScript Demo

After generating pkg/ with wasm-pack, run:

node js-demo/index.js

The demo loads:

  • js-demo/example-schema.json
  • js-demo/example-data.json

and prints the validator result returned by Rust/WASM.

Why This Matters

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.

About

This shows how Rust validation logic can be compiled to WebAssembly and called from JavaScript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors