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
56 changes: 53 additions & 3 deletions pyavd_utils/validation.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
from pathlib import Path
from typing import Literal

class Configuration:
"""Configuration for validation behavior."""

ignore_required_keys_on_root_dict: bool
"""Ignore required keys on the root dictionary."""

return_coercion_infos: bool
"""Return coercion information in the validation result."""

restrict_null_values: bool
"""Emit type errors for Null values instead of ignoring them."""

warn_eos_cli_config_gen_keys: bool
"""
When validating eos_designs, emit warnings for top-level keys that exist in eos_cli_config_gen but not in eos_designs.
"""

def __init__(
self,
*,
ignore_required_keys_on_root_dict: bool = False,
return_coercion_infos: bool = False,
restrict_null_values: bool = False,
warn_eos_cli_config_gen_keys: bool = False,
) -> None: ...

class Violation:
"""Input data violates the schema."""

Expand All @@ -30,11 +56,20 @@ class Deprecation:
url: str | None
"""Url where more information can be found."""

class IgnoredEosConfigKey:
"""EOS Config key found in AVD Design input."""

message: str
"""String detailing the ignored key."""
path: list[str]
"""Path to the ignored key."""

class ValidationResult:
"""Result of data validation."""

violations: list[Violation]
deprecations: list[Deprecation]
ignored_eos_config_keys: list[IgnoredEosConfigKey]

class ValidatedDataResult:
"""Result of data validation including the validated data as JSON."""
Expand All @@ -56,19 +91,28 @@ def init_store_from_file(file: Path) -> None:
RuntimeError: For any issue hit during loading, deserializing, combining and resolving schemas.
"""

def validate_json(data_as_json: str, schema_name: Literal["eos_cli_config_gen", "eos_designs"]) -> ValidationResult:
def validate_json(
data_as_json: str,
schema_name: Literal["eos_cli_config_gen", "eos_designs"],
configuration: Configuration | None = None,
) -> ValidationResult:
"""
Validate data against a schema specified by name.

Args:
data_as_json: Structured data dumped as JSON.
schema_name: The name of the schema to validate against.
configuration: Optional configuration for validation behavior.

Returns:
ValidationResult holding lists of violations and deprecations.
"""

def get_validated_data(data_as_json: str, schema_name: Literal["eos_cli_config_gen", "eos_designs"]) -> ValidatedDataResult:
def get_validated_data(
data_as_json: str,
schema_name: Literal["eos_cli_config_gen", "eos_designs"],
configuration: Configuration | None = None,
) -> ValidatedDataResult:
"""
Validate data against a schema specified by name and return the data after coercion and validation.

Expand All @@ -77,18 +121,24 @@ def get_validated_data(data_as_json: str, schema_name: Literal["eos_cli_config_g
Args:
data_as_json: Structured data dumped as JSON.
schema_name: The name of the schema to validate against.
configuration: Optional configuration for validation behavior.

Returns:
ValidatedDataResult holding the validated data and the ValidationResult with lists of violations and deprecations.
"""

def validate_json_with_adhoc_schema(data_as_json: str, schema_as_json: str) -> ValidationResult:
def validate_json_with_adhoc_schema(
data_as_json: str,
schema_as_json: str,
configuration: Configuration | None = None,
) -> ValidationResult:
"""
Validate data against the given schema.

Args:
data_as_json: Structured data dumped as JSON.
schema_as_json: A fully resolved schema dumped as JSON.
configuration: Optional configuration for validation behavior.

Returns:
ValidationResult holding lists of violations and deprecations.
Expand Down
15 changes: 11 additions & 4 deletions rust/avdschema/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,15 @@ pub struct SchemaName {
#[cfg(test)]
mod tests {

use super::Load;

use crate::utils::test_utils::{get_avd_store, get_tmp_file};
use crate::{Dump as _, Store};
#[cfg(feature = "dump_load_files")]
use {
super::Load,
crate::utils::test_utils::{get_avd_store, get_tmp_file},
crate::{Dump as _, Store},
};

#[test]
#[cfg(feature = "dump_load_files")]
fn dump_avd_store() {
// Dumping uncompressed and compressed schema.
let store = get_avd_store();
Expand All @@ -132,6 +135,7 @@ mod tests {
}

#[test]
#[cfg(feature = "dump_load_files")]
fn load_avd_store() {
dump_avd_store();
let store = get_avd_store();
Expand All @@ -157,6 +161,7 @@ mod tests {
}

#[test]
#[cfg(feature = "dump_load_files")]
#[ignore = "Test only used for manual performance testing"]
fn quick_load_avd_store_json() {
//Depends on dump to be done before. This is just here to test the speed of loading from the file.
Expand All @@ -166,6 +171,7 @@ mod tests {
}

#[test]
#[cfg(feature = "dump_load_files")]
#[ignore = "Test only used for manual performance testing"]
fn quick_load_avd_store_gz() {
//Depends on dump to be done before. This is just here to test the speed of loading from the file.
Expand All @@ -175,6 +181,7 @@ mod tests {
}

#[test]
#[cfg(feature = "dump_load_files")]
#[ignore = "Test only used for manual performance testing"]
fn quick_load_avd_store_xz2() {
//Depends on dump to be done before. This is just here to test the speed of loading from the file.
Expand Down
2 changes: 1 addition & 1 deletion rust/pyvalidation/benches/performance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn benchmark_init_store_from_file(c: &mut Criterion) {
pub fn benchmark_get_validated_data(c: &mut Criterion) {
c.bench_function("get_validated_data", |b| {
pyo3::Python::initialize();
pyo3::Python::attach(|py| b.iter(|| get_validated_data(py, TEST_DATA, "eos_designs")))
pyo3::Python::attach(|py| b.iter(|| get_validated_data(py, TEST_DATA, "eos_designs", None)))
});
}

Expand Down
Loading