Add validation mechanisms for writing JSON. Validate against a specification. #1982
Replies: 4 comments
-
|
We should be able to support both compile time and runtime validation. Initially validation should be based off of JSON schema. |
Beta Was this translation helpful? Give feedback.
-
|
There are two approaches to runtime validation. We can perform validation on parse, or we can perform validation on C++ memory. The later will be much more performant and I think should be the first focus of Glaze. We will use the same glz::json_schema, but we can use these structs at runtime to validate the memory of the C++ objects. my_struct obj{};
if (glz::schema_validate(obj)) {
// true if the object matches the gz::json_schema
// note that we don't use a buffer, instead we would first read in a JSON file
// and then we would validate
}This works with validating before a write or after a read. |
Beta Was this translation helpful? Give feedback.
-
|
I would be interested in compile time validation based on a schema if that is possible. Current use case is to require a struct to have a couple of properties and then support structs that add whatever else they want in an object struct. Something like: {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Node",
"type": "object",
"properties": {
"type": {
"type": "string"
},
"description": {
"type": "string"
},
"additionalInformation": {
"type": "object"
}
},
"required": ["type", "description"]
}Which would be validated for both A and B: struct A
{
std::string type;
std::string description;
}
struct Internal
{
...
}
struct B
{
std::string type;
std::string description;
Internal additionalInformation;
} |
Beta Was this translation helpful? Give feedback.
-
|
I recently came across an instance where being able to validate the structs that will be converted to json against their glz::meta definitions would be awesome; especially the option where we do it in memory before ever creating JSON. Is this feature still on the roadmap? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Discussed in #674
Originally posted by scalpel4k January 3, 2024
Hi,
I see that the write operations are all noexcept. Is there a way to abort writing when some conditions fail using the custom serializers?
thx Michi
Beta Was this translation helpful? Give feedback.
All reactions