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(File.expand_path(File.dirname(__FILE__)), SCHEMA_DIR)
BASE_SCHEMAS =
%i[occi-schema].freeze
DELEGATED =

Shortcuts to interesting methods on logger

%i[debug? info? warn? error? fatal?].freeze

Class Method Summary collapse

Class Method Details

.schema_for(type) ⇒ Object

:nodoc:


73
74
75
76
77
78
79
80
81
# File 'lib/occi/core/parsers/json/validator.rb', line 73

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}" if logger_debug?

  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:


28
29
30
31
32
33
34
35
# File 'lib/occi/core/parsers/json/validator.rb', line 28

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

  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:


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

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

.validate_category_identifiers!(json) ⇒ Object

:nodoc:


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

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

.validate_entity_collection!(json) ⇒ Object

:nodoc:


68
69
70
# File 'lib/occi/core/parsers/json/validator.rb', line 68

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

.validate_link!(json) ⇒ Object

:nodoc:


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

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

.validate_locations!(json) ⇒ Object

:nodoc:


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

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

.validate_model!(json) ⇒ Object

:nodoc:


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

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

.validate_resource!(json) ⇒ Object

:nodoc:


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

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