JSON Structure is a data structure definition language that enforces strict typing, modularity, and determinism.
Ruby SDK for JSON Structure schema validation using FFI bindings to the C library.
Add this line to your application’s Gemfile:
gem 'jsonstructure'
And then execute:
bundle install
Or install it yourself as:
gem install jsonstructure
The gem automatically downloads pre-built C library binaries from GitHub releases. No compilation is required during installation.
Supported platforms:
If you’re developing the gem itself or contributing, see the Development section below for building from source.
require 'jsonstructure'
schema = '{"type": "string", "minLength": 1}'
result = JsonStructure::SchemaValidator.validate(schema)
if result.valid?
puts "Schema is valid!"
else
result.errors.each do |error|
puts "Error: #{error.message}"
end
end
require 'jsonstructure'
schema = '{"type": "string", "minLength": 1}'
instance = '"hello"'
result = JsonStructure::InstanceValidator.validate(instance, schema)
if result.valid?
puts "Instance is valid!"
else
result.errors.each do |error|
puts "Error: #{error.message}"
puts " Path: #{error.path}" if error.path
end
end
Both validators provide a validate! method that raises an exception on validation failure:
require 'jsonstructure'
begin
schema = '{"type": "string"}'
JsonStructure::SchemaValidator.validate!(schema)
puts "Schema is valid!"
rescue JsonStructure::SchemaValidationError => e
puts "Schema validation failed: #{e.message}"
e.errors.each { |err| puts " - #{err.message}" }
end
begin
instance = '123'
schema = '{"type": "string"}'
JsonStructure::InstanceValidator.validate!(instance, schema)
puts "Instance is valid!"
rescue JsonStructure::InstanceValidationError => e
puts "Instance validation failed: #{e.message}"
e.errors.each { |err| puts " - #{err.message}" }
end
result = JsonStructure::InstanceValidator.validate(instance, schema)
# Check validity
puts result.valid? # => true or false
puts result.invalid? # => opposite of valid?
# Access errors
result.errors.each do |error|
puts error.code # Error code
puts error.severity # Severity level
puts error.message # Human-readable message
puts error.path # JSON Pointer path to error location
puts error.location # Hash with :line, :column, :offset
# Check error type
puts error.error? # true if severity is ERROR
puts error.warning? # true if severity is WARNING
puts error.info? # true if severity is INFO
end
# Get error/warning messages
error_msgs = result.error_messages # Array of error messages
warning_msgs = result.warning_messages # Array of warning messages
JsonStructure::SchemaValidatorvalidate(schema_json) - Validate a schema, returns ValidationResultvalidate!(schema_json) - Validate a schema, raises SchemaValidationError on failureJsonStructure::InstanceValidatorvalidate(instance_json, schema_json) - Validate an instance against a schema, returns ValidationResultvalidate!(instance_json, schema_json) - Validate an instance, raises InstanceValidationError on failureJsonStructure::ValidationResultvalid? - Returns true if validation passedinvalid? - Returns true if validation failederrors - Array of ValidationError objectserror_messages - Array of error message stringswarning_messages - Array of warning message stringsJsonStructure::ValidationErrorcode - Error code (integer)severity - Severity level (ERROR, WARNING, or INFO)message - Human-readable error messagepath - JSON Pointer path to error location (may be nil)location - Hash with :line, :column, :offset keyserror?, warning?, info? - Check severity levelThis library is thread-safe. Multiple threads can perform validations concurrently without any external synchronization.
require 'jsonstructure'
schema = '{"type": "object", "properties": {"name": {"type": "string"}}}'
# Safe to validate from multiple threads simultaneously
threads = 10.times.map do |i|
Thread.new do
instance = %Q({"name": "Thread #{i}"})
result = JsonStructure::InstanceValidator.validate(instance, schema)
puts "Thread #{i}: #{result.valid? ? 'valid' : 'invalid'}"
end
end
threads.each(&:join)
at_exit cleanup hook coordinates with active validations to avoid races during process shutdownPrefer stateless validation: Each validate call is independent. There’s no need to create validator instances or manage state.
Thread-local results: ValidationResult objects returned by validate are thread-local and can be safely used without synchronization.
Exception safety: If a validation raises an exception (e.g., ArgumentError for invalid input), the library state remains consistent.
After checking out the repo, run bundle install to install dependencies.
When developing the gem with the C library in the same repository:
# Build the C library locally
rake build_c_lib_local
# Run the tests
rake test
The rake test task will attempt to download a pre-built binary first. If that fails (e.g., during development), it will fall back to building the C library from source if available.
Production installations automatically download pre-built binaries from GitHub releases. No local build is required:
# Just install and use
gem install jsonstructure
# The binary will be downloaded automatically on first require
This Ruby SDK treats the JSON Structure C library as an external dependency, downloading pre-built binaries from GitHub releases. The architecture consists of:
.so, .dylib, .dll) distributed via GitHub releaseslib/jsonstructure/binary_installer.rb) - Downloads and installs platform-specific binarieslib/jsonstructure/ffi.rb) - Low-level FFI mappings to C functionsSchemaValidator - Schema validationInstanceValidator - Instance validationValidationResult - Result containerValidationError - Error informationThis design allows the Ruby SDK to be distributed independently of the C library source code, treating it as a foreign SDK dependency.
Bug reports and pull requests are welcome on GitHub at https://github.com/json-structure/sdk.
The gem is available as open source under the terms of the MIT License.