module JsonValidation

Constants

TYPES_TO_CLASSES
VERSION

Public Instance Methods

add_format_validator(key, format_validator) click to toggle source
# File lib/json_validation.rb, line 120
def add_format_validator(key, format_validator)
  format_validators[key] = format_validator
end
build_validator(schema, base_uri = nil) click to toggle source
# File lib/json_validation.rb, line 35
def build_validator(schema, base_uri = nil)
  if base_uri.nil?
    base_uri = generate_uri(schema)
    schema_cache[base_uri] = schema
  end

  if schema["id"]
    base_uri = base_uri.join(Addressable::URI.parse(schema["id"]))
  end

  validators = schema.keys.map {|key|
    if key == '$ref'
      validator_name = 'Ref'
    else
      validator_name = key[0].upcase + key[1..-1]
    end

    begin
      klass = JsonValidation::Validators.const_get(:"#{validator_name}")
    rescue NameError
      nil
    else
      klass.new(schema, base_uri)
    end
  }.compact

  ValidatorCollection.new(validators)
end
clear_format_validators() click to toggle source
# File lib/json_validation.rb, line 124
def clear_format_validators
  @format_validators = nil
end
clear_schema_cache() click to toggle source
# File lib/json_validation.rb, line 106
def clear_schema_cache
  @schema_cache = nil
end
format_validators() click to toggle source
# File lib/json_validation.rb, line 128
def format_validators
  @format_validators ||= {}
end
generate_uri(schema) click to toggle source
# File lib/json_validation.rb, line 64
def generate_uri(schema)
  Addressable::URI.parse(Digest::SHA1.hexdigest(schema.to_json))
end
get_format_validator(key) click to toggle source
# File lib/json_validation.rb, line 116
def get_format_validator(key)
  format_validators.fetch(key).new
end
load_schema(uri) click to toggle source
# File lib/json_validation.rb, line 68
def load_schema(uri)
  raise unless uri.is_a?(Addressable::URI)

  uri = uri.clone
  uri_fragment = uri.fragment
  uri.fragment = nil
  schema = schema_cache[uri]

  return schema if uri_fragment == "" || uri_fragment.nil?

  fragment = schema

  uri_fragment[1..-1].split('/').each do |element|
    element = element.gsub('~0', '~').gsub('~1', '/').gsub('%25', '%')

    case fragment
    when Hash
      if fragment.has_key?(element)
        fragment = fragment[element]
        next
      end
    when Array
      begin
        ix = Integer(element)
        if ix < fragment.size
          fragment = fragment[ix]
          next
        end
      rescue ArgumentError
      end
    else
      raise "Could not look up #{uri_fragment} in #{schema}"
    end
  end

  fragment
end
load_validator(uri) click to toggle source
# File lib/json_validation.rb, line 28
def load_validator(uri)
  uri = Addressable::URI.parse(uri) unless uri.is_a?(Addressable::URI)

  schema = load_schema(uri)
  build_validator(schema, uri)
end
schema_cache() click to toggle source
# File lib/json_validation.rb, line 110
def schema_cache
  @schema_cache ||= Hash.new {|cache, uri|
    cache[uri] = JSON.parse(open(uri).read)
  }
end