Class: RequestHandler::Validation::DefinitionEngine

Inherits:
Engine
  • Object
show all
Defined in:
lib/request_handler/validation/definition_engine.rb

Class Method Summary collapse

Class Method Details

.error_message(validation_error) ⇒ Object



26
27
28
# File 'lib/request_handler/validation/definition_engine.rb', line 26

def self.error_message(validation_error)
  validation_error.message
end

.error_pointer(validation_error) ⇒ Object



30
31
32
# File 'lib/request_handler/validation/definition_engine.rb', line 30

def self.error_pointer(validation_error)
  validation_error.error_path.join('/')
end

.valid_schema?(definition) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/request_handler/validation/definition_engine.rb', line 9

def self.valid_schema?(definition)
  definition.is_a?(::Definition::Types::Base)
end

.validate(value, schema, options: {}) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



13
14
15
16
17
# File 'lib/request_handler/validation/definition_engine.rb', line 13

def self.validate(value, schema, options: {}) # rubocop:disable Lint/UnusedMethodArgument
  value = value.deep_symbolize_keys if value.is_a?(Hash)
  result = schema.conform(value)
  Result.new(output: result.value, errors: result.error_hash)
end

.validate!(value, schema, options: {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/request_handler/validation/definition_engine.rb', line 19

def self.validate!(value, schema, options: {})
  validate(value, schema, options).tap do |result|
    valid = result.respond_to?(:valid?) ? result.valid? : result.success?
    raise Validation::Error unless valid
  end
end