-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start of JSON schema tool kit #6
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
abba189
moved schema files from DAS
Geary-Layne 616982e
added moved files
Geary-Layne 6fc0b4e
removed old schema and code
Geary-Layne a277013
moved test code to test_validate_schema.py
Geary-Layne b69ea8e
fixed recursive ref lookup
Geary-Layne f2f49e1
fixed recursive ref lookup
Geary-Layne d8988e8
improved msessage schemas
Geary-Layne c4900b0
updated to iso for datetime
Geary-Layne 1220669
added json-schema doc
Geary-Layne d90669c
Update json-schema.rst
Geary-Layne 4d0c420
extended to support das data request
Geary-Layne 97ee890
fix test
Geary-Layne 257686a
Merge branch 'main' into chore/idsse-219/mv-schema
Geary-Layne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
=========== | ||
JSON Schema | ||
=========== | ||
|
||
Goals: | ||
====== | ||
Document and Validate all JSON messages used to communicate between microservices within IDSS Engine. | ||
|
||
Additional Goal: | ||
---------------- | ||
The different message types should share as much structure in common as is practical. This consistency | ||
should increase human readability, as developers become familiar with standard, and reduce complexity | ||
for message handling. | ||
|
||
Resources: | ||
========== | ||
- `JSON schema <https://json-schema.org/>`_ | ||
- `Understanding JSON schema <https://json-schema.org/understanding-json-schema/>`_ | ||
|
||
Convention/Patterns: | ||
==================== | ||
- Auxiliary or Subschema are used to group related objects. Subschema objects are defined at the root | ||
level in json files. The filename is based on the dominant object. | ||
|
||
**Timing.json**:: | ||
|
||
{ | ||
"TimeString": { | ||
"description": "String representation of a date/time", | ||
"type": "string", | ||
"format": "date-time" | ||
}, | ||
"TimeList": { | ||
"description": "A list of specific time_string(s)", | ||
"type": "array", | ||
"items": {"$ref": "#/TimeString"}, | ||
"minItems": 1 | ||
}, | ||
"TimeRange": { | ||
"description": "Date/time string specifying the start and end date/ times", | ||
"type": "object", | ||
"properties": { | ||
"start": {"$ref": "#/TimeString"}, | ||
"end": {"$ref": "#/TimeString"} | ||
}, | ||
"required": [ | ||
"start", | ||
"end" | ||
] | ||
}, | ||
"Timing": { | ||
"description": "Either a TimeString, TimeList, or TimeRange", | ||
"oneOf": [ | ||
{"$ref": "#/TimeString"}, | ||
{"$ref": "#/TimeList"}, | ||
{"$ref": "#/TimeRange"} | ||
] | ||
} | ||
} | ||
|
||
|
||
|
||
- Schemas that define a message should, for the most part, be entirely built from subschema. The | ||
filename should represent the message type and end with _schema in order to distinguish from | ||
subschema. | ||
|
||
.. note:: *These examples are not complete, thus will need to be updated* | ||
|
||
|
||
**New_data_schema.json** | ||
|
||
*(This currently is only a partial schema, new data service publishes other information)* | ||
|
||
One of the messages that the new_data service publishes indicates when a field from a product source | ||
is available for a specific issue/valid datetime. Basically this is a message specifying the Variable | ||
subschema, except that a Variable does NOT require a Field *(it is optional)*. Thus the new_data message | ||
utilized the Variable subschema and adds the additional requirement that there must be at least one | ||
field.:: | ||
|
||
{ | ||
"type": "object", | ||
"allOf": [ | ||
{"$ref": "variable.json#/Variable"}, | ||
{"properties": {"field": {"minItems": 1}}} | ||
] | ||
} | ||
|
||
|
||
**Criteria_schema.json** | ||
|
||
*(This is incomplete, there is more in a criteria message, but is a | ||
good example of how a message is built from subschemas)*:: | ||
|
||
{ | ||
"type": "object", | ||
"properties": { | ||
"corrId": {"$ref": "corr_id.json#/CorrId"}, | ||
"issueDt": {"$ref": "timing.json#/Timing"}, | ||
"tags": {"$ref": "tags.json#/Tags"}, | ||
"validDt": {"$ref": "timing.json#/Timing"}, | ||
"location": {"$ref": "location.json#/Location"} | ||
}, | ||
"required": [ | ||
"corrId", | ||
"issueDt", | ||
"tags", | ||
"validDt", | ||
"location" | ||
] | ||
} |
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,72 @@ | ||
"""Class for validating IDSSe JSON messages against schema""" | ||
# ---------------------------------------------------------------------------------- | ||
# Created on Mon Aug 07 2023 | ||
# | ||
# Copyright (c) 2023 Regents of the University of Colorado. All rights reserved. (1) | ||
# | ||
# Contributors: | ||
# Geary Layne (1) | ||
# | ||
# ---------------------------------------------------------------------------------- | ||
import json | ||
import os | ||
from typing import Optional, Union | ||
|
||
from jsonschema import Validator, FormatChecker, RefResolver | ||
from jsonschema.validators import validator_for | ||
|
||
|
||
def _get_refs(json_obj: Union[dict, list], result: Optional[set] = None) -> set: | ||
if result is None: | ||
result = set() | ||
if isinstance(json_obj, dict): | ||
for key, value in json_obj.items(): | ||
print(key, ':', value, 'type', type(value)) | ||
if key == '$ref': | ||
idx = value.index('#/') | ||
if idx > 0: | ||
result.add(value[:idx]) | ||
else: | ||
_get_refs(value, result) | ||
elif isinstance(json_obj, list): | ||
print('\tas list') | ||
for item in json_obj: | ||
_get_refs(item, result) | ||
return result | ||
|
||
|
||
def get_validator(schema_name) -> Validator: | ||
"""Get a jsonschema Validator to be used when evaluating json messages against specified schema | ||
|
||
Args: | ||
schema_name (str): The name of the message schema, | ||
must exist under idss-engine-common/schema | ||
|
||
Returns: | ||
Validator: A validator loaded with schema and all dependencies | ||
""" | ||
current_path = os.path.dirname(os.path.realpath(__file__)) | ||
schema_dir = os.path.join(os.path.sep, *(current_path.split(os.path.sep)[:-4]), 'schema') | ||
schema_filename = os.path.join(schema_dir, schema_name+'.json') | ||
with open(schema_filename, 'r', encoding='utf8') as file: | ||
schema = json.load(file) | ||
|
||
base = json.loads('{"$schema": "http://json-schema.org/draft-07/schema#"}') | ||
|
||
dependencies = {base.get('$id'): base} | ||
refs = _get_refs(schema) | ||
while len(refs): | ||
new_refs = set() | ||
for ref in refs: | ||
schema_filename = os.path.join(schema_dir, ref) | ||
with open(schema_filename, 'r', encoding='utf8') as file: | ||
ref_schema = json.load(file) | ||
dependencies[ref_schema.get('$id', ref)] = ref_schema | ||
new_refs = _get_refs(ref_schema, new_refs) | ||
refs = {ref for ref in new_refs if ref not in dependencies} | ||
|
||
print(dependencies) | ||
resolver = RefResolver.from_schema(schema=base, | ||
store=dependencies) | ||
|
||
return validator_for(base)(schema, resolver=resolver, format_checker=FormatChecker()) |
Binary file modified
BIN
+5.02 KB
(130%)
python/idsse_common/test/__pycache__/test_config.cpython-311-pytest-7.3.1.pyc
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
python/idsse_common/test/__pycache__/test_utils.cpython-311-pytest-7.3.1.pyc
Binary file not shown.
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad we have a common method now to create timestamps, it's something that is done a lot in different ways in many places. We will have update some of our services (EPM, ECM, IMS Gateway) to include the commons package as they currently do not