Class: Philiprehberger::JsonSchema::CompiledSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/json_schema/validator.rb

Overview

Compiled schema for repeated validation without re-parsing

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ CompiledSchema

Returns a new instance of CompiledSchema.

Parameters:

  • schema (Hash)

    the JSON Schema to compile



374
375
376
377
# File 'lib/philiprehberger/json_schema/validator.rb', line 374

def initialize(schema)
  @schema = schema
  @validator = Validator.new
end

Instance Method Details

#valid?(data) ⇒ Boolean

Check if data is valid against the compiled schema

Parameters:

  • data (Object)

    the data to validate

Returns:

  • (Boolean)

    true if valid



391
392
393
# File 'lib/philiprehberger/json_schema/validator.rb', line 391

def valid?(data)
  validate(data).empty?
end

#validate(data) ⇒ Array<String>

Validate data against the compiled schema

Parameters:

  • data (Object)

    the data to validate

Returns:

  • (Array<String>)

    list of validation errors



383
384
385
# File 'lib/philiprehberger/json_schema/validator.rb', line 383

def validate(data)
  @validator.validate(data, @schema)
end