Class: JsonSchema::FormSchemas

Inherits:
Object
  • Object
show all
Defined in:
lib/json_schema/form_schemas.rb

Instance Method Summary collapse

Instance Method Details

#base_dirObject



8
9
10
# File 'lib/json_schema/form_schemas.rb', line 8

def base_dir
  # set in subclass
end

#get_schemasObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/json_schema/form_schemas.rb', line 23

def get_schemas
  return_val = {}

  Dir.glob(File.join(base_dir, '/*.json')).each do |schema|
    schema_name = File.basename(schema, '.json').upcase
    return_val[schema_name] = MultiJson.load(File.read(schema))
  end

  return_val
end

#schema(form) ⇒ Object

safe access (over schemas[]). throws error when trying to access non-existent schema



17
18
19
20
21
# File 'lib/json_schema/form_schemas.rb', line 17

def schema(form)
  return schemas[form] if schemas.key? form

  raise JsonSchema::MissingSchema.new(form, schemas.keys)
end

#schemasObject



12
13
14
# File 'lib/json_schema/form_schemas.rb', line 12

def schemas
  @schemas ||= get_schemas
end

#validate!(form, payload) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/json_schema/form_schemas.rb', line 34

def validate!(form, payload)
  schema_validator = JSONSchemer.schema(schema(form), insert_property_defaults: true)
  # there is currently a bug in the gem
  # that it runs the logic based validations
  # before inserting defaults
  # this is a work around
  schema_validator.validate(payload).count
  errors = schema_validator.validate(payload).to_a
  raise JsonSchema::JsonApiMissingAttribute, errors unless errors.empty?

  true
end