Hi everyone!
I'm having trouble with the validation of JSON. Hope you can help, because it's quite specific and there is not much information online.
This is my current code:
def validate_schema(message, schema_path):
try:
# We encapsulate the code in a try-catch to always
# have a controlled answer
message_json = TypeUtilities.pyToGson(message)
# We load the schema from the file
with open(schema_path) as f:
schema_file = json.load(f)
# We create the schema
schema = JsonSchema(TypeUtilities.pyToGson(schema_file))
# Validation of the message with the schema
errors = JsonValidator.validate(schema, message_json)
if (len(errors) != 0):
return_message = (
"Error in the schema validation. "
+ "Check that the format follows the schema ("
+ schema_path.split(os.sep)[-1]
+ "). "
+ str(errors)
)
return False, return_message
# Everything goes right
result = True
return_message = ""
except:
filename = str(sys._getframe().f_code.co_filename)
func = str(sys._getframe().f_code.co_name),
line = str(sys._getframe().f_lineno)
exception_message = get_exception_message(filename, func, line)
system.util.getLogger("SGP.Core").fatal(exception_message)
result = False
return_message = exception_message
return result, return_message
Being brief, this code gets a JSON dict (param message) and validate it with the json schema located in the file path (param schema_path).
The message:
{'date': '2024-06-10 10:40:00.000+01:00'}
The schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "http://example.com/example.json",
"type": "object",
"properties": {
"date": {
"type": "string",
"format": "date-time"
}
}
}
The thing is, that I'm not able to parse the validation with this message:
{'date': '2024-06-10 10:40:00.000'}
As you may see, there is no time offset at the end. However, the schema speciffications say that it should be possible.
My question is: Is the Ignition code validation outdated or I'm doing something wrong? Also, is there any other way I could validate that type of date-format? I think that with pattern you can do it, but not sure if is the correct aproach.
Thanks in advance!