Skip to content

Zheng-Bote/schema_validator

Repository files navigation

schema_validator

A portable C++23 command-line tool for validating data files (JSON, YAML, TOML) against a schema.

Features

  • Multi-Format Support: Read Schema and Data files in JSON, YAML, or TOML format.
  • C++23: Uses std::expected, std::println, and std::ranges.
  • Portable: Compiles on Linux and Windows.
  • Static Linking: Produces a self-contained executable.
  • JSON Output: Machine-readable validation results.

Supported Schema Keywords

The validator supports the following JSON-Schema-like keywords:

General

  • type: string, number, integer, boolean, object, array.
  • enum: List of allowed values.
  • const: Exactly one allowed value.

Numbers

  • minimum: Minimum value allowed.
  • maximum: Maximum value allowed.
  • pattern: Regular expression match. The numeric value is converted to its string representation before the check.
    • Example: To ensure an integer has between 6 and 8 digits: "pattern": "^[0-9]{6,8}$"

Strings

  • minLength: Minimum character length.
  • maxLength: Maximum character length.
  • pattern: Regular expression match.

Arrays

  • minItems: Minimum number of items.
  • maxItems: Maximum number of items.
  • uniqueItems: If true, all items must be unique.
  • items: Schema for each item in the array.

Objects

  • properties: Map of keys to sub-schemas.
  • required: List of required property keys.

Prerequisites

  • CMake 3.25+
  • Conan 2.x
  • C++23 compliant compiler (GCC 13+, Clang 16+, or MSVC 19.34+)

Building

1. Install Dependencies

conan install . --output-folder=build --build=missing -s build_type=Release

This command will generate a CMakeUserPresets.json in the project root, which includes the necessary build presets.

2. Configure and Build

cmake --preset conan-release
cmake --build --preset conan-release

Example

Schema (schema.json)

{
    "type": "object",
    "properties": {
        "project_name": { 
            "type": "string",
            "minLength": 3,
            "pattern": "^[A-Z].*" 
        },
        "version": { 
            "type": "integer",
            "minimum": 1 
        },
        "active": { "type": "boolean" },
        "environment": { 
            "type": "string", 
            "enum": ["development", "staging", "production"] 
        },
        "tags": {
            "type": "array",
            "minItems": 1,
            "uniqueItems": true,
            "items": { "type": "string" }
        }
    },
    "required": ["project_name", "version", "active"]
}

Data (data.yaml)

project_name: "ValidatorProject"
version: 1
active: true
environment: "production"
tags:
  - "cpp23"
  - "validation"

Validation

./schema_validator --schema schema.json --data data.yaml
# Result: valid

License

This project is licensed under the Apache-2.0 License. Copyright (c) 2026 ZHENG Robert.

About

portable C++23 command-line tool for validating data files (JSON, YAML, TOML) against a schema.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors