Skip to content

Commit 4480b1e

Browse files
committed
Adds URI validation.
closes #901
1 parent d4127f2 commit 4480b1e

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
@@ -36,7 +36,8 @@ def self.validate(...)
3636
# @return [JSONSchema validator] a cached per-definition validator
3737
def self.validator_for(def_name)
3838
@validators ||= {}
39-
@validators[def_name] ||= JSONSchema.validator_for({ '$ref' => "#/$defs/#{def_name}", '$defs' => document['$defs'] })
39+
schema = { '$ref' => "#/$defs/#{def_name}", '$defs' => document['$defs'] }
40+
@validators[def_name] ||= JSONSchema.validator_for(schema, validate_formats: true)
4041
end
4142

4243
# @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.",
@@ -3075,7 +3078,8 @@
30753078
},
30763079
"uri": {
30773080
"description": "URI for the value source.",
3078-
"type": "string"
3081+
"type": "string",
3082+
"format": "uri"
30793083
},
30803084
"value": {
30813085
"description": "String describing the value source.",
@@ -3122,7 +3126,8 @@
31223126
},
31233127
"uri": {
31243128
"description": "URI for the standard or encoding.",
3125-
"type": "string"
3129+
"type": "string",
3130+
"format": "uri"
31263131
},
31273132
"value": {
31283133
"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)