Module: JsonValidation

Extended by:
JsonValidation
Included in:
JsonValidation
Defined in:
lib/json_validation.rb,
lib/json_validation/version.rb,
lib/json_validation/validators/not.rb,
lib/json_validation/validators/ref.rb,
lib/json_validation/validators/enum.rb,
lib/json_validation/validators/type.rb,
lib/json_validation/validators/items.rb,
lib/json_validation/validators/all_of.rb,
lib/json_validation/validators/any_of.rb,
lib/json_validation/validators/format.rb,
lib/json_validation/validators/one_of.rb,
lib/json_validation/validators/default.rb,
lib/json_validation/validators/maximum.rb,
lib/json_validation/validators/minimum.rb,
lib/json_validation/validators/pattern.rb,
lib/json_validation/validators/required.rb,
lib/json_validation/validator_collection.rb,
lib/json_validation/validators/max_items.rb,
lib/json_validation/validators/min_items.rb,
lib/json_validation/validators/validator.rb,
lib/json_validation/validators/max_length.rb,
lib/json_validation/validators/min_length.rb,
lib/json_validation/validators/properties.rb,
lib/json_validation/validators/multiple_of.rb,
lib/json_validation/validators/dependencies.rb,
lib/json_validation/validators/unique_items.rb,
lib/json_validation/validators/max_properties.rb,
lib/json_validation/validators/min_properties.rb,
lib/json_validation/validators/additional_items.rb,
lib/json_validation/validators/exclusive_maximum.rb,
lib/json_validation/validators/exclusive_minimum.rb,
lib/json_validation/validators/pattern_properties.rb,
lib/json_validation/validators/additional_properties.rb

Defined Under Namespace

Modules: Validators Classes: ValidatorCollection

Constant Summary collapse

TYPES_TO_CLASSES =
{
  :array => [Array],
  :boolean => [TrueClass, FalseClass],
  :integer => [Integer],
  :number => [Numeric],
  :null => [NilClass],
  :object => [Hash],
  :string => [String],
  :any => [Object],
}
VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#add_format_validator(key, format_validator) ⇒ Object



120
121
122
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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_validatorsObject



124
125
126
# File 'lib/json_validation.rb', line 124

def clear_format_validators
  @format_validators = nil
end

#clear_schema_cacheObject



106
107
108
# File 'lib/json_validation.rb', line 106

def clear_schema_cache
  @schema_cache = nil
end

#format_validatorsObject



128
129
130
# File 'lib/json_validation.rb', line 128

def format_validators
  @format_validators ||= {}
end

#generate_uri(schema) ⇒ Object



64
65
66
# 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) ⇒ Object



116
117
118
# File 'lib/json_validation.rb', line 116

def get_format_validator(key)
  format_validators.fetch(key).new
end

#load_schema(uri) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object



28
29
30
31
32
33
# 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_cacheObject



110
111
112
113
114
# File 'lib/json_validation.rb', line 110

def schema_cache
  @schema_cache ||= Hash.new {|cache, uri|
    cache[uri] = JSON.parse(open(uri).read)
  }
end