jsonapi-validations 0.1.1.beta1 → 0.1.1.beta3
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/jsonapi/validations.rb +3 -0
- data/lib/jsonapi/validations/relationship.rb +17 -11
- data/lib/jsonapi/validations/resource.rb +35 -24
- metadata +29 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50c60a21dbdeffb1d691453e6d7ff0fbb26687e7
|
4
|
+
data.tar.gz: 7c83f67e2f20618b772d8c32d3d5c98910db64e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15047d47eaf62e4080591e517f84db8a35bf42617b62cf85d1ad59ace892499cfdf1786e21acfbd2719157926731ff397091735072d28f5eeaccf05651dd35c1
|
7
|
+
data.tar.gz: 24d80073f4377572b7d409d67268388285c3357824c209fdf86b5ee852d2d0ee324adc4d14ae93f20ec126d317a9e46a6851b1453d2bb21fcabf1b54685895e8
|
data/lib/jsonapi/validations.rb
CHANGED
@@ -11,7 +11,7 @@ module JSONAPI
|
|
11
11
|
# @param [Hash] params Validation parameters.
|
12
12
|
# @option [Symbol] kind Whether it is a :has_many or :has_one.
|
13
13
|
# @option [Array<Symbol>] types Permitted types for the relationship.
|
14
|
-
# @raise [JSONAPI::
|
14
|
+
# @raise [JSONAPI::Parser::InvalidDocument] if document is invalid.
|
15
15
|
def validate_relationship!(document, params = {})
|
16
16
|
JSONAPI.parse_relationship!(document)
|
17
17
|
validate_types!(document['data'], params[:types])
|
@@ -21,23 +21,29 @@ module JSONAPI
|
|
21
21
|
def validate_types!(rel, rel_types, key = nil)
|
22
22
|
rel_name = key ? " #{key}" : ''
|
23
23
|
if rel_types[:kind] == :has_many
|
24
|
-
|
25
|
-
|
24
|
+
unless rel['data'].is_a?(Array)
|
25
|
+
raise JSONAPI::Parser::InvalidDocument,
|
26
|
+
"Expected relationship#{rel_name} to be has_many."
|
27
|
+
end
|
26
28
|
return unless rel_types[:types]
|
27
29
|
rel['data'].each do |ri|
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
next if rel_types[:types].include?(ri['type'].to_sym)
|
31
|
+
raise JSONAPI::Parser::InvalidDocument,
|
32
|
+
"Type mismatch for relationship#{rel_name}: " \
|
33
|
+
"#{ri['type']} should be one of #{rel_types}"
|
31
34
|
end
|
32
35
|
else
|
33
36
|
return if rel['data'].nil?
|
34
|
-
|
35
|
-
|
37
|
+
unless rel['data'].is_a?(Hash)
|
38
|
+
raise JSONAPI::Parser::InvalidDocument,
|
39
|
+
"Expected relationship#{rel_name} to be has_one."
|
40
|
+
end
|
36
41
|
return unless rel_types[:types]
|
37
42
|
ri = rel['data']
|
38
|
-
|
39
|
-
|
40
|
-
|
43
|
+
return if rel_types[:types].include?(ri['type'].to_sym)
|
44
|
+
raise JSONAPI::Parser::InvalidDocument,
|
45
|
+
"Type mismatch for relationship#{rel_name}: " \
|
46
|
+
"#{ri['type']} should be one of #{rel_types}"
|
41
47
|
end
|
42
48
|
end
|
43
49
|
end
|
@@ -19,7 +19,7 @@ module JSONAPI
|
|
19
19
|
# @option [Hash] types Permitted primary/relationships types. Optional.
|
20
20
|
# The relationships must be explicitly permitted. Not all
|
21
21
|
# relationships' types have to be specified.
|
22
|
-
# @raise [JSONAPI::
|
22
|
+
# @raise [JSONAPI::Parser::InvalidDocument] if document is invalid.
|
23
23
|
#
|
24
24
|
# @example
|
25
25
|
# params = {
|
@@ -58,54 +58,65 @@ module JSONAPI
|
|
58
58
|
# @api private
|
59
59
|
def validate_permitted!(data, permitted)
|
60
60
|
return if permitted.nil?
|
61
|
-
|
62
|
-
|
61
|
+
unless permitted[:id] || !data.key?('id')
|
62
|
+
raise JSONAPI::Parser::InvalidDocument,
|
63
|
+
'Unpermitted id.'
|
64
|
+
end
|
63
65
|
# TODO(beauby): Handle meta (and possibly links) once the spec has
|
64
66
|
# been clarified.
|
65
67
|
permitted_attrs = permitted[:attributes] || []
|
66
68
|
if data.key?('attributes')
|
67
69
|
data['attributes'].keys.each do |attr|
|
68
|
-
|
69
|
-
|
70
|
+
unless permitted_attrs.include?(attr.to_sym)
|
71
|
+
raise JSONAPI::Parser::InvalidDocument,
|
72
|
+
"Unpermitted attribute #{attr}"
|
73
|
+
end
|
70
74
|
end
|
71
75
|
end
|
72
76
|
permitted_rels = permitted[:relationships] || []
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
+
return unless data.key?('relationships')
|
78
|
+
data['relationships'].keys.each do |rel|
|
79
|
+
next if permitted_rels.include?(rel.to_sym)
|
80
|
+
raise JSONAPI::Parser::InvalidDocument
|
77
81
|
end
|
78
82
|
end
|
79
83
|
|
80
84
|
# @api private
|
81
85
|
def validate_required!(data, required)
|
82
86
|
return if required.nil?
|
83
|
-
|
84
|
-
|
87
|
+
unless data.key?('id') || !required[:id]
|
88
|
+
raise JSONAPI::Parser::InvalidDocument, 'Missing required id.'
|
89
|
+
end
|
85
90
|
# TODO(beauby): Same as for permitted.
|
86
91
|
|
87
|
-
|
88
|
-
|
92
|
+
unless data.key?('attributes') || !required[:attributes]
|
93
|
+
raise JSONAPI::Parser::InvalidDocument, 'Missing required attributes.'
|
94
|
+
end
|
89
95
|
required[:attributes].each do |attr|
|
90
|
-
|
91
|
-
|
96
|
+
next if data['attributes'][attr.to_s]
|
97
|
+
raise JSONAPI::Parser::InvalidDocument,
|
98
|
+
"Missing required attribute #{attr}."
|
99
|
+
end
|
100
|
+
unless data.key?('relationships') || !required[:relationships]
|
101
|
+
raise JSONAPI::Parser::InvalidDocument,
|
102
|
+
'Missing required relationships.'
|
92
103
|
end
|
93
|
-
Document.ensure!(data.key?('relationships') ||
|
94
|
-
!required[:relationships],
|
95
|
-
'Missing required relationships.')
|
96
104
|
required[:relationships].each do |rel|
|
97
|
-
|
98
|
-
|
105
|
+
unless data['relationships'][rel.to_s]
|
106
|
+
raise JSONAPI::Parser::InvalidDocument,
|
107
|
+
"Missing required relationship #{rel}."
|
108
|
+
end
|
99
109
|
end
|
100
110
|
end
|
101
111
|
|
102
112
|
# @api private
|
103
113
|
def validate_types!(data, types)
|
104
114
|
return if types.nil?
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
115
|
+
unless !types[:primary] || types[:primary].include?(data['type'].to_sym)
|
116
|
+
raise JSONAPI::Parser::InvalidDocument,
|
117
|
+
"Type mismatch for resource: #{data['type']} " \
|
118
|
+
"should be one of #{types[:primary]}"
|
119
|
+
end
|
109
120
|
return unless data.key?('relationships') && types.key?(:relationships)
|
110
121
|
types[:relationships].each do |key, rel_types|
|
111
122
|
rel = data['relationships'][key.to_s]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.1.
|
4
|
+
version: 0.1.1.beta3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Hosseini
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
27
55
|
description: Validate presence/absence of resource creation/update and relationship
|
28
56
|
update payloads, as well as types related resources
|
29
57
|
email: lucas.hosseini@gmail.com
|