Class: Marty::Api::Base::SchemaValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/marty/api/base.rb

Class Method Summary collapse

Class Method Details

._get_errors(errs) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/marty/api/base.rb', line 235

def self._get_errors(errs)
  if errs.is_a?(Array)
    errs.map { |err| _get_errors(err) }
  elsif errs.is_a?(Hash)
    if !errs.include?(:failed_attribute)
      errs.map { |_k, v| _get_errors(v) }
    else
      fa, fragment, message, errors = errs.values_at(:failed_attribute,
                                                     :fragment,
                                                     :message, :errors)
      (['AllOf', 'AnyOf', 'Not'].include?(fa) && fragment == '#/' ?
         [] : [massage_message(message)]) + _get_errors(errors || {})
    end
  end
end

.get_errors(errs) ⇒ Object



251
252
253
# File 'lib/marty/api/base.rb', line 251

def self.get_errors(errs)
  _get_errors(errs).flatten
end

.get_schema(params) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'lib/marty/api/base.rb', line 205

def self.get_schema params
    Marty::ScriptSet.new(params[:tag]).get_engine(params[:script] + 'Schemas').
      evaluate(params[:node], params[:attr], {})
rescue StandardError => e
    msg = e.message == 'No such script' ? 'Schema not defined' :
            'Problem with schema: ' + e.message

    raise "Schema error for #{params[:script]}/#{params[:node]} "\
          "attrs=#{params[:attr]}: #{msg}"
end

.massage_message(msg) ⇒ Object



226
227
228
229
230
231
232
233
# File 'lib/marty/api/base.rb', line 226

def self.massage_message(msg)
  m = %r{'#/([^']+)' of type ([^ ]+) matched the disallowed schema}.
        match(msg)

  return msg unless m

  "disallowed parameter '#{m[1]}' of type #{m[2]} was received"
end

.validate_schema(schema, hash) ⇒ Object



216
217
218
219
220
221
222
223
224
# File 'lib/marty/api/base.rb', line 216

def self.validate_schema schema, hash
  JSON::Validator.fully_validate(
    schema.merge("\$schema" => Marty::JsonSchema::RAW_URI),
    hash,
    validate_schema:   true,
    errors_as_objects: true,
    version:           Marty::JsonSchema::RAW_URI,
  )
end