Skip to content

Commit

Permalink
Forbid extra fields in Pydantic models (#43)
Browse files Browse the repository at this point in the history
* Forbid extra fields

* Add a test for unknown field

* Release v4.3.0

* [GENERATE] Pydantic models from JSON Schema

---------

Co-authored-by: dapla-bot[bot] <143391972+dapla-bot[bot]@users.noreply.github.com>
  • Loading branch information
mmwinther and dapla-bot[bot] authored Feb 21, 2024
1 parent 15f70cd commit 61f25c2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
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")

0 comments on commit 61f25c2

Please sign in to comment.