Class: JSON::Schema::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/json-schema/validator.rb

Direct Known Subclasses

Draft3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidator

Returns a new instance of Validator.



37
38
39
40
# File 'lib/json-schema/validator.rb', line 37

def initialize()
  @attributes = {}
  @uri = nil
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



35
36
37
# File 'lib/json-schema/validator.rb', line 35

def attributes
  @attributes
end

#uriObject

Returns the value of attribute uri.



35
36
37
# File 'lib/json-schema/validator.rb', line 35

def uri
  @uri
end

Instance Method Details

#extend_schema_definition(schema_uri) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/json-schema/validator.rb', line 42

def extend_schema_definition(schema_uri)
  u = URI.parse(schema_uri)
  validator = JSON::Validator.validators["#{u.scheme}://#{u.host}#{u.path}"]
  if validator.nil?
    raise SchemaError.new("Schema not found: #{u.scheme}://#{u.host}#{u.path}")
  end
  @attributes.merge!(validator.attributes)
end

#to_sObject



51
52
53
# File 'lib/json-schema/validator.rb', line 51

def to_s
  "#{@uri.scheme}://#{uri.host}#{uri.path}"
end

#validate(current_schema, data, fragments) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/json-schema/validator.rb', line 55

def validate(current_schema, data, fragments)
  current_schema.schema.each do |attr_name,attribute|
    if @attributes.has_key?(attr_name)
      @attributes[attr_name].validate(current_schema, data, fragments, self)
    end
  end
  data
end