Module: JsonSchemaRails

Defined in:
lib/json_schema_rails.rb,
lib/json_schema_rails/engine.rb,
lib/json_schema_rails/errors.rb,
lib/json_schema_rails/helpers.rb,
lib/json_schema_rails/loaders.rb,
lib/json_schema_rails/version.rb,
lib/json_schema_rails/loaders/base.rb,
lib/json_schema_rails/schema_validator.rb,
lib/json_schema_rails/loaders/directory.rb,
lib/json_schema_rails/loaders/hyper_schema.rb,
app/controllers/json_schema_rails/schemas_controller.rb,
app/controllers/json_schema_rails/application_controller.rb

Defined Under Namespace

Modules: Helpers, Loaders Classes: ApplicationController, Engine, Error, SchemaNotFound, SchemaParseError, SchemaValidator, SchemasController, ValidationError

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.lookup_schema(schema_name) ⇒ Object



32
33
34
35
36
# File 'lib/json_schema_rails.rb', line 32

def self.lookup_schema(schema_name)
  lookup_schema!(schema_name)
rescue SchemaNotFound
  nil
end

.lookup_schema!(schema_name) ⇒ Object

Raises:



38
39
40
41
42
43
44
# File 'lib/json_schema_rails.rb', line 38

def self.lookup_schema!(schema_name)
  Array(schema_name).each do |name|
    schema = schema_loader.load_schema(name)
    return schema if schema
  end
  raise SchemaNotFound
end

.schema_loaderObject



10
11
12
# File 'lib/json_schema_rails.rb', line 10

def self.schema_loader
  @schema_loader ||= Loaders::Directory.new('app/schemas')
end

.schema_loader=(loader) ⇒ Object



14
15
16
# File 'lib/json_schema_rails.rb', line 14

def self.schema_loader=(loader)
  @schema_loader = loader
end

.validate(schema_name, data) ⇒ Object



18
19
20
21
# File 'lib/json_schema_rails.rb', line 18

def self.validate(schema_name, data)
  schema = lookup_schema!(schema_name)
  schema.validate(data)
end

.validate!(schema_name, data) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/json_schema_rails.rb', line 23

def self.validate!(schema_name, data)
  schema = lookup_schema!(schema_name)
  valid, errors = schema.validate(data)
  unless valid
    raise ValidationError.from_errors(errors, schema, schema_name)
  end
  true
end