-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from dnstapir/schema_validation
Schema validation
- Loading branch information
Showing
8 changed files
with
461 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema#", | ||
"type": "object", | ||
"additionalProperties": true, | ||
"required": [ | ||
"type", | ||
"version", | ||
"timestamp", | ||
"qname" | ||
], | ||
"properties": { | ||
"version": { | ||
"type": "integer", | ||
"minimum": 0 | ||
}, | ||
"timestamp": { | ||
"type": "string", | ||
"format": "date-time" | ||
}, | ||
"type": { | ||
"const": "new_qname" | ||
}, | ||
"initiator": { | ||
"type": "string", | ||
"enum": [ | ||
"client", | ||
"resolver" | ||
] | ||
}, | ||
"qname": { | ||
"description": "Query Name", | ||
"$ref": "#/$defs/domain_name" | ||
}, | ||
"qtype": { | ||
"description": "Query Type", | ||
"type": "integer", | ||
"minimum": 0 | ||
}, | ||
"qclass": { | ||
"description": "Query Class", | ||
"type": "integer", | ||
"minimum": 0 | ||
}, | ||
"flags": { | ||
"description": "Flag Field (QR/Opcode/AA/TC/RD/TA/Z/RCODE)", | ||
"type": "integer" | ||
}, | ||
"rdlength": { | ||
"type": "integer", | ||
"minimum": 0 | ||
} | ||
}, | ||
"$defs": { | ||
"domain_name": { | ||
"type": "string" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
import logging | ||
import os | ||
from pathlib import Path | ||
|
||
import jsonschema | ||
|
||
SCHEMA_DIR = os.path.dirname(__file__) + "/schema" | ||
|
||
|
||
class MessageValidator: | ||
"""MQTT Message Validator""" | ||
|
||
VALIDATOR = jsonschema.Draft202012Validator | ||
|
||
def __init__(self) -> None: | ||
self.logger = logging.getLogger(__name__).getChild(self.__class__.__name__) | ||
self.schemas: dict[str, jsonschema.Validator] = {} | ||
for filename in Path(SCHEMA_DIR).glob("*.json"): | ||
with open(filename) as fp: | ||
schema = json.load(fp) | ||
name = filename.name.removesuffix(".json") | ||
self.schemas[name] = self.VALIDATOR( | ||
schema, format_checker=self.VALIDATOR.FORMAT_CHECKER | ||
) | ||
self.logger.debug("Loaded schema %s from %s", name, filename) | ||
|
||
def validate_message(self, topic: str, payload: bytes) -> None: | ||
"""Validate message against schema based on content type""" | ||
content = json.loads(payload) | ||
content_type = content["type"] | ||
if schema := self.schemas.get(content_type): | ||
schema.validate(content) | ||
self.logger.debug( | ||
"Message on %s validated against schema %s", topic, content_type | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.