Skip to content

Commit

Permalink
InvalidPrefixedResclassError and InvalidPrefixedPropError
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Nov 12, 2024
1 parent dd5798d commit 5970aef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
23 changes: 11 additions & 12 deletions dsp_permissions_scripts/doap/doap_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from dsp_permissions_scripts.models.errors import EmptyDoapTargetError
from dsp_permissions_scripts.models.errors import InvalidEntityDoapTargetError
from dsp_permissions_scripts.models.errors import InvalidPrefixedPropError
from dsp_permissions_scripts.models.errors import InvalidPrefixedResclassError
from dsp_permissions_scripts.models.group import PREFIXED_IRI_REGEX
from dsp_permissions_scripts.models.group import Group
from dsp_permissions_scripts.models.scope import PermissionScope
Expand Down Expand Up @@ -82,16 +84,13 @@ def _validate(self) -> Self:

@model_validator(mode="after")
def _validate_name_format(self) -> Self:
if self.prefixed_class and any([x in self.prefixed_class for x in ["#", "/", "knora.org", "dasch.swiss"]]):
raise ValueError(f"The resource class name must not be a full IRI, but you provided {self.prefixed_class}")
if self.prefixed_class and not re.search(PREFIXED_IRI_REGEX, self.prefixed_class):
raise ValueError(
"The resource class name must be in the format 'onto:resclass_name', "
f"but you provided {self.prefixed_class}"
)
if self.prefixed_prop and any([x in self.prefixed_prop for x in ["#", "/", "knora.org", "dasch.swiss"]]):
raise ValueError(f"The property name must not be a full IRI, but you provided {self.prefixed_prop}")
if self.prefixed_prop and not re.search(PREFIXED_IRI_REGEX, self.prefixed_prop):
msg = f"The property name must be in the format 'onto:prop_name', but you provided {self.prefixed_prop}"
raise ValueError(msg)
def _fails(s: str) -> bool:
contains_forbidden = any(x in s for x in ["#", "/", "knora.org", "dasch.swiss"])
fails_regex = not re.search(PREFIXED_IRI_REGEX, s)
return contains_forbidden or fails_regex

if self.prefixed_class and _fails(self.prefixed_class):
raise InvalidPrefixedResclassError(self.prefixed_class)
if self.prefixed_prop and _fails(self.prefixed_prop):
raise InvalidPrefixedPropError(self.prefixed_prop)
return self
24 changes: 22 additions & 2 deletions dsp_permissions_scripts/models/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,30 @@ class EmptyDoapTargetError(Exception):
class InvalidEntityDoapTargetError(Exception):
message: str

def __init__(self, resclass_iri: str) -> None:
def __init__(self, invalid_resclass_iri: str) -> None:
iri_formats = [
"http://0.0.0.0:3333/ontology/<shortcode>/<ontoname>/v2#<classname_or_property_name>",
"http://api.<subdomain>.dasch.swiss/ontology/<shortcode>/<ontoname>/v2#<classname_or_property_name>",
"http://api.knora.org/ontology/knora-api/v2#<knora_base_class_or_base_property>",
]
self.message = f"The IRI must be in one of the formats {iri_formats}, but you provided {resclass_iri}"
self.message = f"The IRI must be in one of the formats {iri_formats}, but you provided {invalid_resclass_iri}"


class InvalidPrefixedResclassError(Exception):
message: str

def __init__(self, invalid_prefixed_resclass: str) -> None:
self.message = (
f"The resource class name must be in the format 'onto:resclass_name', "
f"but you provided {invalid_prefixed_resclass}"
)


class InvalidPrefixedPropError(Exception):
message: str

def __init__(self, invalid_prefixed_property: str) -> None:
self.message = (
f"The property name must be in the format 'onto:property_name', "
f"but you provided {invalid_prefixed_property}"
)

0 comments on commit 5970aef

Please sign in to comment.