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

refactor: tidy schema valdation #120

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
6 changes: 2 additions & 4 deletions blurry/file_processors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ def write_html_file(
extra_context["sibling_pages"] = sibling_pages
folder_in_build = convert_content_path_to_directory_in_build(filepath)

file_data = [
f for f in file_data_by_directory[filepath.parent] if f.path == filepath
][0]
file_data = [f for f in file_data_list if f.path == filepath][0]
schema_type = file_data.front_matter.get("@type")
if not schema_type:
raise ValueError(
Expand Down Expand Up @@ -135,7 +133,7 @@ def write_html_file(
default=json_converter_with_dates,
)

validate_front_matter_as_schema(filepath, front_matter, warning_console)
validate_front_matter_as_schema(filepath, schema_variables, warning_console)

schema_type_tag = f'<script type="application/ld+json">{schema_data}</script>'

Expand Down
23 changes: 5 additions & 18 deletions blurry/schema_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@
from rich.console import Console

from blurry.settings import SETTINGS
from blurry.settings import update_settings


class Config:
extra = "forbid"


def validate_front_matter_as_schema(
path: Path, front_matter: MutableMapping, console: Console
path: Path, schema_variables: MutableMapping, console: Console
):
"""
Validates schema data using pydantic_schemaorg, disallowing extra fields
"""
update_settings()

schema_type = front_matter["@type"]
schema_type = schema_variables["@type"]

if mapped_schema_type := SETTINGS["TEMPLATE_SCHEMA_TYPES"].get(schema_type):
schema_type = mapped_schema_type
Expand All @@ -37,21 +30,15 @@ def validate_front_matter_as_schema(
)
return

schema_model = getattr(pydantic_schemaorg_model_module, schema_type)
SchemaModel = getattr(pydantic_schemaorg_model_module, schema_type)

# Create new Pydantic model that forbids extra fields
class NonExtraSchemaModel(schema_model, extra="forbid"): # type: ignore
class SchemaModelWithoutExtraFields(SchemaModel, extra="forbid"): # type: ignore
pass

# Validate model and print errors
try:
non_schema_variable_prefix = SETTINGS["FRONTMATTER_NON_SCHEMA_VARIABLE_PREFIX"]
schema_front_matter = {
k: v
for k, v in front_matter.items()
if not k.startswith(non_schema_variable_prefix)
}
NonExtraSchemaModel(**schema_front_matter)
SchemaModelWithoutExtraFields(**schema_variables)
except ValidationError as e:
for error in e.errors():
msg = error["msg"]
Expand Down
28 changes: 4 additions & 24 deletions tests/test_schema_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,13 @@ def test_validate_front_matter_as_schema_with_valid_front_matter():


def test_validate_front_matter_as_schema_with_extra_value():
_, front_matter = get_data(MARKDOWN_WITH_EXTRA_VALUE_IN_TOML_FRONT_MATTER)
_, front_matter_with_extra_value = get_data(
MARKDOWN_WITH_EXTRA_VALUE_IN_TOML_FRONT_MATTER
)
path = Path("pages/intro.md")
test_console = Console()
test_console.print = MagicMock()
validate_front_matter_as_schema(path, front_matter, test_console)
validate_front_matter_as_schema(path, front_matter_with_extra_value, test_console)
test_console.print.assert_called_with(
"pages/intro.md: WebPage schema validation error: extra fields not permitted: ('extra_value',)"
)


MARKDOWN_WITH_NON_SCHEMA_VALUE_IN_TOML_FRONT_MATTER = """
+++
"@type" = "WebPage"
name = "Introduction"
abstract = "A Python-powered static site generator with a focus on page speed and SEO."
datePublished = 2023-04-09
_valid_extra_value = true
+++

# Blurry: A Python-powered static site generator
""".strip()


def test_validate_front_matter_as_schema_with_non_schema_value():
_, front_matter = get_data(MARKDOWN_WITH_NON_SCHEMA_VALUE_IN_TOML_FRONT_MATTER)
path = Path("pages/intro.md")
test_console = Console()
test_console.print = MagicMock()
validate_front_matter_as_schema(path, front_matter, test_console)
assert not test_console.print.called
Loading