Skip to content

Commit 5de44b8

Browse files
Fix: add format check to the identifier notation (#223)
* add format check to the identifier notation * Fix test using the good format of identifier notations
1 parent faee36d commit 5de44b8

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

lib/ontologies_linked_data/models/agents/identifier.rb

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class AgentIdentifier < LinkedData::Models::Base
66

77
model :Identifier, namespace: :adms, name_with: lambda { |i| generate_identifier(i.notation, i.schemaAgency)}
88

9-
attribute :notation, namespace: :skos, enforce: %i[existence no_url]
9+
attribute :notation, namespace: :skos, enforce: %i[existence no_url notation_format]
1010
attribute :schemaAgency, namespace: :adms, enforcedValues: IDENTIFIER_SCHEMES.keys, enforce: [:existence]
1111
attribute :schemeURI, handler: :scheme_uri_infer
1212
attribute :creator, type: :user, enforce: [:existence]
@@ -31,6 +31,33 @@ def no_url(inst,attr)
3131
return notation&.start_with?('http') ? [:no_url, "`notation` must not be a URL"] : []
3232
end
3333

34+
def notation_format(inst, attr)
35+
inst.bring([attr, :schemaAgency]) if inst.bring?(attr)
36+
notation = inst.send(attr)
37+
schema_agency = inst.send(:schemaAgency)
38+
39+
# Validate notation format depending on schema to not have weird ids
40+
case schema_agency
41+
when "ROR"
42+
unless notation.match?(/^[0-9a-z]{9}$/i) # ROR IDs are 9-char base32
43+
return [:notation_format, "`notation` must be compliant with ROR format"]
44+
end
45+
when "ORCID"
46+
unless notation.match?(/^\d{4}-\d{4}-\d{4}-\d{3}[\dX]$/)
47+
return [:notation_format, "`notation` must be compliant with ORCID format"]
48+
end
49+
when "ISNI"
50+
unless notation.match?(/^\d{4}\s?\d{4}\s?\d{4}\s?\d{3}[\dX]$/)
51+
return [:notation_format, "`notation` must be compliant with ISNI format"]
52+
end
53+
when "GRID"
54+
unless notation.match?(/^grid\.[0-9]+\.[a-f0-9]{1,2}$/i)
55+
return [:notation_format, "`notation` must be compliant with GRID format"]
56+
end
57+
end
58+
59+
end
60+
3461
def scheme_uri_infer
3562
self.bring(:schemaAgency) if self.bring?(:schemaAgency)
3663
IDENTIFIER_SCHEMES[self.schemaAgency.to_sym] if self.schemaAgency

test/models/test_agent.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_agent_no_valid
2525
]
2626
@identifiers = [
2727
LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ROR', creator: @@user1),
28-
LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ORCID', creator: @@user1),
28+
LinkedData::Models::AgentIdentifier.new(notation: '0000-0012-1501-8134', schemaAgency: 'ORCID', creator: @@user1),
2929
]
3030

3131
@identifiers.each { |i| i.save }
@@ -75,7 +75,7 @@ def test_identifier_no_valid
7575

7676
refute LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ROR', creator: @@user1).valid?
7777

78-
assert LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ORCID', creator: @@user1).valid?
78+
assert LinkedData::Models::AgentIdentifier.new(notation: '0000-0012-1501-8134', schemaAgency: 'ORCID', creator: @@user1).valid?
7979
id.delete
8080
end
8181

test/models/test_search.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_search_agents
9797
]
9898
@identifiers = [
9999
LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ROR', creator: @@user1),
100-
LinkedData::Models::AgentIdentifier.new(notation: '000h6jb29', schemaAgency: 'ORCID', creator: @@user1),
100+
LinkedData::Models::AgentIdentifier.new(notation: '0000-0012-1501-8134', schemaAgency: 'ORCID', creator: @@user1),
101101
]
102102

103103
@identifiers.each { |i| i.save }

0 commit comments

Comments
 (0)