collection_json_serializer 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -1
- data/collection_json_serializer.gemspec +1 -0
- data/lib/collection_json_serializer.rb +4 -2
- data/lib/collection_json_serializer/validator.rb +3 -111
- data/lib/collection_json_serializer/validator/base.rb +69 -0
- data/lib/collection_json_serializer/validator/items_validator.rb +100 -0
- data/lib/collection_json_serializer/validator/{url.rb → types/url.rb} +2 -2
- data/lib/collection_json_serializer/validator/{value.rb → types/value.rb} +1 -1
- data/lib/collection_json_serializer/version.rb +1 -1
- data/test/builder/builder_test.rb +8 -0
- data/test/extensions/open_attrs_test.rb +2 -4
- data/test/fixtures/serializers/user_serializer.rb +6 -1
- data/test/minitest_helper.rb +41 -0
- data/test/objects/template_test.rb +2 -1
- data/test/serializer/template_test.rb +7 -1
- data/test/validator/invalid_test.rb +4 -11
- data/test/validator/items_validator_test.rb +56 -0
- data/test/validator/url_validator_test.rb +11 -6
- data/test/validator/validation_test.rb +21 -0
- data/test/validator/value_validator_test.rb +5 -22
- metadata +11 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a43d5f7a36d702aceb4fd3bdd71ff86da64e027c
|
4
|
+
data.tar.gz: 12b188b39d695b9e1edbef6cc7285b3990a7c572
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48434f77b5c372f61c7b4919c1389fc85e10e3e834a0a8f17ae1594d77af316c2b5d4af5e1c5e2ff4201db2d4a8e5e2215e34c3bcaa0bf695c395630c28e8ef9
|
7
|
+
data.tar.gz: ad03089d4119b6e799274cba090f4d5e6909553ad12e49347b48220607ca188ef8275521ca80caf0448345580faeafe2cf12813c07d45a917168833d30cba57e
|
data/README.md
CHANGED
@@ -30,7 +30,10 @@ As this gem user, you will be mainly writing/generating and mantaining serialize
|
|
30
30
|
class UserSerializer < CollectionJson::Serializer
|
31
31
|
href "http://example.com/users",
|
32
32
|
|
33
|
-
template :name
|
33
|
+
template :name
|
34
|
+
template email: { prompt: "My email" }
|
35
|
+
# This could be written in a single line, too, wrapping the hash:
|
36
|
+
# template :name, { email: { ... } }
|
34
37
|
|
35
38
|
# Please note that links can only be passed as hashes
|
36
39
|
links dashboard: { href: "http://example.com/my-dashboard" }
|
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.description = %q{CollectionJson::Serializer makes it easy to serialize objects into the Collection+JSON hypermedia type.}
|
13
13
|
spec.homepage = "https://github.com/carlesjove/collection_json_serializer"
|
14
14
|
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.0"
|
15
16
|
|
16
17
|
spec.files = `git ls-files -z`.split("\x0")
|
17
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
@@ -10,9 +10,11 @@ require "collection_json_serializer/items"
|
|
10
10
|
require "collection_json_serializer/spec"
|
11
11
|
require "collection_json_serializer/builder"
|
12
12
|
|
13
|
+
require "collection_json_serializer/validator/base"
|
13
14
|
require "collection_json_serializer/validator"
|
14
|
-
require "collection_json_serializer/validator/
|
15
|
-
require "collection_json_serializer/validator/
|
15
|
+
require "collection_json_serializer/validator/items_validator"
|
16
|
+
require "collection_json_serializer/validator/types/url"
|
17
|
+
require "collection_json_serializer/validator/types/value"
|
16
18
|
|
17
19
|
require "collection_json_serializer/objects/item"
|
18
20
|
require "collection_json_serializer/objects/template"
|
@@ -1,24 +1,8 @@
|
|
1
1
|
module CollectionJson
|
2
2
|
class Serializer
|
3
|
-
class Validator
|
3
|
+
class Validator < Validation
|
4
4
|
include CollectionJson::Serializer::Support
|
5
5
|
|
6
|
-
attr_accessor :errors
|
7
|
-
|
8
|
-
def initialize(serializer)
|
9
|
-
@serializer = serializer
|
10
|
-
@errors = {}
|
11
|
-
validate
|
12
|
-
end
|
13
|
-
|
14
|
-
def valid?
|
15
|
-
!invalid?
|
16
|
-
end
|
17
|
-
|
18
|
-
def invalid?
|
19
|
-
@errors.any?
|
20
|
-
end
|
21
|
-
|
22
6
|
private
|
23
7
|
|
24
8
|
def validate
|
@@ -32,57 +16,8 @@ module CollectionJson
|
|
32
16
|
end
|
33
17
|
|
34
18
|
def validate_items
|
35
|
-
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
def validate_items_attributes
|
40
|
-
@serializer.items.attributes.each do |attr|
|
41
|
-
params = attr.extract_params
|
42
|
-
validate_attributes_values(@serializer.resources, params)
|
43
|
-
validate_attributes_properties(params) if params[:properties]
|
44
|
-
end if @serializer.items? && @serializer.items.attributes?
|
45
|
-
end
|
46
|
-
|
47
|
-
def validate_attributes_values(resources, params)
|
48
|
-
resources.each do |resource|
|
49
|
-
val = extract_value_from(resource, params[:name])
|
50
|
-
if value_is_invalid?(val)
|
51
|
-
error_for :value, root: :attributes, path: [params[:name]]
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def validate_attributes_properties(params)
|
57
|
-
params[:properties].each do |key, value|
|
58
|
-
unless definition[:items][:data].keys.include?(key.to_sym)
|
59
|
-
error_for(
|
60
|
-
:unknown_attribute,
|
61
|
-
root: :attributes,
|
62
|
-
path: [params[:name], key]
|
63
|
-
)
|
64
|
-
next
|
65
|
-
end unless @serializer.uses?(:open_attrs)
|
66
|
-
|
67
|
-
if value_is_invalid?(value)
|
68
|
-
error_for :value, root: :attributes, path: [params[:name], key]
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def validate_items_links
|
74
|
-
@serializer.items.links.each do |attr|
|
75
|
-
params = attr.extract_params
|
76
|
-
params[:properties].keys.each do |key|
|
77
|
-
unless definition[:items][:links].keys.include?(key.to_sym)
|
78
|
-
error_for(
|
79
|
-
:unknown_attribute,
|
80
|
-
root: :items,
|
81
|
-
path: [:links, params[:name], key]
|
82
|
-
)
|
83
|
-
end unless @serializer.uses?(:open_attrs)
|
84
|
-
end
|
85
|
-
end
|
19
|
+
items_validation = ItemsValidator.new(@serializer)
|
20
|
+
@errors.merge!(items_validation.errors)
|
86
21
|
end
|
87
22
|
|
88
23
|
def validate_href
|
@@ -199,49 +134,6 @@ module CollectionJson
|
|
199
134
|
end
|
200
135
|
end if @serializer.queries.present?
|
201
136
|
end
|
202
|
-
|
203
|
-
def value_is_invalid?(value)
|
204
|
-
v = CollectionJson::Serializer::Validator::Value.new(value)
|
205
|
-
v.invalid?
|
206
|
-
end
|
207
|
-
|
208
|
-
def url_is_invalid?(value)
|
209
|
-
v = CollectionJson::Serializer::Validator::Url.new(value)
|
210
|
-
v.invalid?
|
211
|
-
end
|
212
|
-
|
213
|
-
def error_for(kind, root: root, path: [])
|
214
|
-
case kind.to_sym
|
215
|
-
when :url
|
216
|
-
ending = " is an invalid URL"
|
217
|
-
when :value
|
218
|
-
ending = " is an invalid value"
|
219
|
-
when :missing_attribute
|
220
|
-
ending = " is missing"
|
221
|
-
when :unknown_attribute
|
222
|
-
ending = " is an unknown attribute"
|
223
|
-
else
|
224
|
-
ending = " is an invalid value"
|
225
|
-
end
|
226
|
-
|
227
|
-
@errors[root] = [] unless @errors.key? root
|
228
|
-
e = "#{@serializer.class} #{root}"
|
229
|
-
e << ":" + path.join(":") if path.any?
|
230
|
-
e << ending
|
231
|
-
@errors[root] << e
|
232
|
-
end
|
233
|
-
|
234
|
-
def definition
|
235
|
-
CollectionJson::Spec::DEFINITION
|
236
|
-
end
|
237
|
-
|
238
|
-
def attributes?
|
239
|
-
@serializer.items? && @serializer.items.attributes?
|
240
|
-
end
|
241
|
-
|
242
|
-
def links?
|
243
|
-
@serializer.items? && @serializer.items.links?
|
244
|
-
end
|
245
137
|
end
|
246
138
|
end
|
247
139
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module CollectionJson
|
2
|
+
class Serializer
|
3
|
+
class Validation
|
4
|
+
attr_accessor :errors
|
5
|
+
|
6
|
+
def initialize(serializer)
|
7
|
+
@serializer = serializer
|
8
|
+
@errors = {}
|
9
|
+
validate
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
!invalid?
|
14
|
+
end
|
15
|
+
|
16
|
+
def invalid?
|
17
|
+
@errors.any?
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def validate
|
23
|
+
raise NotImplementedError, "This should be implemented by validators"
|
24
|
+
end
|
25
|
+
|
26
|
+
def definition
|
27
|
+
CollectionJson::Spec::DEFINITION
|
28
|
+
end
|
29
|
+
|
30
|
+
def value_is_invalid?(value)
|
31
|
+
v = Value.new(value)
|
32
|
+
v.invalid?
|
33
|
+
end
|
34
|
+
|
35
|
+
def url_is_invalid?(value)
|
36
|
+
v = Url.new(value)
|
37
|
+
v.invalid?
|
38
|
+
end
|
39
|
+
|
40
|
+
def href_or_error(object, root: root, path: path)
|
41
|
+
unless object.key?(:href)
|
42
|
+
error_for :missing_attribute, root: root, path: path
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def error_for(kind, root: root, path: [])
|
47
|
+
case kind.to_sym
|
48
|
+
when :url
|
49
|
+
ending = " is an invalid URL"
|
50
|
+
when :value
|
51
|
+
ending = " is an invalid value"
|
52
|
+
when :missing_attribute
|
53
|
+
ending = " is missing"
|
54
|
+
when :unknown_attribute
|
55
|
+
ending = " is an unknown attribute"
|
56
|
+
else
|
57
|
+
ending = " is an invalid value"
|
58
|
+
end
|
59
|
+
|
60
|
+
@errors[root] = [] unless @errors.key? root
|
61
|
+
e = "#{@serializer.class} #{root}"
|
62
|
+
e << ":" + path.join(":") if path.any?
|
63
|
+
e << ending
|
64
|
+
@errors[root] << e
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module CollectionJson
|
2
|
+
class Serializer
|
3
|
+
class Validator
|
4
|
+
class ItemsValidator < Validation
|
5
|
+
include CollectionJson::Serializer::Support
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def validate
|
10
|
+
validate_attributes if attributes?
|
11
|
+
validate_links if links?
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate_attributes
|
15
|
+
@serializer.items.attributes.each do |attr|
|
16
|
+
params = attr.extract_params
|
17
|
+
validate_attributes_values(@serializer.resources, params)
|
18
|
+
validate_attributes_properties(params) if params[:properties]
|
19
|
+
end if @serializer.items? && @serializer.items.attributes?
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate_links
|
23
|
+
@serializer.items.links.each do |attr|
|
24
|
+
link = attr.extract_params
|
25
|
+
|
26
|
+
href_or_error(
|
27
|
+
link[:properties],
|
28
|
+
root: :items,
|
29
|
+
path: [link[:name]]
|
30
|
+
)
|
31
|
+
|
32
|
+
link[:properties].each do |key, value|
|
33
|
+
unless definition[:items][:links].keys.include?(key.to_sym)
|
34
|
+
error_for(
|
35
|
+
:unknown_attribute,
|
36
|
+
root: :items,
|
37
|
+
path: [:links, link[:name], key]
|
38
|
+
)
|
39
|
+
end unless @serializer.uses?(:open_attrs)
|
40
|
+
|
41
|
+
case key
|
42
|
+
when :href
|
43
|
+
if url_is_invalid?(value)
|
44
|
+
error_for(
|
45
|
+
:url,
|
46
|
+
root: :items,
|
47
|
+
path: [:links, link[:name], key]
|
48
|
+
)
|
49
|
+
end
|
50
|
+
else
|
51
|
+
if value_is_invalid?(value)
|
52
|
+
error_for(
|
53
|
+
:value,
|
54
|
+
root: :items,
|
55
|
+
path: [:links, link[:name], key]
|
56
|
+
)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def validate_attributes_values(resources, params)
|
64
|
+
resources.each do |resource|
|
65
|
+
val = extract_value_from(resource, params[:name])
|
66
|
+
if value_is_invalid?(val)
|
67
|
+
error_for :value, root: :attributes, path: [params[:name]]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def validate_attributes_properties(params)
|
73
|
+
params[:properties].each do |key, value|
|
74
|
+
unless definition[:items][:data].keys.include?(key.to_sym)
|
75
|
+
error_for(
|
76
|
+
:unknown_attribute,
|
77
|
+
root: :attributes,
|
78
|
+
path: [params[:name], key]
|
79
|
+
)
|
80
|
+
next
|
81
|
+
end unless @serializer.uses?(:open_attrs)
|
82
|
+
|
83
|
+
if value_is_invalid?(value)
|
84
|
+
error_for :value, root: :attributes, path: [params[:name], key]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def attributes?
|
90
|
+
@serializer.items? && @serializer.items.attributes?
|
91
|
+
end
|
92
|
+
|
93
|
+
def links?
|
94
|
+
@serializer.items? && @serializer.items.links?
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
@@ -1,12 +1,12 @@
|
|
1
1
|
module CollectionJson
|
2
2
|
class Serializer
|
3
|
-
class
|
3
|
+
class Validation
|
4
4
|
class Url
|
5
5
|
# Stolen from https://github.com/eparreno/ruby_regex/blob/master/lib/ruby_regex.rb
|
6
6
|
VALID = /(\A\z)|(\A(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?\z)/ix
|
7
7
|
|
8
8
|
def initialize(value)
|
9
|
-
@uri = value
|
9
|
+
@uri = value.to_s
|
10
10
|
end
|
11
11
|
|
12
12
|
def valid?
|
@@ -57,6 +57,10 @@ module CollectionJson
|
|
57
57
|
name: "email",
|
58
58
|
value: "",
|
59
59
|
prompt: "My email"
|
60
|
+
},
|
61
|
+
{
|
62
|
+
name: "password",
|
63
|
+
value: ""
|
60
64
|
}
|
61
65
|
]
|
62
66
|
},
|
@@ -138,6 +142,10 @@ module CollectionJson
|
|
138
142
|
name: "email",
|
139
143
|
value: "",
|
140
144
|
prompt: "My email"
|
145
|
+
},
|
146
|
+
{
|
147
|
+
name: "password",
|
148
|
+
value: ""
|
141
149
|
}
|
142
150
|
]
|
143
151
|
},
|
@@ -23,9 +23,7 @@ module CollectionJson
|
|
23
23
|
]
|
24
24
|
@serializer.items.links = [
|
25
25
|
dashboard: {
|
26
|
-
|
27
|
-
# This URL shouldn't validate!
|
28
|
-
href: "/my-dashboard",
|
26
|
+
href: "http://example.com/my-dashboard",
|
29
27
|
anything: "at all"
|
30
28
|
}
|
31
29
|
]
|
@@ -69,7 +67,7 @@ module CollectionJson
|
|
69
67
|
links: [
|
70
68
|
{
|
71
69
|
rel: "dashboard",
|
72
|
-
href: "/my-dashboard",
|
70
|
+
href: "http://example.com/my-dashboard",
|
73
71
|
name: "dashboard",
|
74
72
|
anything: "at all",
|
75
73
|
}
|
@@ -1,7 +1,12 @@
|
|
1
1
|
class UserSerializer < CollectionJson::Serializer
|
2
2
|
href "http://example.com/users"
|
3
|
-
|
3
|
+
|
4
|
+
template :name
|
5
|
+
template email: { prompt: "My email" }
|
6
|
+
template :password
|
7
|
+
|
4
8
|
links dashboard: { href: "http://example.com/my-dashboard" }
|
9
|
+
|
5
10
|
queries search: {
|
6
11
|
href: "http://example.com/search",
|
7
12
|
name: false
|
data/test/minitest_helper.rb
CHANGED
@@ -22,4 +22,45 @@ module TestHelper
|
|
22
22
|
serializer.items.attributes = []
|
23
23
|
serializer
|
24
24
|
end
|
25
|
+
|
26
|
+
def values_for_test(of_kind)
|
27
|
+
case of_kind
|
28
|
+
when :invalid
|
29
|
+
return [
|
30
|
+
/regex/,
|
31
|
+
:symbol,
|
32
|
+
{},
|
33
|
+
[]
|
34
|
+
]
|
35
|
+
when :valid
|
36
|
+
return [
|
37
|
+
"string",
|
38
|
+
1,
|
39
|
+
1.5,
|
40
|
+
-1,
|
41
|
+
BigDecimal.new(1),
|
42
|
+
(22/7.0).to_r,
|
43
|
+
true,
|
44
|
+
false,
|
45
|
+
nil
|
46
|
+
]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def urls_for_test(of_kind)
|
51
|
+
case of_kind
|
52
|
+
when :invalid
|
53
|
+
return %w(
|
54
|
+
/hello
|
55
|
+
http:hello
|
56
|
+
)
|
57
|
+
when :valid
|
58
|
+
return %w(
|
59
|
+
http://example.com
|
60
|
+
https://example.com
|
61
|
+
http://my.example.com
|
62
|
+
https://my.example.com/?plus=query_string
|
63
|
+
)
|
64
|
+
end
|
65
|
+
end
|
25
66
|
end
|
@@ -16,7 +16,8 @@ module CollectionJson
|
|
16
16
|
def test_that_a_template_can_be_build
|
17
17
|
expected = [
|
18
18
|
{ name: "name", value: "" },
|
19
|
-
{ name: "email", value: "", prompt: "My email" }
|
19
|
+
{ name: "email", value: "", prompt: "My email" },
|
20
|
+
{ name: "password", value: "" }
|
20
21
|
]
|
21
22
|
|
22
23
|
assert_equal expected.to_json, @template.create.to_json
|
@@ -9,7 +9,13 @@ module CollectionJson
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_template_attributes
|
12
|
-
|
12
|
+
expected = [
|
13
|
+
:name,
|
14
|
+
{ email: { prompt: "My email" } },
|
15
|
+
:password
|
16
|
+
]
|
17
|
+
|
18
|
+
assert_equal expected, @user_serializer.class.template
|
13
19
|
end
|
14
20
|
end
|
15
21
|
end
|
@@ -18,13 +18,6 @@ module CollectionJson
|
|
18
18
|
)
|
19
19
|
@user.account = @account
|
20
20
|
|
21
|
-
@invalid_value_types = [
|
22
|
-
/regex/,
|
23
|
-
:symbol,
|
24
|
-
{},
|
25
|
-
[]
|
26
|
-
]
|
27
|
-
|
28
21
|
@invalid = empty_serializer_for(@user)
|
29
22
|
end
|
30
23
|
|
@@ -79,7 +72,7 @@ module CollectionJson
|
|
79
72
|
def test_that_invalid_attributes_return_values_generate_errors
|
80
73
|
@invalid.items.attributes = [:name]
|
81
74
|
|
82
|
-
|
75
|
+
values_for_test(:invalid).each do |invalidate|
|
83
76
|
@user.name = invalidate
|
84
77
|
assert @invalid.invalid?,
|
85
78
|
"#{invalidate} should be invalid"
|
@@ -91,7 +84,7 @@ module CollectionJson
|
|
91
84
|
end
|
92
85
|
|
93
86
|
def test_that_invalid_attributes_properties_values_generate_errors
|
94
|
-
|
87
|
+
values_for_test(:invalid).each do |invalidate|
|
95
88
|
@invalid.items.attributes = [
|
96
89
|
name: {
|
97
90
|
prompt: invalidate
|
@@ -109,7 +102,7 @@ module CollectionJson
|
|
109
102
|
|
110
103
|
# Template
|
111
104
|
def test_that_template_values_validate
|
112
|
-
|
105
|
+
values_for_test(:invalid).each do |invalidate|
|
113
106
|
@invalid.class.template = [
|
114
107
|
name: {
|
115
108
|
prompt: invalidate,
|
@@ -159,7 +152,7 @@ module CollectionJson
|
|
159
152
|
end
|
160
153
|
|
161
154
|
def test_that_queries_values_are_validated
|
162
|
-
|
155
|
+
values_for_test(:invalid).each do |invalidate|
|
163
156
|
@invalid.class.queries = [
|
164
157
|
search: {
|
165
158
|
href: "http://example.com/",
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class Validator
|
6
|
+
class ItemsValidator
|
7
|
+
class ItemsValidatorTest < MiniTest::Test
|
8
|
+
include TestHelper
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@user = User.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_that_attributes_are_validated
|
15
|
+
values_for_test(:invalid).each do |invalid|
|
16
|
+
@user.name = invalid
|
17
|
+
serializer = empty_serializer_for(@user)
|
18
|
+
serializer.items.attributes = [:name]
|
19
|
+
|
20
|
+
items_validator = ItemsValidator.new(serializer)
|
21
|
+
|
22
|
+
assert items_validator.errors.any?, "should have errors"
|
23
|
+
assert items_validator.errors.key?(:attributes),
|
24
|
+
"should have key attributes"
|
25
|
+
assert items_validator.errors[:attributes][0].
|
26
|
+
include?("attributes:name is an invalid value")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_that_links_are_validated
|
31
|
+
values_for_test(:invalid).each do |invalid|
|
32
|
+
serializer = empty_serializer_for(@user)
|
33
|
+
serializer.items.links = [
|
34
|
+
dashboard: {
|
35
|
+
href: "invalid url",
|
36
|
+
rel: invalid
|
37
|
+
}
|
38
|
+
]
|
39
|
+
|
40
|
+
items_validator = ItemsValidator.new(serializer)
|
41
|
+
|
42
|
+
assert items_validator.errors.any?, "should have errors"
|
43
|
+
assert items_validator.errors.key?(:items),
|
44
|
+
"should have key items"
|
45
|
+
assert items_validator.errors[:items][0].
|
46
|
+
include?("items:links:dashboard:href is an invalid URL")
|
47
|
+
assert items_validator.errors[:items][1].
|
48
|
+
include?("items:links:dashboard:rel is an invalid value")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
@@ -2,16 +2,21 @@ require "minitest_helper"
|
|
2
2
|
|
3
3
|
module CollectionJson
|
4
4
|
class Serializer
|
5
|
-
class
|
5
|
+
class Validation
|
6
6
|
class Url
|
7
7
|
class TestUrl < Minitest::Test
|
8
|
+
include TestHelper
|
9
|
+
|
8
10
|
def test_that_urls_validate
|
9
|
-
invalid
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
urls_for_test(:invalid).each do |url|
|
12
|
+
value = Url.new(url)
|
13
|
+
refute value.valid?
|
14
|
+
assert value.invalid?
|
15
|
+
end
|
16
|
+
end
|
13
17
|
|
14
|
-
|
18
|
+
def test_that_different_data_types_generate_error
|
19
|
+
values_for_test(:invalid).each do |url|
|
15
20
|
value = Url.new(url)
|
16
21
|
refute value.valid?
|
17
22
|
assert value.invalid?
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class Validation
|
6
|
+
class TestValidation < MiniTest::Test
|
7
|
+
include TestHelper
|
8
|
+
|
9
|
+
def test_that_instantiating_validation_raises_error
|
10
|
+
user = User.new(name: 'Carles Jove')
|
11
|
+
serializer = empty_serializer_for(user)
|
12
|
+
|
13
|
+
assert_raises(NotImplementedError) do
|
14
|
+
Validation.new(serializer)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -2,36 +2,19 @@ require "minitest_helper"
|
|
2
2
|
|
3
3
|
module CollectionJson
|
4
4
|
class Serializer
|
5
|
-
class
|
5
|
+
class Validation
|
6
6
|
class Value
|
7
7
|
class TestValue < Minitest::Test
|
8
|
-
|
9
|
-
invalid = [
|
10
|
-
/regex/,
|
11
|
-
:symbol,
|
12
|
-
{},
|
13
|
-
[]
|
14
|
-
]
|
15
|
-
|
16
|
-
valid = [
|
17
|
-
"string",
|
18
|
-
1,
|
19
|
-
1.5,
|
20
|
-
-1,
|
21
|
-
BigDecimal.new(1),
|
22
|
-
(22/7.0).to_r,
|
23
|
-
true,
|
24
|
-
false,
|
25
|
-
nil
|
26
|
-
]
|
8
|
+
include TestHelper
|
27
9
|
|
28
|
-
|
10
|
+
def test_that_values_validate
|
11
|
+
values_for_test(:invalid).each do |v|
|
29
12
|
value = Value.new(v)
|
30
13
|
refute value.valid?, "#{v} should be invalid, but was valid"
|
31
14
|
assert value.invalid?, "#{v} should be invalid, but was valid"
|
32
15
|
end
|
33
16
|
|
34
|
-
valid
|
17
|
+
values_for_test(:valid) do |v|
|
35
18
|
value = Value.new(v)
|
36
19
|
assert value.valid?, "#{v} should be valid, but was invalid"
|
37
20
|
refute value.invalid?, "#{v} should be valid, but was invalid"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collection_json_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carles Jove i Buxeda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -93,8 +93,10 @@ files:
|
|
93
93
|
- lib/collection_json_serializer/spec.rb
|
94
94
|
- lib/collection_json_serializer/support.rb
|
95
95
|
- lib/collection_json_serializer/validator.rb
|
96
|
-
- lib/collection_json_serializer/validator/
|
97
|
-
- lib/collection_json_serializer/validator/
|
96
|
+
- lib/collection_json_serializer/validator/base.rb
|
97
|
+
- lib/collection_json_serializer/validator/items_validator.rb
|
98
|
+
- lib/collection_json_serializer/validator/types/url.rb
|
99
|
+
- lib/collection_json_serializer/validator/types/value.rb
|
98
100
|
- lib/collection_json_serializer/version.rb
|
99
101
|
- test/builder/builder_test.rb
|
100
102
|
- test/extensions/open_attrs_test.rb
|
@@ -123,7 +125,9 @@ files:
|
|
123
125
|
- test/support/ext_test.rb
|
124
126
|
- test/support/support_test.rb
|
125
127
|
- test/validator/invalid_test.rb
|
128
|
+
- test/validator/items_validator_test.rb
|
126
129
|
- test/validator/url_validator_test.rb
|
130
|
+
- test/validator/validation_test.rb
|
127
131
|
- test/validator/validator_test.rb
|
128
132
|
- test/validator/value_validator_test.rb
|
129
133
|
homepage: https://github.com/carlesjove/collection_json_serializer
|
@@ -138,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
142
|
requirements:
|
139
143
|
- - ">="
|
140
144
|
- !ruby/object:Gem::Version
|
141
|
-
version: '0'
|
145
|
+
version: '2.0'
|
142
146
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
147
|
requirements:
|
144
148
|
- - ">="
|
@@ -178,7 +182,9 @@ test_files:
|
|
178
182
|
- test/support/ext_test.rb
|
179
183
|
- test/support/support_test.rb
|
180
184
|
- test/validator/invalid_test.rb
|
185
|
+
- test/validator/items_validator_test.rb
|
181
186
|
- test/validator/url_validator_test.rb
|
187
|
+
- test/validator/validation_test.rb
|
182
188
|
- test/validator/validator_test.rb
|
183
189
|
- test/validator/value_validator_test.rb
|
184
190
|
has_rdoc:
|