Skip to content
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
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ jobs:
run: hatch publish -y
env:
HATCH_INDEX_AUTH: ${{ secrets.HATCH_INDEX_AUTH }}
HATCH_INDEX_USER: ${{ secrets.HATCH_INDEX_USER }}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dependencies = ["mypy>=1.8.0", "pydocstyle>=6.3.0", "ruff>=0.2.2"]
features = ["cloud", "file-formats", "validation"]

[tool.hatch.envs.lint.scripts]
lint = "ruff src"
lint = "ruff check src"
typing = "mypy src"
docs = "pydocstyle src"

Expand Down
3 changes: 2 additions & 1 deletion src/config/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def validate(
self,
schema: Any,
raise_on_error: bool = False,
nested: bool = False,
**kwargs: Mapping[str, Any],
) -> bool:
"""Validate the current config using JSONSchema."""
Expand All @@ -412,7 +413,7 @@ def validate(
"Validation requires the `jsonschema` library.",
) from None
try:
validate(self.as_dict(), schema, **kwargs)
validate(self.as_attrdict() if nested else self.as_dict(), schema, **kwargs)
except ValidationError as err:
if raise_on_error:
raise err
Expand Down
47 changes: 39 additions & 8 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Validation tests."""

# ruff: noqa: D103

import pytest
from config import (
Configuration,
ConfigurationSet,
EnvConfiguration,
config,
config_from_dict,
)
import pytest

try:
import jsonschema
Expand All @@ -25,7 +25,7 @@ def test_validation_ok(): # type: ignore
"type": "array",
"items": {"enum": [1, 2, 3]},
"maxItems": 2,
}
},
},
}

Expand All @@ -43,7 +43,7 @@ def test_validation_fail(): # type: ignore
"type": "array",
"items": {"enum": [1, 2, 3]},
"maxItems": 2,
}
},
},
}

Expand Down Expand Up @@ -89,5 +89,36 @@ def test_validation_format(): # type: ignore
raise_on_error=True,
format_checker=Draft202012Validator.FORMAT_CHECKER,
)
print(str(err))
assert "'10' is not a 'ipv4'" in str(err)


@pytest.mark.skipif("jsonschema is None")
def test_validation_nested(): # type: ignore
d = {"item": {"sub1": 1, "sub2": "abc"}}
cfg = config_from_dict(d)

schema = {
"type": "object",
"properties": {
"item.sub1": {"type": "number"},
"item.sub2": {"type": "string"},
},
"required": ["item.sub1", "item.sub2"],
}
assert cfg.validate(schema)

schema = {
"type": "object",
"properties": {
"item": {
"type": "object",
"properties": {
"sub1": {"type": "number"},
"sub2": {"type": "string"},
},
"required": ["sub1", "sub2"],
},
},
"required": ["item"],
}
assert cfg.validate(schema, nested=True)