Skip to content

Commit 6e4e9ae

Browse files
committed
Clean up naming and usage of validation predicates
This clarifies the difference between validate! methods that should raise, and the valid? method that does not raise. Placates Rubocop.
1 parent 26204d5 commit 6e4e9ae

File tree

11 files changed

+42
-34
lines changed

11 files changed

+42
-34
lines changed

lib/geo_combine/bounding_box.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ def valid?
4848
end
4949

5050
def self.from_envelope(envelope)
51-
return if envelope.nil?
52-
5351
envelope = envelope[/.*ENVELOPE\(([^)]*)/, 1].split(',')
5452
new(
5553
west: envelope[0],
@@ -63,8 +61,6 @@ def self.from_envelope(envelope)
6361
# @param [String] spatial w,s,e,n or w s e n
6462
# @param [String] delimiter "," or " "
6563
def self.from_string_delimiter(spatial, delimiter: ',')
66-
return if spatial.nil?
67-
6864
spatial = spatial.split(delimiter)
6965
new(
7066
west: spatial[0],

lib/geo_combine/esri_open_data.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module GeoCombine
44
# Data model for ESRI's open data portal metadata
55
class EsriOpenData
66
include GeoCombine::Formatting
7+
78
attr_reader :metadata
89

910
##

lib/geo_combine/geoblacklight.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,29 @@ def to_json(options = {})
5555
metadata.to_json(options)
5656
end
5757

58+
# True if the GeoBlacklight-Schema document is valid
59+
# @return [Boolean]
60+
def valid?
61+
validate!
62+
true
63+
rescue StandardError
64+
false
65+
end
66+
5867
##
5968
# Validates a GeoBlacklight-Schema json document
6069
# @return [Boolean]
61-
def valid?
62-
JSON::Validator.validate!(schema, to_json, fragment: '#/definitions/layer') &&
63-
dct_references_validate! &&
64-
spatial_validate!
70+
def validate!
71+
JSON::Validator.validate!(schema, to_json, fragment: '#/definitions/layer')
72+
validate_references!
73+
validate_spatial!
6574
end
6675

6776
##
6877
# Validate dct_references_s
6978
# @return [Boolean]
70-
def dct_references_validate!
71-
return true unless metadata.key?('dct_references_s') # TODO: shouldn't we require this field?
79+
def validate_references!
80+
return unless metadata.key?('dct_references_s') # Not required
7281

7382
begin
7483
ref = JSON.parse(metadata['dct_references_s'])
@@ -83,8 +92,8 @@ def dct_references_validate!
8392
end
8493
end
8594

86-
def spatial_validate!
87-
GeoCombine::BoundingBox.from_envelope(metadata['solr_geom']).valid?
95+
def validate_spatial!
96+
raise GeoCombine::Exceptions::InvalidGeometry unless GeoCombine::BoundingBox.from_envelope(metadata['solr_geom']).valid?
8897
end
8998

9099
private
@@ -163,7 +172,7 @@ def upgrade_to_v1
163172
metadata.except!(*DEPRECATED_KEYS_V1)
164173

165174
# ensure we have a proper v1 record
166-
valid?
175+
validate!
167176
end
168177

169178
def schema

lib/geo_combine/ogp.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module GeoCombine
88
class OGP
99
class InvalidMetadata < RuntimeError; end
1010
include GeoCombine::Formatting
11+
1112
attr_reader :metadata
1213

1314
##

spec/features/fgdc2html_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# TODO: Provide additional expectations on html structure
66
describe 'FGDC to html' do
77
include XmlDocs
8+
89
let(:page) { GeoCombine::Fgdc.new(tufts_fgdc).to_html }
910

1011
describe 'Identification Information' do

spec/features/iso2html_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# TODO: Provide additional expectations on html structure
66
describe 'ISO 19139 to html' do
77
include XmlDocs
8+
89
let(:page) { GeoCombine::Iso19139.new(stanford_iso).to_html }
910

1011
describe 'Identification Information' do

spec/lib/geo_combine/ckan_metadata_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
RSpec.describe GeoCombine::CkanMetadata do
66
include JsonDocs
7+
78
let(:ckan_sample) { described_class.new(ckan_metadata) }
89

910
describe '#to_geoblacklight' do

spec/lib/geo_combine/esri_open_data_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
RSpec.describe GeoCombine::EsriOpenData do
66
include JsonDocs
7+
78
let(:esri_sample) { described_class.new(esri_opendata_metadata) }
89
let(:metadata) { esri_sample.instance_variable_get(:@metadata) }
910

spec/lib/geo_combine/fgdc_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
RSpec.describe GeoCombine::Fgdc do
66
include XmlDocs
7+
78
let(:fgdc_object) { described_class.new(tufts_fgdc) }
89

910
describe '#initialize' do
@@ -32,7 +33,7 @@
3233
end
3334

3435
it 'is not valid due to bad modification date but valid otherwise' do
35-
expect { fgdc_geobl.valid? }.to raise_error(JSON::Schema::ValidationError, /layer_modified_dt/)
36+
expect { fgdc_geobl.validate! }.to raise_error(JSON::Schema::ValidationError, /layer_modified_dt/)
3637
fgdc_geobl.metadata.delete 'layer_modified_dt'
3738
expect(fgdc_geobl).to be_valid
3839
end

spec/lib/geo_combine/geoblacklight_spec.rb

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
include XmlDocs
77
include JsonDocs
88
include GeoCombine::Exceptions
9+
910
let(:full_geobl) { described_class.new(full_geoblacklight) }
1011
let(:enhanced_geobl) { described_class.new(basic_geoblacklight, 'layer_geom_type_s' => 'esriGeometryPolygon') }
1112
let(:basic_geobl) { described_class.new(basic_geoblacklight) }
@@ -62,9 +63,9 @@
6263
end
6364
end
6465

65-
describe '#valid?' do
66+
describe '#validate!' do
6667
it 'a valid geoblacklight-schema document should be valid' do
67-
expect(full_geobl.valid?).to be true
68+
expect(full_geobl).to be_valid
6869
end
6970

7071
context 'must have required fields' do
@@ -78,7 +79,7 @@
7879
].each do |field|
7980
it field do
8081
full_geobl.metadata.delete field
81-
expect { full_geobl.valid? }.to raise_error(JSON::Schema::ValidationError, /#{field}/)
82+
expect { full_geobl.validate! }.to raise_error(JSON::Schema::ValidationError, /#{field}/)
8283
end
8384
end
8485
end
@@ -104,7 +105,7 @@
104105
].each do |field|
105106
it field do
106107
full_geobl.metadata.delete field
107-
expect { full_geobl.valid? }.not_to raise_error
108+
expect { full_geobl.validate! }.not_to raise_error
108109
end
109110
end
110111
end
@@ -119,31 +120,25 @@
119120
].each do |field|
120121
it field do
121122
full_geobl.metadata.delete field
122-
expect { full_geobl.valid? }.not_to raise_error
123+
expect { full_geobl.validate! }.not_to raise_error
123124
end
124125
end
125126
end
126127

127128
it 'an invalid document' do
128-
expect { basic_geobl.valid? }.to raise_error JSON::Schema::ValidationError
129+
expect { basic_geobl.validate! }.to raise_error JSON::Schema::ValidationError
129130
end
130131

131132
it 'calls the dct_references_s validator' do
132-
expect(enhanced_geobl).to receive(:dct_references_validate!)
133+
expect(enhanced_geobl).to receive(:validate_references!)
133134
enhanced_geobl.valid?
134135
end
135-
136-
it 'validates spatial bounding box' do
137-
expect(JSON::Validator).to receive(:validate!).and_return true
138-
expect { basic_geobl.valid? }
139-
.to raise_error GeoCombine::Exceptions::InvalidGeometry
140-
end
141136
end
142137

143-
describe '#dct_references_validate!' do
138+
describe '#validate_references!' do
144139
context 'with valid document' do
145140
it 'is valid' do
146-
expect(full_geobl.dct_references_validate!).to be true
141+
expect { full_geobl.validate_references! }.not_to raise_error
147142
end
148143
end
149144

@@ -173,22 +168,22 @@
173168
end
174169

175170
it 'unparseable json' do
176-
expect { bad_ref.dct_references_validate! }.to raise_error JSON::ParserError
171+
expect { bad_ref.validate_references! }.to raise_error JSON::ParserError
177172
end
178173

179174
it 'not a hash' do
180-
expect { not_hash.dct_references_validate! }.to raise_error GeoCombine::Exceptions::InvalidDCTReferences
175+
expect { not_hash.validate_references! }.to raise_error GeoCombine::Exceptions::InvalidDCTReferences
181176
end
182177
end
183178
end
184179

185-
describe 'spatial_validate!' do
180+
describe 'validate_spatial!' do
186181
context 'when valid' do
187-
it { expect { full_geobl.spatial_validate! }.not_to raise_error }
182+
it { expect { full_geobl.validate_spatial! }.not_to raise_error }
188183
end
189184

190185
context 'when invalid' do
191-
it { expect { basic_geobl.spatial_validate! }.to raise_error GeoCombine::Exceptions::InvalidGeometry }
186+
it { expect { basic_geobl.validate_spatial! }.to raise_error GeoCombine::Exceptions::InvalidGeometry }
192187
end
193188
end
194189

0 commit comments

Comments
 (0)