Skip to content

Commit

Permalink
Merge pull request #280 from Silvanoc/fix-uris
Browse files Browse the repository at this point in the history
fix: avoid uri shadowing
  • Loading branch information
sierra-moxon authored Oct 16, 2023
2 parents 2fc42c0 + b86c1fa commit 687fc53
Show file tree
Hide file tree
Showing 7 changed files with 414 additions and 28 deletions.
27 changes: 19 additions & 8 deletions linkml_runtime/utils/metamodelcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from linkml_runtime.utils.namespaces import Namespaces
from linkml_runtime.utils.strictness import is_strict

from linkml_runtime.utils.uri_validator import validate_uri
from linkml_runtime.utils.uri_validator import validate_uri_reference
from linkml_runtime.utils.uri_validator import validate_curie

# Reference Decimal to make sure it stays in the imports
_z = Decimal(1)

Expand Down Expand Up @@ -105,17 +109,21 @@ def is_valid(cls, v: Union[str, URIRef, "Curie", "URIorCURIE"]) -> bool:
if not isinstance(v, (str, URIRef, Curie, URIorCURIE)):
return False
v = str(v)
if ':' in v and '://' not in v:
return URIorCURIE.is_curie(v)
if validate_uri(v):
return True
elif validate_uri_reference(v):
return True
else:
return URI.is_valid(v)
return URIorCURIE.is_curie(v)

@staticmethod
def is_absolute(v: str) -> bool:
return bool(urlparse(v).netloc)

@staticmethod
def is_curie(v: str, nsm: Optional[Namespaces] = None) -> bool:
if not validate_curie(v):
return False
if ':' in v and '://' not in v:
ns, ln = v.split(':', 1)
return len(ns) == 0 or (NCName.is_valid(ns) and
Expand All @@ -136,13 +144,14 @@ def __init__(self, v: str) -> None:
raise ValueError(f"'{v}': is not a valid URI")
super().__init__(v)

# this is more inclusive than the W3C specification
#uri_re = re.compile("^[A-Za-z]\\S*$")
uri_re = re.compile("^\\S+$")

@classmethod
def is_valid(cls, v: str) -> bool:
return v is not None and not URIorCURIE.is_curie(v) and cls.uri_re.match(v)
if validate_uri(v):
return True
elif validate_uri_reference(v):
return True
else:
return False


class Curie(URIorCURIE):
Expand Down Expand Up @@ -174,6 +183,8 @@ def ns_ln(cls, v: str) -> Optional[Tuple[str, str]]:

@classmethod
def is_valid(cls, v: str) -> bool:
if not validate_curie(v):
return False
pnln = cls.ns_ln(v)
#return pnln is not None and (not pnln[0] or isinstance(pnln[0], PN_PREFIX))
return pnln is not None
Expand Down
Loading

0 comments on commit 687fc53

Please sign in to comment.