Skip to content

Commit 2d7237d

Browse files
committed
Ensure dashes and underscores are permitted in key names
The validation added #36 is too strict, it doesn't allow underscores, hyphens, and spaces. These are supposed to be permissible as long as they don't come at the beginning or end. This makes the following changes to the ReGex: - Add " ", "_", and "-" to the allowed middle of word characters - Change "\u10FFFFA" to "\u10FF". This caused a warning from Ruby because it doesn't understand utf16 codes and the extra F and A characters were interpreted as literals. Fixes #37
1 parent 19bcdc4 commit 2d7237d

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

lib/jsonapi/include_directive.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def valid?(key)
8181

8282
def valid_json_key_name_regex
8383
# https://jsonapi.org/format/#document-member-names
84-
/^(?![\s\-_])[\u0080-\u10FFFFA-Za-z0-9*]+(?<![\s\-_])$/
84+
/^(?![\s\-_])[\u0080-\u10FFA-Za-z0-9* _-]+(?<![\s\-_])$/
8585
end
8686
end
8787
end

spec/include_directive_spec.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@
3131
end
3232
end
3333

34-
context 'not raises InvalidKey' do
34+
context 'does not raise InvalidKey' do
3535
["\u0080", "B", "t", "5", "\u0100", "\u10FFFAA"].each do |char|
36-
it "when provided characher '#{char}'" do
36+
it "when provided character '#{char}'" do
3737
expect(JSONAPI::IncludeDirective.new(char).key?(char)).to be true
3838
end
3939
end
40+
41+
[' ', '_', '-'].each do |char|
42+
it "when '#{char}' is not at beginning or end" do
43+
key = "valid#{char}valid"
44+
expect(JSONAPI::IncludeDirective.new(key).key?(key)).to be true
45+
end
46+
end
4047
end
4148
end
4249

0 commit comments

Comments
 (0)