ruby-livr 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/livr/rules/string.rb +51 -1
- data/ruby-livr.gemspec +1 -1
- data/spec/07-like_anchors_spec.rb +50 -0
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3c5ade5f85882fc10e6244fc81e0084772e6968af145a1a4ef3d6b88a98c40ab
|
|
4
|
+
data.tar.gz: 7e24a495ee0b7ce646c1572f90709691700bba758e86867ad148bcff4e6320b7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0d35faa9d6fc2e75a23c5b65ea845f0a27d4a322e5c33f03152eb040f2e388ed5652fbc9aea2e22fced7bb5d3abc13dc9d96478350b771eb476cb4779f963b33
|
|
7
|
+
data.tar.gz: 927a24be0e409ed99ed851eeb154172d54177103b0e19a7077fda3cc8792a20cbb9b041c757fdb5b26e9b00ccb9b59f26f0f6753bd070e603910be1aae79473e
|
data/lib/livr/rules/string.rb
CHANGED
|
@@ -133,7 +133,7 @@ module LIVR
|
|
|
133
133
|
return 'FORMAT_ERROR' unless is_primitive(value)
|
|
134
134
|
|
|
135
135
|
value = value.to_s
|
|
136
|
-
regexp = Regexp.compile(@re_str, @flags)
|
|
136
|
+
regexp = Regexp.compile(whole_value_anchors(@re_str), @flags)
|
|
137
137
|
|
|
138
138
|
unless regexp.match(value)
|
|
139
139
|
return 'WRONG_FORMAT'
|
|
@@ -143,6 +143,56 @@ module LIVR
|
|
|
143
143
|
|
|
144
144
|
return
|
|
145
145
|
end
|
|
146
|
+
|
|
147
|
+
private
|
|
148
|
+
|
|
149
|
+
# LIVR patterns follow JavaScript, where ^ and $ anchor the whole value. In Ruby
|
|
150
|
+
# they anchor any line, so "12345\n<script>" would match "^[0-9]{5}$".
|
|
151
|
+
# Replaces ^ and $ outside character classes with \A and \z.
|
|
152
|
+
def whole_value_anchors(re_str)
|
|
153
|
+
result = +""
|
|
154
|
+
class_depth = 0
|
|
155
|
+
chars = re_str.to_s.chars
|
|
156
|
+
i = 0
|
|
157
|
+
|
|
158
|
+
while i < chars.size
|
|
159
|
+
char = chars[i]
|
|
160
|
+
|
|
161
|
+
if char == "\\"
|
|
162
|
+
result << char << chars[i + 1].to_s
|
|
163
|
+
i += 2
|
|
164
|
+
next
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
if class_depth > 0
|
|
168
|
+
class_depth += 1 if char == "["
|
|
169
|
+
class_depth -= 1 if char == "]"
|
|
170
|
+
result << char
|
|
171
|
+
elsif char == "["
|
|
172
|
+
class_depth = 1
|
|
173
|
+
result << char
|
|
174
|
+
# "]" right after "[" or "[^" is a literal, not the end of the class
|
|
175
|
+
if chars[i + 1] == "^"
|
|
176
|
+
result << chars[i + 1]
|
|
177
|
+
i += 1
|
|
178
|
+
end
|
|
179
|
+
if chars[i + 1] == "]"
|
|
180
|
+
result << chars[i + 1]
|
|
181
|
+
i += 1
|
|
182
|
+
end
|
|
183
|
+
elsif char == "^"
|
|
184
|
+
result << "\\A"
|
|
185
|
+
elsif char == "$"
|
|
186
|
+
result << "\\z"
|
|
187
|
+
else
|
|
188
|
+
result << char
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
i += 1
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
result
|
|
195
|
+
end
|
|
146
196
|
end
|
|
147
197
|
|
|
148
198
|
end
|
data/ruby-livr.gemspec
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Gem::Specification.new do |gem|
|
|
2
2
|
gem.name = "ruby-livr"
|
|
3
|
-
gem.version = "2.1.
|
|
3
|
+
gem.version = "2.1.1"
|
|
4
4
|
gem.summary = "Implementation of LIVR v2.0"
|
|
5
5
|
gem.description = "Language Independent Validation Rules. Implements 2.0 specification"
|
|
6
6
|
gem.authors = ["Vitaliy Yanchuk"]
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'spec_helper.rb'
|
|
2
|
+
|
|
3
|
+
RSpec.describe "like" do
|
|
4
|
+
|
|
5
|
+
it "^ and $ anchor the whole value, not a line" do
|
|
6
|
+
validator = LIVR::Validator.new(
|
|
7
|
+
"zip1" => { "like" => "^[0-9]{5}$" },
|
|
8
|
+
"zip2" => { "like" => "^[0-9]{5}$" },
|
|
9
|
+
"zip3" => { "like" => "^[0-9]{5}$" },
|
|
10
|
+
"zip4" => { "like" => ["^[0-9]{5}$", "i"] },
|
|
11
|
+
"zip5" => { "like" => "^[0-9]{5}$" }
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
output = validator.validate(
|
|
15
|
+
"zip1" => "12345\n<script>",
|
|
16
|
+
"zip2" => "<script>\n12345",
|
|
17
|
+
"zip3" => "12345\n",
|
|
18
|
+
"zip4" => "12345\n<script>",
|
|
19
|
+
"zip5" => "12345"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
expect(output).to eq(false)
|
|
23
|
+
expect(validator.get_errors).to eq(
|
|
24
|
+
"zip1" => "WRONG_FORMAT",
|
|
25
|
+
"zip2" => "WRONG_FORMAT",
|
|
26
|
+
"zip3" => "WRONG_FORMAT",
|
|
27
|
+
"zip4" => "WRONG_FORMAT"
|
|
28
|
+
)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "^ and $ in character classes and escaped ones stay literal" do
|
|
32
|
+
validator = LIVR::Validator.new(
|
|
33
|
+
"price" => { "like" => "^\\$[0-9]+$" },
|
|
34
|
+
"caret" => { "like" => "^[\\^$]+$" },
|
|
35
|
+
"negated" => { "like" => "^[^0-9]+$" },
|
|
36
|
+
"bracket" => { "like" => "^[]^$]+$" }
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
output = validator.validate("price" => "$10", "caret" => "^$^", "negated" => "abc", "bracket" => "]^$")
|
|
40
|
+
|
|
41
|
+
expect(validator.get_errors).to eq(nil)
|
|
42
|
+
expect(output).to eq("price" => "$10", "caret" => "^$^", "negated" => "abc", "bracket" => "]^$")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "patterns without anchors still match part of the value" do
|
|
46
|
+
validator = LIVR::Validator.new("name" => { "like" => "[0-9]+" })
|
|
47
|
+
|
|
48
|
+
expect(validator.validate("name" => "abc\n123")).to eq("name" => "abc\n123")
|
|
49
|
+
end
|
|
50
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ruby-livr
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.1.
|
|
4
|
+
version: 2.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Vitaliy Yanchuk
|
|
@@ -71,6 +71,7 @@ files:
|
|
|
71
71
|
- spec/04-custom_filters_spec.rb
|
|
72
72
|
- spec/05-rules_replacement_spec.rb
|
|
73
73
|
- spec/06-primitive_values_spec.rb
|
|
74
|
+
- spec/07-like_anchors_spec.rb
|
|
74
75
|
- spec/spec_helper.rb
|
|
75
76
|
- spec/test_suite/aliases_negative/01-adult_age/aliases.json
|
|
76
77
|
- spec/test_suite/aliases_negative/01-adult_age/errors.json
|
|
@@ -316,6 +317,7 @@ test_files:
|
|
|
316
317
|
- spec/04-custom_filters_spec.rb
|
|
317
318
|
- spec/05-rules_replacement_spec.rb
|
|
318
319
|
- spec/06-primitive_values_spec.rb
|
|
320
|
+
- spec/07-like_anchors_spec.rb
|
|
319
321
|
- spec/spec_helper.rb
|
|
320
322
|
- spec/test_suite/aliases_negative/01-adult_age/aliases.json
|
|
321
323
|
- spec/test_suite/aliases_negative/01-adult_age/errors.json
|