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

Forbid extra fields in Pydantic models #43

Merged
merged 5 commits into from
Feb 21, 2024
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pydantic import ConfigDict, BaseModel
from pydantic import BaseModel, ConfigDict


class DatadocBaseModel(BaseModel):
"""Defines configuration which applies to all Models in this package."""

model_config = ConfigDict(validate_assignment=True, use_enum_values=True)
model_config = ConfigDict(
validate_assignment=True, use_enum_values=True, extra="forbid"
)
2 changes: 1 addition & 1 deletion generated/python/datadoc_model/datadoc_model/model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# generated by datamodel-codegen:
# filename: metadata-container-json-schema.json
# timestamp: 2024-02-21T08:51:44+00:00
# timestamp: 2024-02-21T09:13:37+00:00

from __future__ import annotations

Expand Down
2 changes: 1 addition & 1 deletion generated/python/datadoc_model/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ssb-datadoc-model"
version = "4.2.0"
version = "4.3.0"
description = "Data Model for use in Statistics Norway's Metadata system"
authors = ["Statistics Norway <[email protected]>"]
license = "MIT"
Expand Down
7 changes: 7 additions & 0 deletions generated/python/datadoc_model/tests/test_datadoc_model.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pytest
from datadoc_model.model import (
DatadocMetadata,
MetadataContainer,
PseudonymizationMetadata,
)
from pydantic import ValidationError


def test_instantiate_no_values_container():
Expand All @@ -18,3 +20,8 @@ def test_instantiate_no_values_datadoc():
def test_instantiate_no_values_pseudo():
pseudo = PseudonymizationMetadata()
assert isinstance(pseudo, PseudonymizationMetadata)


def test_unknown_field():
with pytest.raises(ValidationError):
DatadocMetadata(unknown_field="random_value")
Loading