Skip to content

Commit 613906b

Browse files
committed
Start moving validation inside jsonapi-validator.
1 parent d99f29a commit 613906b

17 files changed

+18
-137
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require 'jsonapi'
2525
Then, see docs for the part you are interested in:
2626
* [parsing](parser/README.md)
2727
* [rendering](renderer/README.md)
28+
* [validating](validator/README.md)
2829

2930
## License
3031

jsonapi.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Gem::Specification.new do |spec|
1414

1515
spec.add_dependency 'jsonapi-parser', version
1616
spec.add_dependency 'jsonapi-renderer', version
17+
spec.add_dependency 'jsonapi-validator', version
1718

1819
spec.add_development_dependency 'rake', '>=0.9'
1920
spec.add_development_dependency 'rspec', '~>3.4'

parser/jsonapi-parser.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ Gem::Specification.new do |spec|
1212

1313
spec.files = Dir['LICENSE', 'README.md', 'lib/**/*']
1414
spec.require_path = 'lib'
15+
16+
spec.add_dependency 'jsonapi-validator'
1517
end

parser/lib/jsonapi/parser.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'jsonapi/validator'
12
require 'jsonapi/parser/attributes'
23
require 'jsonapi/parser/document'
34
require 'jsonapi/parser/error'
@@ -21,7 +22,7 @@ module JSONAPI
2122
# objects in the primary data must have an id.
2223
# @return [JSONAPI::Parser::Document]
2324
def parse(document, options = {})
24-
raise Parser::InvalidDocument, 'document must be a hash' unless document.is_a?(Hash)
25+
validate!(document)
2526
Parser::Document.new(document, options)
2627
end
2728
end

parser/lib/jsonapi/parser/attributes.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ class Attributes
55
include Enumerable
66

77
def initialize(attributes_hash, options = {})
8-
fail InvalidDocument,
9-
"the value of 'attributes' MUST be an object" unless
10-
attributes_hash.is_a?(Hash)
11-
128
@hash = attributes_hash
139
@attributes = {}
1410
attributes_hash.each do |attr_name, attr_val|

parser/lib/jsonapi/parser/document.rb

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,11 @@ def collection?
3636
private
3737

3838
def validate!
39-
case
40-
when !@data_defined && !@meta_defined && !@errors_defined
41-
fail InvalidDocument,
42-
"a document MUST contain at least one of 'data', 'meta', or" \
43-
" or 'errors' at top-level"
44-
when @data_defined && @errors_defined
45-
fail InvalidDocument,
46-
"'data' and 'errors' MUST NOT coexist in the same document"
47-
when !@data_defined && @included_defined
48-
fail InvalidDocument, "'included' MUST NOT be present unless 'data' is"
49-
when @options[:verify_duplicates] && duplicates?
50-
fail InvalidDocument,
39+
if @options[:verify_duplicates] && duplicates?
40+
raise JSONAPI::Validator::InvalidDocument,
5141
"resources MUST NOT appear both in 'data' and 'included'"
52-
when @options[:verify_linkage] && !full_linkage?
53-
fail InvalidDocument,
42+
elsif @options[:verify_linkage] && !full_linkage?
43+
raise JSONAPI::Validator::InvalidDocument,
5444
"resources in 'included' MUST respect full-linkage"
5545
end
5646
end
@@ -69,8 +59,7 @@ def full_linkage?
6959
return true unless @included
7060

7161
reachable = Set.new
72-
# NOTE(lucas): Does Array() already dup?
73-
queue = Array(data).dup
62+
queue = Array(data)
7463
included_resources = Hash[included.map { |r| [[r.type, r.id], r] }]
7564
queue.each { |resource| reachable << [resource.type, resource.id] }
7665

@@ -94,33 +83,23 @@ def full_linkage?
9483
def parse_data(data_hash)
9584
collection = data_hash.is_a?(Array)
9685
if collection
97-
data_hash.map { |h| Resource.new(h, @options.merge(id_optional: true)) }
86+
data_hash.map { |h| Resource.new(h, @options) }
9887
elsif data_hash.nil?
9988
nil
10089
else
101-
Resource.new(data_hash, @options.merge(id_optional: true))
90+
Resource.new(data_hash, @options)
10291
end
10392
end
10493

10594
def parse_meta(meta_hash)
106-
fail InvalidDocument, "the value of 'meta' MUST be an object" unless
107-
meta_hash.is_a?(Hash)
10895
meta_hash
10996
end
11097

11198
def parse_included(included_hash)
112-
fail InvalidDocument,
113-
"the value of 'included' MUST be an array of resource objects" unless
114-
included_hash.is_a?(Array)
115-
11699
included_hash.map { |h| Resource.new(h, @options) }
117100
end
118101

119102
def parse_errors(errors_hash)
120-
fail InvalidDocument,
121-
"the value of 'errors' MUST be an array of error objects" unless
122-
errors_hash.is_a?(Array)
123-
124103
errors_hash.map { |h| Error.new(h, @options) }
125104
end
126105
end

parser/lib/jsonapi/parser/error.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ class Error
55
attr_reader :id, :links, :status, :code, :title, :detail, :source, :meta
66

77
def initialize(error_hash, options = {})
8-
fail InvalidDocument,
9-
"the value of 'errors' MUST be an array of error objects" unless
10-
error_hash.is_a?(Hash)
11-
128
@hash = error_hash
139
@id = error_hash['id'] if error_hash.key?('id')
1410
links_hash = error_hash['links'] || {}

parser/lib/jsonapi/parser/jsonapi.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ class JsonApi
55
attr_reader :version, :meta
66

77
def initialize(jsonapi_hash, options = {})
8-
fail InvalidDocument, "the value of 'jsonapi' MUST be an object" unless
9-
jsonapi_hash.is_a?(Hash)
10-
118
@hash = jsonapi_hash
129
@version = jsonapi_hash['version'] if jsonapi_hash.key?('meta')
1310
@meta = jsonapi_hash['meta'] if jsonapi_hash.key?('meta')

parser/lib/jsonapi/parser/link.rb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class Link
77
def initialize(link_hash, options = {})
88
@hash = link_hash
99

10-
validate!(link_hash)
1110
@value = link_hash
1211
return unless link_hash.is_a?(Hash)
1312

@@ -18,26 +17,6 @@ def initialize(link_hash, options = {})
1817
def to_hash
1918
@hash
2019
end
21-
22-
private
23-
24-
def validate!(link_hash)
25-
case
26-
when !link_hash.is_a?(String) && !link_hash.is_a?(Hash)
27-
fail InvalidDocument,
28-
"a 'link' object MUST be either a string or an object"
29-
when link_hash.is_a?(Hash) && (!link_hash.key?('href') ||
30-
!link_hash['href'].is_a?(String))
31-
fail InvalidDocument,
32-
"a 'link' object MUST be either a string or an object containing" \
33-
" an 'href' string"
34-
when link_hash.is_a?(Hash) && (!link_hash.key?('meta') ||
35-
!link_hash['meta'].is_a?(Hash))
36-
fail InvalidDocument,
37-
"a 'link' object MUST be either a string or an object containing" \
38-
" an 'meta' object"
39-
end
40-
end
4120
end
4221
end
4322
end

parser/lib/jsonapi/parser/links.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ module Parser
33
# c.f. http://jsonapi.org/format/#document-links
44
class Links
55
def initialize(links_hash, options = {})
6-
fail InvalidDocument, "the value of 'links' MUST be an object" unless
7-
links_hash.is_a?(Hash)
8-
96
@hash = links_hash
107
@links = {}
118
links_hash.each do |link_name, link_val|

0 commit comments

Comments
 (0)