Class: Apipie::Params::Descriptor::JsonSchema

Inherits:
Base
  • Object
show all
Defined in:
lib/apipie/params/descriptor.rb

Direct Known Subclasses

Array, Enum, Hash, Integer, Regexp, String, Undef

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#description, find, #initialize, #invalid_param_error, #to_json

Constructor Details

This class inherits a constructor from Apipie::Params::Descriptor::Base

Class Method Details

.build(*args) ⇒ Object



63
64
65
66
# File 'lib/apipie/params/descriptor.rb', line 63

def self.build(*args)
  # this is an abstract class
  nil
end

.inherited(subclass) ⇒ Object



59
60
61
# File 'lib/apipie/params/descriptor.rb', line 59

def self.inherited(subclass)
  Base.inherited(subclass)
end

Instance Method Details

#json_schemaObject



68
69
70
# File 'lib/apipie/params/descriptor.rb', line 68

def json_schema
  {'description' => description}
end

#validate!(param_description, value) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/apipie/params/descriptor.rb', line 73

def validate!(param_description, value)
  encapsulated_value = {'root' => value}
  encapsulated_schema = {
    'type' => 'object',
    'properties' => {'root' => json_schema}
  }
  errors = JSON::Validator.fully_validate(encapsulated_schema,
                                          encapsulated_value.with_indifferent_access,
                                          :errors_as_objects => true)

  if errors.any?
    raise invalid_param_error(param_description, value, errors)
  else
    return true
  end
end