Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve curie validation #285

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions linkml_runtime/utils/metamodelcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,17 @@

@classmethod
def ns_ln(cls, v: str) -> Optional[Tuple[str, str]]:
# See if this is indeed a valid CURIE, ie, it can be split by a colon
if not validate_curie(v):
return None

Check warning on line 171 in linkml_runtime/utils/metamodelcore.py

View check run for this annotation

Codecov / codecov/patch

linkml_runtime/utils/metamodelcore.py#L171

Added line #L171 was not covered by tests
# assume the presence of a colon and try to split the CURIE
curie_split = v.split(':', 1)
if len(curie_split) == 1:
# there is no ':' character in the string, ie, it is not a valid CURIE
return None
# there is no ':' character in the string, it's only a reference
return '', v
else:
prefix = curie_split[0].lower()
if not NCName.is_valid(prefix):
return None
reference = curie_split[1]
if not cls.term_name.match(reference):
return None
return prefix, reference
return prefix, reference

@classmethod
def is_valid(cls, v: str) -> bool:
Expand Down
10 changes: 4 additions & 6 deletions tests/test_utils/test_metamodelcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,17 @@ def test_uriorcuries(self):
def test_curie(self):
""" Test the CURIE type """
self.assertEqual("rdf:type", Curie("rdf:type"))
with self.assertRaises(ValueError):
Curie("type")
self.assertFalse(Curie.is_valid("type"))
self.assertTrue(Curie.is_valid("type"))
self.assertEqual(":type", Curie(":type"))
self.assertTrue(Curie.is_valid(':type'))
self.assertTrue(Curie.is_valid('_abc:123'))
self.assertTrue(Curie.is_valid('WIKIDATA_PROPERTY:P854'))
self.assertTrue(Curie.is_valid('WIKIDATA.PROPERTY:P854'))
with self.assertRaises(ValueError):
Curie("1df:type")
self.assertFalse(Curie.is_valid('1df:type'))
with self.assertRaises(ValueError):
Curie("rdf:17")
self.assertFalse(Curie.is_valid('rdf:17'))
self.assertTrue(Curie.is_valid('rdf:17'))
self.assertTrue(Curie.is_valid('17'))
nsm = Namespaces(Graph())
self.assertEqual(RDF.type, Curie("rdf:type").as_uri(nsm))
self.assertIsNone(Curie("ex:foo").as_uri(nsm))
Expand Down