Skip to content

Shubh-Raj/rs-schema-validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rs-schema-validator

A minimal schema validation engine in Rust, designed for cross-language behavioral parity with a JavaScript reference implementation.

Features

  • Type validation: string, number, boolean, object, array, null
  • Required fields: Ensures mandatory properties are present
  • Numeric constraints: min, max
  • String constraints: minLength, maxLength
  • Array validation: items (recursive), minItems, maxItems
  • Nested objects: Recursive property validation with nested required
  • Enum validation: Restrict values to an allowed set
  • Pattern matching: Regex-based string validation

Project Structure

├── src/
│   ├── main.rs          # CLI entry point
│   ├── lib.rs           # Module declarations
│   ├── schema.rs        # Schema types (SchemaType, PropertyRule, Schema)
│   └── validator.rs     # Validation logic + unit tests
├── js/
│   ├── validate.js      # JavaScript reference validator
│   └── package.json
├── tests/
│   ├── golden/          # Golden test cases (schema + input + expected)
│   └── golden_tests.rs  # Integration test runner
├── scripts/
│   └── run_parity_tests.sh  # Cross-language parity runner
├── Cargo.toml
└── README.md

Usage

Rust Validator

cargo build --release
./target/release/rs-schema-validator schema.json input.json

JavaScript Validator

node js/validate.js schema.json input.json

Both output VALID or INVALID: <error message> lines.

Example Schema

{
  "type": "object",
  "required": ["name", "age"],
  "properties": {
    "name": { "type": "string", "minLength": 2 },
    "age": { "type": "number", "min": 18, "max": 120 },
    "tags": {
      "type": "array",
      "items": { "type": "string" },
      "minItems": 1
    },
    "status": {
      "type": "string",
      "enum": ["active", "inactive"]
    }
  }
}

Testing

Unit Tests

cargo test

Golden Tests

cargo test --test golden_tests

Cross-Language Parity

./scripts/run_parity_tests.sh

Runs both Rust and JS validators against all golden test cases and verifies output parity.

Design Decisions

  • Deterministic errors: Validation errors follow a consistent format across both implementations.
  • Recursive validation: PropertyRule is recursive (using Box) to support nested objects and array items.
  • Struct update syntax: Tests use ..simple_rule() for concise PropertyRule construction.
  • No external schema standards: This is a custom, minimal schema format — not JSON Schema.

License

MIT License. See LICENSE.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published