Skip to content

Commit

Permalink
Adding draft-02 tests and fixing the draft-02 maximum / minimum inclu…
Browse files Browse the repository at this point in the history
…sive stuff.
  • Loading branch information
hoxworth committed Mar 19, 2011
1 parent 1d2dd6f commit 2571c3f
Show file tree
Hide file tree
Showing 5 changed files with 839 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/json-schema/attributes/maximum_inclusive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class Schema
class MaximumInclusiveAttribute < Attribute
def self.validate(current_schema, data, fragments, validator, options = {})
if data.is_a?(Numeric)
if (current_schema.schema['maximumCanEqual'] ? data >= current_schema.schema['maximum'] : data > current_schema.schema['maximum'])
current_schema.schema['maximumCanEqual'] = true if current_schema.schema['maximumCanEqual'].nil?
if (current_schema.schema['maximumCanEqual'] ? data > current_schema.schema['maximum'] : data >= current_schema.schema['maximum'])
message = "The property '#{build_fragment(fragments)}' did not have a maximum value of #{current_schema.schema['maximum']}, "
message += current_schema.schema['exclusiveMaximum'] ? 'exclusively' : 'inclusively'
raise ValidationError.new(message, fragments, current_schema)
Expand Down
3 changes: 2 additions & 1 deletion lib/json-schema/attributes/minimum_inclusive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class Schema
class MinimumInclusiveAttribute < Attribute
def self.validate(current_schema, data, fragments, validator, options = {})
if data.is_a?(Numeric)
if (current_schema.schema['minimumCanEqual'] ? data <= current_schema.schema['minimum'] : data < current_schema.schema['minimum'])
current_schema.schema['minimumCanEqual'] = true if current_schema.schema['minimumCanEqual'].nil?
if (current_schema.schema['minimumCanEqual'] ? data < current_schema.schema['minimum'] : data <= current_schema.schema['minimum'])
message = "The property '#{build_fragment(fragments)}' did not have a minimum value of #{current_schema.schema['minimum']}, "
message += current_schema.schema['exclusiveMinimum'] ? 'exclusively' : 'inclusively'
raise ValidationError.new(message, fragments, current_schema)
Expand Down
2 changes: 1 addition & 1 deletion lib/json-schema/attributes/properties_optional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class PropertiesOptionalAttribute < Attribute
def self.validate(current_schema, data, fragments, validator, options = {})
if data.is_a?(Hash)
current_schema.schema['properties'].each do |property,property_schema|
if (!property_schema['optional'] && !data.has_key?(property))
if ((property_schema['optional'].nil? || property_schema['optional'] == false) && !data.has_key?(property))
message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
raise ValidationError.new(message, fragments, current_schema)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class Validator

def initialize(schema_data, data, opts={})
@options = @@default_opts.clone.merge(opts)

# I'm not a fan of this, but it's quick and dirty to get it working for now
if @options[:version]
@options[:version] = case @options[:version].to_s
when "draft3"
Expand Down
Loading

0 comments on commit 2571c3f

Please sign in to comment.