Class: Occi::Core::Parsers::Json::Validator

Inherits:
Object
  • Object
show all
Includes:
Yell::Loggable
Defined in:
lib/occi/core/parsers/json/validator.rb

Overview

Class responsible for validating JSON content before parsing. This should be called from every parsing class.

Author:

Constant Summary collapse

SCHEMA_DIR =

Repository constants

'validator'.freeze
SCHEMA_REPO =
File.join(__dir__, SCHEMA_DIR)
BASE_SCHEMAS =
%i[occi-schema].freeze

Class Method Summary collapse

Class Method Details

.schema_for(type) ⇒ Object

:nodoc:



69
70
71
72
73
74
75
76
77
# File 'lib/occi/core/parsers/json/validator.rb', line 69

def schema_for(type)
  if type.blank? || BASE_SCHEMAS.include?(type)
    raise Occi::Core::Errors::ParserError, "Schema type #{type.inspect} is not allowed"
  end
  schema_path = File.join(SCHEMA_REPO, "#{type}.json")
  logger.debug { "Found JSON schema for #{type} in #{schema_path}" }

  schema_path
end

.validate!(json, type) ⇒ Object

Validates given ‘json` text with the appropriate schema for `type`. This method raises `Occi::Core::Errors::ParsingError` on failure.

Parameters:

  • json (String)

    JSON text to validate

  • type (Symbol)

    schema selector

Raises:



24
25
26
27
28
29
30
31
# File 'lib/occi/core/parsers/json/validator.rb', line 24

def validate!(json, type)
  logger.debug { "Validating #{json.inspect} as #{type}" }

  JSON::Validator.schema_reader = JSON::Schema::Reader.new(accept_uri: false, accept_file: true)
  JSON::Validator.validate!(schema_for(type), json, json: true)
rescue JSON::Schema::JsonParseError, JSON::Schema::ValidationError => e
  raise Occi::Core::Errors::ParsingError, e.message
end

.validate_action_instance!(json) ⇒ Object

:nodoc:



49
50
51
# File 'lib/occi/core/parsers/json/validator.rb', line 49

def validate_action_instance!(json)
  validate! json, :'action-instance'
end

.validate_category_identifiers!(json) ⇒ Object

:nodoc:



39
40
41
# File 'lib/occi/core/parsers/json/validator.rb', line 39

def validate_category_identifiers!(json)
  validate! json, :'category-identifiers'
end

.validate_entity_collection!(json) ⇒ Object

:nodoc:



64
65
66
# File 'lib/occi/core/parsers/json/validator.rb', line 64

def validate_entity_collection!(json)
  validate! json, :'entity-collection'
end

.validate_link!(json) ⇒ Object

:nodoc:



59
60
61
# File 'lib/occi/core/parsers/json/validator.rb', line 59

def validate_link!(json)
  validate! json, :link
end

.validate_locations!(json) ⇒ Object

:nodoc:



34
35
36
# File 'lib/occi/core/parsers/json/validator.rb', line 34

def validate_locations!(json)
  validate! json, :locations
end

.validate_model!(json) ⇒ Object

:nodoc:



44
45
46
# File 'lib/occi/core/parsers/json/validator.rb', line 44

def validate_model!(json)
  validate! json, :model
end

.validate_resource!(json) ⇒ Object

:nodoc:



54
55
56
# File 'lib/occi/core/parsers/json/validator.rb', line 54

def validate_resource!(json)
  validate! json, :resource
end