Skip to content

Commit

Permalink
Merge pull request #276 from RDFLib/lawson/predicate-validation
Browse files Browse the repository at this point in the history
lawson/predicate validation
  • Loading branch information
lalewis1 authored Oct 9, 2024
2 parents 7ceebfd + 372ea1c commit a4c1728
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 70 deletions.
59 changes: 5 additions & 54 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 30 additions & 14 deletions prez/config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from os import environ
from pathlib import Path
from typing import Optional, List, Tuple
from typing import Union, Any, Dict
from typing import Any, Dict, List, Optional, Tuple, Union

import toml
from pydantic import field_validator
from pydantic_settings import BaseSettings
from rdflib import URIRef, DCTERMS, RDFS, SDO
from rdflib import DCTERMS, RDFS, SDO, URIRef
from rdflib.namespace import SKOS

from prez.reference_data.prez_ns import REG, EP
from prez.reference_data.prez_ns import EP, REG


class Settings(BaseSettings):
Expand Down Expand Up @@ -40,19 +39,25 @@ class Settings(BaseSettings):
order_lists_by_label: bool = True
listing_count_limit: int = 100
search_count_limit: int = 10
label_predicates: Optional[List[URIRef]] = [
label_predicates: list = [
SKOS.prefLabel,
DCTERMS.title,
RDFS.label,
SDO.name,
]
description_predicates: Optional[List[URIRef]] = [
description_predicates: list = [
SKOS.definition,
DCTERMS.description,
SDO.description,
]
provenance_predicates: Optional[List[URIRef]] = [DCTERMS.provenance]
other_predicates: Optional[List[URIRef]] = [SDO.color, REG.status]
provenance_predicates: list = [DCTERMS.provenance]
search_predicates: list = [
RDFS.label,
SKOS.prefLabel,
SDO.name,
DCTERMS.title,
]
other_predicates: list = [SDO.color, REG.status]
sparql_repo_type: str = "remote"
sparql_timeout: int = 30
log_level: str = "INFO"
Expand All @@ -66,12 +71,6 @@ class Settings(BaseSettings):
prez_contact: Optional[Dict[str, Union[str, Any]]] = None
disable_prefix_generation: bool = False
default_language: str = "en"
default_search_predicates: Optional[List[URIRef]] = [
RDFS.label,
SKOS.prefLabel,
SDO.name,
DCTERMS.title,
]
local_rdf_dir: str = "rdf"
endpoint_structure: Optional[Tuple[str, ...]] = ("catalogs", "collections", "items")
system_endpoints: Optional[List[URIRef]] = [
Expand Down Expand Up @@ -114,5 +113,22 @@ def get_version(cls, v):
"PREZ_VERSION not set, and cannot find a pyproject.toml to extract the version."
)

@field_validator(
"label_predicates",
"description_predicates",
"provenance_predicates",
"search_predicates",
"other_predicates",
)
def validate_predicates(cls, v):
try:
v = [URIRef(predicate) for predicate in v]
except ValueError as e:
raise ValueError(
"Could not parse predicates. predicates must be valid URIs no prefixes allowed "
f"original message: {e}"
)
return v


settings = Settings()
2 changes: 1 addition & 1 deletion prez/services/query_generation/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
limit += 1 # increase the limit by one so we know if there are further pages of results.

if not predicates:
predicates = settings.default_search_predicates
predicates = settings.search_predicates

sr_uri: Var = Var(value="focus_node")
pred: Var = Var(value="pred")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ include = [
]

[tool.poetry.dependencies]
python = "^3.11"
python = "^3.12"
uvicorn = {version = "^0.30.0", optional = true }
httpx = "^0.27.0"
rdflib = "^7.0.0"
Expand Down
88 changes: 88 additions & 0 deletions tests/test_predicates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import pytest

from prez.config import Settings


@pytest.mark.parametrize(
"label_predicates, error",
[
[["https://schema.org/name"], None],
[["1", "2", "3"], None],
[[1], TypeError],
["not a list", ValueError],
],
)
def test_label_predicates(label_predicates, error):
if error:
with pytest.raises(error):
assert Settings(label_predicates=label_predicates)
else:
assert Settings(label_predicates=label_predicates)


@pytest.mark.parametrize(
"description_predicates, error",
[
[["https://schema.org/description"], None],
[["1", "2", "3"], None],
[[1], TypeError],
["not a list", ValueError],
],
)
def test_description_predicates(description_predicates, error):
if error:
with pytest.raises(error):
assert Settings(description_predicates=description_predicates)
else:
assert Settings(description_predicates=description_predicates)


@pytest.mark.parametrize(
"provenance_predicates, error",
[
[["https://schema.org/provenance"], None],
[["1", "2", "3"], None],
[[1], TypeError],
["not a list", ValueError],
],
)
def test_provenance_predicates(provenance_predicates, error):
if error:
with pytest.raises(error):
assert Settings(provenance_predicates=provenance_predicates)
else:
assert Settings(provenance_predicates=provenance_predicates)


@pytest.mark.parametrize(
"search_predicates, error",
[
[["https://schema.org/search"], None],
[["1", "2", "3"], None],
[[1], TypeError],
["not a list", ValueError],
],
)
def test_search_predicates(search_predicates, error):
if error:
with pytest.raises(error):
assert Settings(search_predicates=search_predicates)
else:
assert Settings(search_predicates=search_predicates)


@pytest.mark.parametrize(
"other_predicates, error",
[
[["https://schema.org/other"], None],
[["1", "2", "3"], None],
[[1], TypeError],
["not a list", ValueError],
],
)
def test_other_predicates(other_predicates, error):
if error:
with pytest.raises(error):
assert Settings(other_predicates=other_predicates)
else:
assert Settings(other_predicates=other_predicates)

0 comments on commit a4c1728

Please sign in to comment.