Skip to content

Commit

Permalink
Merge pull request #497 from mohanapriya2308/master
Browse files Browse the repository at this point in the history
Integer as string should be defined as string
  • Loading branch information
bastelfreak committed Jul 24, 2023
2 parents 5dbb4df + c9baeef commit 9334461
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ JSON::Validator.validate(schema, { "a" => 1 }, :parse_data => false)
# => false
JSON::Validator.validate(schema, '{ "a": 1 }', :parse_data => false)

#
# with the `:parse_integer` option set to false, the integer value given as string will not be parsed.
#

# => true
JSON::Validator.validate({type: "integer"}, "23")
# => false
JSON::Validator.validate({type: "integer"}, "23", parse_integer: false)
# => true
JSON::Validator.validate({type: "string"}, "123", parse_integer: false)
# => false
JSON::Validator.validate({type: "string"}, "123")

#
# with the `:json` option, the json must be an unparsed json text (not a hash, a uri or a file path)
#
Expand Down
5 changes: 4 additions & 1 deletion lib/json-schema/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Validator
allPropertiesRequired: false,
noAdditionalProperties: false,
parse_data: true,
parse_integer: true,
}
@@validators = {}
@@default_validator = nil
Expand Down Expand Up @@ -577,7 +578,9 @@ def initialize_data(data)
data = self.class.parse(custom_open(json_uri))
elsif data.is_a?(String)
begin
data = self.class.parse(data)
# Check if the string is valid integer
strict_convert = data.match?(/\A[+-]?\d+\z/) && !@options[:parse_integer]
data = strict_convert ? data : self.class.parse(data)
rescue JSON::Schema::JsonParseError
begin
json_uri = Util::URI.normalized_uri(data)
Expand Down
6 changes: 6 additions & 0 deletions test/initialize_data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def test_parse_integer_string

refute(JSON::Validator.validate(schema, data, parse_data: false))

refute(JSON::Validator.validate(schema, data, parse_integer: false))

assert(JSON::Validator.validate(schema, data, json: true))

assert_raises(JSON::Schema::JsonLoadError) { JSON::Validator.validate(schema, data, uri: true) }
Expand Down Expand Up @@ -184,6 +186,10 @@ def test_parse_integer_string_with_instantiated_validator
assert_raises(JSON::Schema::ValidationError) { v.validate(data) }
assert_raises(JSON::Schema::ValidationError) { v.validate(data) }

v = JSON::Validator.new(schema, { parse_integer: false })
assert_raises(JSON::Schema::ValidationError) { v.validate(data) }
assert_raises(JSON::Schema::ValidationError) { v.validate(data) }

v = JSON::Validator.new(schema, { json: true })
assert(v.validate(data))
assert(v.validate(data))
Expand Down

0 comments on commit 9334461

Please sign in to comment.