Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer as string should be defined as string #497

Merged
merged 6 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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