You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/custom_validators.md
+103-7Lines changed: 103 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,7 @@
3
3
With custom validators, you can implement business logic in Python. Schema-enforcer will automatically
4
4
load your plugins from the `validator_directory` and run them against your host data.
5
5
6
-
The validator plugin provides two base classes: ModelValidation and JmesPathModelValidation. The former can be used
7
-
when you want to implement all logic and the latter can be used as a shortcut for jmespath validation.
6
+
The validator plugin provides a few base classes: BaseValidation, JmesPathModelValidation, and PydanticValidation. BaseValidation can be used when you want to implement all logic, JmesPathModelValidation can be used as a shortcut for jmespath validation, and PydanticValidation will validate data against a specific Pydantic model.
8
7
9
8
## BaseValidation
10
9
@@ -26,7 +25,7 @@ by providing a class-level `id` variable.
26
25
27
26
Helper functions are provided to add pass/fail results:
@@ -90,7 +91,8 @@ class CheckInterface(JmesPathModelValidation): # pylint: disable=too-few-public
90
91
```
91
92
92
93
#### With compiled jmespath expression
93
-
```
94
+
95
+
```python
94
96
import jmespath
95
97
from schema_enforcer.schemas.validator import JmesPathModelValidation
96
98
@@ -104,6 +106,100 @@ class CheckInterfaceIPv4(JmesPathModelValidation): # pylint: disable=too-few-pu
104
106
error ="All core interfaces do not have IPv4 addresses"
105
107
```
106
108
109
+
## PydanticValidation
110
+
111
+
Schema Enforcer supports utilizing Pydantic models for validation. Pydantic models can be loaded two different ways.
112
+
113
+
1. Store your models in your `validator_directory`.
114
+
2. Load from a separate library using the `schema_enforcer.schemas.PydanticManager`. These must be defined within the `schema_enforcer` configuration file.
115
+
1.`pydantic_validators` requires a list of library paths to a `PydanticManager` instance.
116
+
117
+
Both methods will replace the Pydantic `BaseModel` with the `PydanticValidation` class that provides the required `validate` method that uses the `model_validate` method to validate data. The model is set to the original Pydantic model to validate data against.
118
+
119
+
### Pydantic Models in External Libraries
120
+
121
+
```python
122
+
classPydanticValidation(BaseValidation):
123
+
"""Basic wrapper for Pydantic models to be used as validators."""
data (dict): variables to be validated by validator
132
+
strict (bool): true when --strict cli option is used to request strict validation (if provided)
133
+
134
+
Returns:
135
+
None
136
+
137
+
Use add_validation_error and add_validation_pass to report results.
138
+
"""
139
+
try:
140
+
self.model.model_validate(data, strict=strict)
141
+
self.add_validation_pass()
142
+
except ValidationError as err:
143
+
self.add_validation_error(str(err))
144
+
```
145
+
146
+
### Pydantic Models in Validators Directory
147
+
148
+
The Pydantic models can be located in any Python file within this directory (new or existing). The only requirement is these are valid Pydantic `BaseModel` subclasses.
149
+
150
+
These will be loaded and can be referenced by their class name. For example, `CheckHostname` will show up as `CheckHostname`.
151
+
152
+
```python
153
+
"""Validate hostname is valid."""
154
+
from pydantic import BaseModel
155
+
156
+
157
+
classCheckHostname(BaseModel):
158
+
"""Validate hostname is valid."""
159
+
160
+
hostname: str
161
+
```
162
+
163
+
```yaml
164
+
# jsonschema: Hostname
165
+
---
166
+
hostname: "az-phx-rtr01"
167
+
```
168
+
169
+
### Load from External Library
170
+
171
+
As an example, we will look at models that are within our `my_custom_pydantic_models.manager`. If a **prefix** is defined, you can reference the validators like `f"{prefix}/{model.__name__}`.
172
+
173
+
```python
174
+
"""Load our models to be used for Schema Enforcer."""
175
+
from pydantic import BaseModel
176
+
from schema_enforcer.schemas.manager import PydanticManager
0 commit comments