mirror of
https://github.com/adulau/pdns-qof.git
synced 2024-11-24 19:17:13 +00:00
Add python code to test the JSON schema. Works.
Added testdata/
This commit is contained in:
parent
ba0d3b01cd
commit
65a1317cd1
4 changed files with 46 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
*.swp
|
||||
|
|
@ -3,4 +3,9 @@
|
|||
This little package can parse the Passive DNS Common Output Format (COF) and validate it.
|
||||
It is given as example code.
|
||||
|
||||
* cofparser.py ... a manually written parser for the COF format
|
||||
* cofparser_jsonschema.py .... one which uses the JSON schema to validate.
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
37
example_code/python/cofparser_jsonschema.py
Normal file
37
example_code/python/cofparser_jsonschema.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
import sys
|
||||
import json
|
||||
import jsonschema
|
||||
from jsonschema import validate
|
||||
|
||||
|
||||
def get_schema(filename):
|
||||
"""This function loads the given schema available"""
|
||||
with open(filename, 'r') as file:
|
||||
schema = json.load(file)
|
||||
return schema
|
||||
|
||||
|
||||
def validate_json(json_data, schema=None):
|
||||
"""REF: https://json-schema.org/ """
|
||||
|
||||
try:
|
||||
validate(instance=json_data, schema=schema)
|
||||
except jsonschema.exceptions.ValidationError as err:
|
||||
print(err)
|
||||
err = "Given JSON data is InValid"
|
||||
return False, err
|
||||
|
||||
message = "Given JSON data is Valid"
|
||||
return True, message
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
schema = get_schema("schema/schema.json")
|
||||
|
||||
# Convert json to python object.
|
||||
with open(sys.argv[1], 'r') as ndjson_file:
|
||||
for line in ndjson_file:
|
||||
jsonData = json.loads(line)
|
||||
# validate it
|
||||
is_valid, msg = validate_json(jsonData, schema=schema)
|
||||
print(msg)
|
2
example_code/testdata/data.json
vendored
Normal file
2
example_code/testdata/data.json
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
{"count":1909,"rdata":["cpa.circl.lu"],"rrname":"www.circl.lu","rrtype":"CNAME","time_first":1315586409,"time_last":1449566799}
|
||||
{"count":2560,"rdata":["cpab.circl.lu"],"rrname":"www.circl.lu","rrtype":"CNAME","time_first":1449584660,"time_last":1617676151}
|
Loading…
Reference in a new issue