lib/jsonschema.rb in jsonschema-2.0.0 vs lib/jsonschema.rb in jsonschema-2.0.1

- old
+ new

@@ -1,10 +1,10 @@ # vim: fileencoding=utf-8 module JSON class Schema - VERSION = '2.0.0' + VERSION = '2.0.1' class ValueError < Exception;end class Undefined;end TypesMap = { "string" => String, "integer" => [Integer, Fixnum], @@ -35,11 +35,11 @@ check_property(value, schema['extends'], key, parent) end if value == Undefined unless schema['optional'] - raise ValueError, "#{key} is missing and it is not optional" + raise ValueError, "#{key}: is missing and it is not optional" end # default if @interactive && !parent.include?(key) && !schema['default'].nil? unless schema["readonly"] @@ -59,11 +59,11 @@ begin check_type(value, schema['disallow'], key, parent) rescue ValueError flag = false end - raise ValueError, "disallowed value was matched" if flag + raise ValueError, "#{key}: disallowed value was matched" if flag end unless value.nil? if value.instance_of? Array if schema['items'] @@ -73,11 +73,11 @@ } if schema.include?('additionalProperties') additional = schema['additionalProperties'] if additional.instance_of?(FalseClass) if schema['items'].size < value.size - raise ValueError, "There are more values in the array than are allowed by the items and additionalProperties restrictions." + raise ValueError, "#{key}: There are more values in the array than are allowed by the items and additionalProperties restrictions." end else value.each_with_index {|val, index| check_property(undefined_check(value, index), schema['additionalProperties'], index, value) } @@ -88,107 +88,107 @@ check_property(undefined_check(value, index), schema['items'], index, value) } end end if schema['minItems'] && value.size < schema['minItems'] - raise ValueError, "There must be a minimum of #{schema['minItems']} in the array" + raise ValueError, "#{key}: There must be a minimum of #{schema['minItems']} in the array" end if schema['maxItems'] && value.size > schema['maxItems'] - raise ValueError, "There must be a maximum of #{schema['maxItems']} in the array" + raise ValueError, "#{key}: There must be a maximum of #{schema['maxItems']} in the array" end elsif schema['properties'] check_object(value, schema['properties'], schema['additionalProperties']) elsif schema.include?('additionalProperties') additional = schema['additionalProperties'] unless additional.instance_of?(TrueClass) if additional.instance_of?(Hash) || additional.instance_of?(FalseClass) properties = {} value.each {|k, val| if additional.instance_of?(FalseClass) - raise ValueError, "Additional properties not defined by 'properties' are not allowed in field '#{k}'" + raise ValueError, "#{key}: Additional properties not defined by 'properties' are not allowed in field '#{k}'" else check_property(val, schema['additionalProperties'], k, value) end } else - raise ValueError, "additionalProperties schema definition for field '#{}' is not an object" + raise ValueError, "#{key}: additionalProperties schema definition for field '#{}' is not an object" end end end if value.instance_of?(String) # pattern if schema['pattern'] && !(value =~ Regexp.new(schema['pattern'])) - raise ValueError, "does not match the regex pattern #{schema['pattern']}" + raise ValueError, "#{key}: does not match the regex pattern #{schema['pattern']}" end strlen = value.split(//).size # maxLength if schema['maxLength'] && strlen > schema['maxLength'] - raise ValueError, "may only be #{schema['maxLength']} characters long" + raise ValueError, "#{key}: may only be #{schema['maxLength']} characters long" end # minLength if schema['minLength'] && strlen < schema['minLength'] - raise ValueError, "must be at least #{schema['minLength']} characters long" + raise ValueError, "#{key}: must be at least #{schema['minLength']} characters long" end end if value.kind_of?(Numeric) # minimum + minimumCanEqual if schema['minimum'] minimumCanEqual = schema.fetch('minimumCanEqual', Undefined) if minimumCanEqual == Undefined || minimumCanEqual if value < schema['minimum'] - raise ValueError, "must have a minimum value of #{schema['minimum']}" + raise ValueError, "#{key}: must have a minimum value of #{schema['minimum']}" end else if value <= schema['minimum'] - raise ValueError, "must have a minimum value of #{schema['minimum']}" + raise ValueError, "#{key}: must have a minimum value of #{schema['minimum']}" end end end # maximum + maximumCanEqual if schema['maximum'] maximumCanEqual = schema.fetch('maximumCanEqual', Undefined) if maximumCanEqual == Undefined || maximumCanEqual if value > schema['maximum'] - raise ValueError, "must have a maximum value of #{schema['maximum']}" + raise ValueError, "#{key}: must have a maximum value of #{schema['maximum']}" end else if value >= schema['maximum'] - raise ValueError, "must have a maximum value of #{schema['maximum']}" + raise ValueError, "#{key}: must have a maximum value of #{schema['maximum']}" end end end # maxDecimal if schema['maxDecimal'] && schema['maxDecimal'].kind_of?(Numeric) if value.to_s =~ /\.\d{#{schema['maxDecimal']+1},}/ - raise ValueError, "may only have #{schema['maxDecimal']} digits of decimal places" + raise ValueError, "#{key}: may only have #{schema['maxDecimal']} digits of decimal places" end end end # enum if schema['enum'] unless(schema['enum'].detect{|enum| enum == value }) - raise ValueError, "does not have a value in the enumeration #{schema['enum'].join(", ")}" + raise ValueError, "#{key}: does not have a value in the enumeration #{schema['enum'].join(", ")}" end end # description if schema['description'] && !schema['description'].instance_of?(String) - raise ValueError, "The description for field '#{value}' must be a string" + raise ValueError, "#{key}: The description for field '#{value}' must be a string" end # title if schema['title'] && !schema['title'].instance_of?(String) - raise ValueError, "The title for field '#{value}' must be a string" + raise ValueError, "#{key}: The title for field '#{value}' must be a string" end # format if schema['format'] end @@ -240,16 +240,16 @@ rescue ValueError next end end unless datavalid - raise ValueError, "#{value.class} value found, but a #{type} is required" + raise ValueError, "#{key}: #{value.class} value found, but a #{type} is required" end elsif converted_fieldtype.instance_of? Hash check_property(value, type, key, parent) else unless value.instance_of? converted_fieldtype - raise ValueError, "#{value.class} value found, but a #{type} is required" + raise ValueError, "#{key}: #{value.class} value found, but a #{type} is required" end end end end