Skip to content

Commit 71ddfe1

Browse files
committed
Adds URI validation.
closes #901
1 parent 181a7fc commit 71ddfe1

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

lib/cocina/models/validators/json_schema_validator.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ def self.validate(...)
3131
# @return [JSONSchema validator] a cached per-definition validator
3232
def self.validator_for(def_name)
3333
@validators ||= {}
34-
@validators[def_name] ||= JSONSchema.validator_for({ '$ref' => "#/$defs/#{def_name}", '$defs' => document['$defs'] })
34+
schema = { '$ref' => "#/$defs/#{def_name}", '$defs' => document['$defs'] }
35+
@validators[def_name] ||= JSONSchema.validator_for(schema, validate_formats: true)
3536
end
3637

3738
# @return [Hash] a hash representation of the schema.json document

schema.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,8 @@
11261126
},
11271127
"uri": {
11281128
"description": "URI value of the descriptive element.",
1129-
"type": "string"
1129+
"type": "string",
1130+
"format": "uri"
11301131
},
11311132
"standard": {
11321133
"$ref": "#/$defs/Standard"
@@ -1416,7 +1417,8 @@
14161417
},
14171418
"uri": {
14181419
"description": "URI for the standard or encoding.",
1419-
"type": "string"
1420+
"type": "string",
1421+
"format": "uri"
14201422
},
14211423
"value": {
14221424
"description": "String describing the standard or encoding.",
@@ -1956,7 +1958,8 @@
19561958
},
19571959
"uri": {
19581960
"description": "URI value of the descriptive element.",
1959-
"type": "string"
1961+
"type": "string",
1962+
"format": "uri"
19601963
},
19611964
"value": {
19621965
"description": "Value of the descriptive element.",
@@ -2984,7 +2987,8 @@
29842987
},
29852988
"uri": {
29862989
"description": "URI for the value source.",
2987-
"type": "string"
2990+
"type": "string",
2991+
"format": "uri"
29882992
},
29892993
"value": {
29902994
"description": "String describing the value source.",
@@ -3031,7 +3035,8 @@
30313035
},
30323036
"uri": {
30333037
"description": "URI for the standard or encoding.",
3034-
"type": "string"
3038+
"type": "string",
3039+
"format": "uri"
30353040
},
30363041
"value": {
30373042
"description": "String describing the standard or encoding.",

spec/cocina/models/description_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,18 @@
9494
end
9595
end
9696

97+
context 'with an invalid uri' do
98+
let(:properties) do
99+
JSON.parse(File.read('spec/fixtures/description/etd.json')).tap do |props|
100+
props['contributor'].first['role'].first['uri'] = 'not a uri'
101+
end
102+
end
103+
104+
it 'raises' do
105+
expect { item }.to raise_error(Cocina::Models::ValidationError)
106+
end
107+
end
108+
97109
describe 'introspecting' do
98110
it 'is possible to get the nodes from the schema' do
99111
# We use this to determine whether the paths in the descriptive spreadsheet are valid

0 commit comments

Comments
 (0)