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

Escape JSON pointer tokens #122

Merged
merged 1 commit into from
Dec 8, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/json_schemer/schema/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def merge(
end
end

JSON_POINTER_TOKEN_ESCAPE_CHARS = { '~' => '~0', '/' => '~1' }
JSON_POINTER_TOKEN_ESCAPE_REGEXP = Regexp.union(JSON_POINTER_TOKEN_ESCAPE_CHARS.keys)

def initialize(
schema,
format: true,
Expand Down Expand Up @@ -531,7 +534,8 @@ def validate_object(instance, &block)
dependencies.each do |key, value|
next unless data.key?(key)
subschema = value.is_a?(Array) ? { 'required' => value } : value
subinstance = instance.merge(schema: subschema, schema_pointer: "#{instance.schema_pointer}/dependencies/#{key}")
escaped_key = escape_json_pointer_token(key)
subinstance = instance.merge(schema: subschema, schema_pointer: "#{instance.schema_pointer}/dependencies/#{escaped_key}")
validate_instance(subinstance, &block)
end
end
Expand All @@ -545,6 +549,8 @@ def validate_object(instance, &block)

regex_pattern_properties = nil
data.each do |key, value|
escaped_key = escape_json_pointer_token(key)

unless property_names.nil?
subinstance = instance.merge(
data: key,
Expand All @@ -559,9 +565,9 @@ def validate_object(instance, &block)
if properties && properties.key?(key)
subinstance = instance.merge(
data: value,
data_pointer: "#{instance.data_pointer}/#{key}",
data_pointer: "#{instance.data_pointer}/#{escaped_key}",
schema: properties[key],
schema_pointer: "#{instance.schema_pointer}/properties/#{key}"
schema_pointer: "#{instance.schema_pointer}/properties/#{escaped_key}"
)
validate_instance(subinstance, &block)
matched_key = true
Expand All @@ -572,12 +578,13 @@ def validate_object(instance, &block)
[pattern, resolve_regexp(pattern), property_schema]
end
regex_pattern_properties.each do |pattern, regex, property_schema|
escaped_pattern = escape_json_pointer_token(pattern)
if regex.match?(key)
subinstance = instance.merge(
data: value,
data_pointer: "#{instance.data_pointer}/#{key}",
data_pointer: "#{instance.data_pointer}/#{escaped_key}",
schema: property_schema,
schema_pointer: "#{instance.schema_pointer}/patternProperties/#{pattern}"
schema_pointer: "#{instance.schema_pointer}/patternProperties/#{escaped_pattern}"
)
validate_instance(subinstance, &block)
matched_key = true
Expand All @@ -590,7 +597,7 @@ def validate_object(instance, &block)
unless additional_properties.nil?
subinstance = instance.merge(
data: value,
data_pointer: "#{instance.data_pointer}/#{key}",
data_pointer: "#{instance.data_pointer}/#{escaped_key}",
schema: additional_properties,
schema_pointer: "#{instance.schema_pointer}/additionalProperties"
)
Expand All @@ -614,6 +621,10 @@ def safe_strict_decode64(data)
nil
end

def escape_json_pointer_token(token)
token.gsub(JSON_POINTER_TOKEN_ESCAPE_REGEXP, JSON_POINTER_TOKEN_ESCAPE_CHARS)
end

def join_uri(a, b)
b = URI.parse(b) if b
if a && b && a.relative? && b.relative?
Expand Down
17 changes: 17 additions & 0 deletions test/json_schemer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1156,4 +1156,21 @@ def test_it_handles_multiple_of_floats
refute(JSONSchemer.schema({ 'multipleOf' => 0.01 }).valid?(8.666))
assert(JSONSchemer.schema({ 'multipleOf' => 0.001 }).valid?(8.666))
end

def test_it_escapes_json_pointer_tokens
schemer = JSONSchemer.schema(
{
'type' => 'object',
'properties' => {
'foo/bar~' => {
'type' => 'string'
}
}
}
)
errors = schemer.validate({ 'foo/bar~' => 1 }).to_a
assert_equal(1, errors.size)
assert_equal('/foo~1bar~0', errors.first.fetch('data_pointer'))
assert_equal('/properties/foo~1bar~0', errors.first.fetch('schema_pointer'))
end
end