Class: Equilibrium::SchemaValidator

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

Overview

Reusable schema validation utility Centralizes JSON Schema validation logic across the application

Defined Under Namespace

Classes: ValidationError

Class Method Summary collapse

Class Method Details

.validate(data, schema) ⇒ Array

Validates data against a JSON Schema and returns validation errors

Parameters:

  • data (Hash)

    The data to validate

  • schema (Hash)

    The JSON Schema to validate against

Returns:

  • (Array)

    Array of validation errors (empty if valid)



32
33
34
35
# File 'lib/equilibrium/schema_validator.rb', line 32

def self.validate(data, schema)
  schemer = JSONSchemer.schema(schema)
  schemer.validate(data).to_a
end

.validate!(data, schema, error_prefix: "Schema validation failed") ⇒ Object

Validates data against a JSON Schema

Parameters:

  • data (Hash)

    The data to validate

  • schema (Hash)

    The JSON Schema to validate against

  • error_prefix (String) (defaults to: "Schema validation failed")

    Prefix for error messages (optional)

Raises:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/equilibrium/schema_validator.rb', line 16

def self.validate!(data, schema, error_prefix: "Schema validation failed")
  errors = validate(data, schema)

  return if errors.empty?

  error_messages = errors.map do |error|
    "#{error["data_pointer"]}: #{error["details"] || error["error"]}"
  end

  raise ValidationError, "#{error_prefix}:\n#{error_messages.join("\n")}"
end