Sha256: af4bcb764db029445fdb0705f8f0996751807238f14ee1b076f475efdde01aca

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

require "json_schema_rails/version"
require "json_schema_rails/errors"
require "json_schema_rails/loaders"
require "json_schema_rails/schema_validator"
require "json_schema_rails/railtie" if defined?(Rails)

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

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

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

  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

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

  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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
json_schema_rails-0.0.3 lib/json_schema_rails.rb
json_schema_rails-0.0.2 lib/json_schema_rails.rb
json_schema_rails-0.0.1 lib/json_schema_rails.rb