Skip to content

Instantly share code, notes, and snippets.

@bk2zsto
Created April 29, 2023 21:11
Show Gist options
  • Save bk2zsto/f9df69ed86a5b0558b103bfaab3a63d1 to your computer and use it in GitHub Desktop.
Save bk2zsto/f9df69ed86a5b0558b103bfaab3a63d1 to your computer and use it in GitHub Desktop.
supporting files for validate module not loading references
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"cidr": {
"$comment": "CIDR, IPv4-only",
"type": "string",
"pattern": "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}/[0-9]{1,2}"
}
},
"type": "object",
"properties": {
"network": {
"$ref": "#/$defs/cidr"
}
}
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "file:///tmp/schemas/common.json",
"validators": {
"cidr": {
"$comment": "CIDR, IPv4-only",
"type": "string",
"pattern": "^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}/[0-9]{1,2}"
}
}
}
#!/usr/bin/env python
import json
from jsonschema import validate
cidr = "192.0.2.0/24"
not_cidr = "192.0.2.0"
combined_schema = json.load(open("/tmp/schemas/combined.json"))
referring_schema = json.load(open("/tmp/schemas/referring.json"))
for k,v in { "combined": combined_schema, "referring": referring_schema }.items():
for net in [cidr, not_cidr]:
try:
validate({"network": net}, v)
except:
print(f"Validation of {net} failed with {k}")
---
- hosts: localhost
gather_facts: false
tasks:
- name: setup test_data
set_fact:
test_data:
- name: cidr
network: 192.0.2.0/24
- name: not_cidr
network: 192.0.2.0
- name: validate with combined schema
ansible.utils.validate:
data: "{{ item | to_json }}"
criteria: "{{ lookup('file','/tmp/schemas/combined.json') | from_json }}"
engine: ansible.utils.jsonschema
loop: "{{ test_data }}"
loop_control:
label: "{{ item.name }}"
ignore_errors: true
- name: validate with referring schema
ansible.utils.validate:
data: "{{ item | to_json }}"
criteria: "{{ lookup('file','/tmp/schemas/referring.json') | from_json }}"
engine: ansible.utils.jsonschema
loop: "{{ test_data }}"
loop_control:
label: "{{ item.name }}"
ignore_errors: true
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "file:///tmp/schemas/referring.json",
"$ref": "file:///tmp/schemas/common.json",
"type": "object",
"properties": {
"network": {
"$ref": "common.json#validators/cidr"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment