Class: JsonStructure::SchemaValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonstructure/schema_validator.rb

Overview

Validates JSON Structure schema documents

This class is thread-safe. Multiple threads can call validate concurrently.

Class Method Summary collapse

Class Method Details

.validate(schema_json) ⇒ ValidationResult

Validate a schema string

This method is thread-safe and can be called from multiple threads concurrently.

Examples:

schema = '{"type": "string", "minLength": 1}'
result = JsonStructure::SchemaValidator.validate(schema)
if result.valid?
  puts "Schema is valid!"
else
  result.errors.each { |e| puts e.message }
end

Parameters:

  • schema_json (String)

    JSON string containing the schema

Returns:

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jsonstructure/schema_validator.rb', line 23

def self.validate(schema_json)
  raise ArgumentError, 'schema_json must be a String' unless schema_json.is_a?(String)

  JsonStructure.validation_started
  begin
    result_ptr = ::FFI::MemoryPointer.new(FFI::JSResult.size)
    FFI.js_result_init(result_ptr)

    FFI.js_validate_schema(schema_json, result_ptr)
    ValidationResult.from_ffi(result_ptr)
  ensure
    JsonStructure.validation_completed
  end
end

.validate!(schema_json) ⇒ ValidationResult

Validate a schema string, raising an exception on failure

Examples:

begin
  JsonStructure::SchemaValidator.validate!(schema)
  puts "Schema is valid!"
rescue JsonStructure::SchemaValidationError => e
  puts "Validation failed: #{e.message}"
end

Parameters:

  • schema_json (String)

    JSON string containing the schema

Returns:

Raises:



51
52
53
54
55
56
# File 'lib/jsonstructure/schema_validator.rb', line 51

def self.validate!(schema_json)
  result = validate(schema_json)
  raise SchemaValidationError.new(result) unless result.valid?

  result
end