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: README.md
+46-9Lines changed: 46 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Schema Enforcer
2
2
3
-
Schema Enforcer provides a framework for testing structured data against schema definitions. Right now, [JSONSchema](https://json-schema.org/understanding-json-schema/index.html) is the only schema definition language supported, but we intend to add support for YANG and other schema definition languages at some point in the future.
3
+
Schema Enforcer provides a framework for testing structured data against schema definitions. Right now, [JSONSchema](https://json-schema.org/understanding-json-schema/index.html) is the only schema definition language supported, but we are thinking about adding other schema definition languages at some point in the future.
4
4
5
5
## Getting Started
6
6
@@ -67,7 +67,7 @@ dns_servers:
67
67
- address: "10.1.1.1"
68
68
- address: "10.2.2.2"
69
69
```
70
-
> Note: The line `# jsonschema: schemas/dns_servers` tells `schema-enforcer` the ID of the schema which the structured data defined in the file should be validated against. More information on how the structured data is mapped to a schema ID to which it should adhere can be found in the [docs/mapping_schemas.md README](./docs/mapping_schemas.md)
70
+
> Note: The line `# jsonschema: schemas/dns_servers` tells `schema-enforcer` the ID of the schema which the structured data defined in the file should be validated against. The schema ID is defined by the `$id` top level key in a schema definition. More information on how the structured data is mapped to a schema ID to which it should adhere can be found in the [mapping_schemas README](./docs/mapping_schemas.md)
71
71
72
72
The file `schema/schemas/dns.yml` is a schema definition file. It contains a schema definition for ntp servers written in JSONSchema. The data in `chi-beijing-rt1/dns.yml` and `eng-london-rt1/dns.yml` should adhere to the schema defined in this schema definition file.
73
73
@@ -104,7 +104,7 @@ required:
104
104
105
105
Once schema-enforcer has been installed, the `schema-enforcer validate` command can be used run schema validations of YAML/JSON instance files against the defined schema.
To run the schema validations, the command `schema-enforcer validate` can be run.
121
121
122
-
```cli
122
+
```shell
123
123
bash$ schema-enforcer validate
124
124
schema-enforcer validate
125
125
ALL SCHEMA VALIDATION CHECKS PASSED
126
126
```
127
127
128
128
To acquire more context regarding what files specifically passed schema validation, the `--show-pass` flag can be passed in.
129
129
130
-
```cli
130
+
```shell
131
131
bash$ schema-enforcer validate --show-pass
132
132
PASS [FILE] ./eng-london-rt1/ntp.yml
133
133
PASS [FILE] ./eng-london-rt1/dns.yml
@@ -138,13 +138,15 @@ ALL SCHEMA VALIDATION CHECKS PASSED
138
138
139
139
If we modify one of the addresses in the `chi-beijing-rt1/dns.yml` file so that it's value is the boolean `true` instead of an IP address string, then run the `schema-enforcer` tool, the validation will fail with an error message.
140
140
141
-
```cli
141
+
```yaml
142
142
bash$ cat chi-beijing-rt1/dns.yml
143
143
# jsonschema: schemas/dns_servers
144
144
---
145
145
dns_servers:
146
146
- address: true
147
147
- address: "10.2.2.2"
148
+
```
149
+
```shell
148
150
bash$ test-schema validate
149
151
FAIL | [ERROR] True is not of type 'string' [FILE] ./chi-beijing-rt1/dns.yml [PROPERTY] dns_servers:0:address
150
152
bash$ echo $?
@@ -153,11 +155,46 @@ bash$ echo $?
153
155
154
156
When a structured data file fails schema validation, `schema-enforcer` exits with a code of 1.
155
157
158
+
### Configuration Settings
159
+
160
+
Schema enforcer will work with default settings, however, a `pyproject.toml` file can be placed at the root of the path in which `schema-enforcer` is run in order to override default settings or declare configuration for more advanced features. Inside of this `pyproject.toml` file, `tool.schema_enfocer` sections can be used to declare settings for schema enforcer. Take for example the `pyproject.toml` file in example 2.
161
+
162
+
```shell
163
+
bash$ cd examples/example2 && tree -L 2
164
+
.
165
+
├── README.md
166
+
├── hostvars
167
+
│ ├── chi-beijing-rt1
168
+
│ ├── eng-london-rt1
169
+
│ └── ger-berlin-rt1
170
+
├── invalid
171
+
├── pyproject.toml
172
+
└── schema
173
+
├── definitions
174
+
└── schemas
175
+
176
+
8 directories, 2 files
177
+
```
178
+
179
+
In this toml file, a schema mapping is declared which tells schema enforcer which structured data files should be checked by which schema IDs.
180
+
181
+
182
+
```shell
183
+
bash$ cat pyproject.toml
184
+
[tool.schema_enforcer.schema_mapping]
185
+
# Map structured data filename to schema IDs
186
+
'dns_v1.yml' = ['schemas/dns_servers']
187
+
'dns_v2.yml' = ['schemas/dns_servers_v2']
188
+
'syslog.yml' = ['schemas/syslog_servers']
189
+
```
190
+
191
+
> More information on available configuration settings can be found in the [configuration README](docs/configuration.md)
156
192
### Where To Go Next
157
193
158
-
More detailed documentation can be found inside of README.md files inside of the `docs/` directory.
194
+
Detailed documentation can be found in the README.md files inside of the `docs/` directory.
195
+
- ["Introducing Schema Enforcer" blog post](https://blog.networktocode.com/post/introducing_schema_enforcer/)
159
196
- [Using a pyproject.toml file for configuration](docs/configuration.md)
160
-
- [Mapping Structured Data Files to Schema Files](docs/mapping_schemas.md)
0 commit comments