Comments (10)
For now, that kind of analysis is just something you have to do manually.
Yes, I have tried many similar libraries, and your library is already a very good work. Thank you for your help.
from json-everything.
I'm sorry, I don't follow your explanation. Could you show an example?
from json-everything.
My problem stems from the following scenario:
Obviously, this is an "if" and "then" structure. "if" contains the "anyof" pattern. If any of the conditions of "anyof" are met, the constraints of "then" need to be accepted. I can know from the Evaluate method whether my data satisfies the "then" constraint, but I cannot know which condition of the "if" triggered the "then" constraint, and it is difficult to convert the corresponding validation logic into a warning.
from json-everything.
对不起,我不明白你的解释。你能举个例子吗?
For example, if the user fills in the A attribute, he needs to fill in the B attribute. In this verification logic, Evaluate only returns a prompt that the B attribute needs to be filled in, but does not return why the B attribute needs to be filled in.
from json-everything.
Why not use a descriptive annotation (like the "description" annotation) in your "then" schema for this purpose?
Then evaluate with either OutputFormat.List
or OutputFormat.Hierarchical
.
The evaluation result will contain a list of the evaluation results of all the constituent (sub-)schemas that make up your whole schema in its EvaluationResults.Details
property.
When validation fails, scan the EvaluationResults.Details
list for any (sub-)schema evaluation result that is invalid and which also has an annotation of interest (like the "description" annotation). If you chose the OutputFormat.Hierarchical
, you will have to do this scan recursively on each Details
list of all the scanned evaluation results.
For every invalid evaluation result with an appropriate annotation (like "description"), extract/print the annotation value.
Illustrative example schema and example code:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"if": {
"anyOf": [
{ "properties": { "strValue": { "type": "string" } } },
{ "properties": { "numValue": { "type": "number" } } },
{ "properties": { "objValue": { "type": "object" } } }
]
},
"then": {
"required": [ "foo" ],
"description": "\"strValue\", \"numValue\" and \"objValue\" require also a \"foo\" value"
}
}
var result = schema.Evaluate(jsonNode, new EvaluationOptions { OutputFormat = OutputFormat.List});
if (!result.IsValid)
{
var failingSchemaDescriptions = result.Details
.Where(r => !r.IsValid && r.Annotations?.ContainsKey("description") == true)
.Select(r => r.Annotations!["description"]);
foreach (var descr in failingSchemaDescriptions)
Console.WriteLine(descr);
}
This approach also has the advantage of not needing complex (and error-prone) special-casing of the error reporting for the countless possible and different structures and semantics of schema(s) possibly being applicable as if
.
from json-everything.
I don't think that addresses the question, which seems to be, "Which subschema in /if/anyOf
passed?"
Do do that, I'd recommend a hierarchical output.
A modified version of @elgonzo's example schema is
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"if": {
"anyOf": [
{ "properties": { "strValue": { "type": "string" } }, "required": ["strValue"] },
{ "properties": { "numValue": { "type": "number" } }, "required": ["numValue"] },
{ "properties": { "objValue": { "type": "object" } }, "required": ["objValue"] }
]
},
"then": {
"required": [ "foo" ],
"description": "\"strValue\", \"numValue\" and \"objValue\" require also a \"foo\" value"
}
}
Adding required
causes the subschema to fail if the property is missing. Running this schema against
{
"numValue": 42,
"foo": true
}
gives the results
{
"valid": true,
"evaluationPath": "",
"schemaLocation": "https://json-everything.net/9e1c5e4c78#",
"instanceLocation": "",
"annotations": {
"if": true
},
"details": [
{
"valid": true,
"evaluationPath": "/if",
"schemaLocation": "https://json-everything.net/9e1c5e4c78#/if",
"instanceLocation": "",
"details": [
{
"valid": false,
"evaluationPath": "/if/anyOf/0",
"schemaLocation": "https://json-everything.net/9e1c5e4c78#/if/anyOf/0",
"instanceLocation": "",
"errors": {
"required": "Required properties [\"strValue\"] are not present"
}
},
{
"valid": true,
"evaluationPath": "/if/anyOf/1",
"schemaLocation": "https://json-everything.net/9e1c5e4c78#/if/anyOf/1",
"instanceLocation": "",
"annotations": {
"properties": [
"numValue"
]
},
"details": [
{
"valid": true,
"evaluationPath": "/if/anyOf/1/properties/numValue",
"schemaLocation": "https://json-everything.net/9e1c5e4c78#/if/anyOf/1/properties/numValue",
"instanceLocation": "/numValue"
}
]
},
{
"valid": false,
"evaluationPath": "/if/anyOf/2",
"schemaLocation": "https://json-everything.net/9e1c5e4c78#/if/anyOf/2",
"instanceLocation": "",
"errors": {
"required": "Required properties [\"objValue\"] are not present"
}
}
]
},
{
"valid": true,
"evaluationPath": "/then",
"schemaLocation": "https://json-everything.net/9e1c5e4c78#/then",
"instanceLocation": "",
"annotations": {
"description": "\"strValue\", \"numValue\" and \"objValue\" require also a \"foo\" value"
}
}
]
}
And you can easily see which subschema of /if/anyOf
passes (valid
is true), and the errors in the failed ones tell you why those subschemas failed.
from json-everything.
您可以轻松查看哪些子模式
/if/anyOf
通过(valid
为真),而失败的子模式中的错误会告诉您这些子模式失败的原因。
I looked at your example, and the "description" attribute does solve this problem to a certain extent, but it seems to already exist in the json schema, and it is not a warning integrated through verification, and my schema comes from a third party
This is my code:
var allOfSchemas = schema.GetAllOf() ?? new List<JsonSchema>();
var failedMessages = new Dictionary<string, List<string>>();
foreach (var allOfSchema in allOfSchemas)
{
EvaluationResults rEvaluation = allOfSchema.Evaluate(_data, new EvaluationOptions
{
OutputFormat = OutputFormat.List
});
if (rEvaluation.IsValid)
continue;
foreach (var detail in rEvaluation.Details)
{
var errors = detail.Errors;
if (errors != null && errors.Count > 0)
AddData(failedMessages, errors.First().Key, errors.First().Value);
}
}
from json-everything.
it seems to already exist in the json schema, and it is not a warning integrated through verification
But it is integrated into the validation result. That result is the output. You have to browse the output to find the information you're looking for.
my schema comes from a third party
This doesn't matter. The output will still reveal the validation result (pass/fail) of every subschema. To answer the question, "Which subschema passed," you need to browse the output.
from json-everything.
这没关系。输出仍然会显示每个子模式的验证结果(通过/失败)。请回答“哪个子模式通过了”这个问题,您需要浏览输出。
Yes, I converted the EvaluationResults to json data and clearly see the validation results and paths for each sub-schema.
But I want to convert the validation logic into natural language output to warn the user, and there's nothing I can do about it.
I also want to know the constraints of some fields in advance through verification. For example, when the A attribute is equal to false, the BCD attribute is disabled.
I tried to achieve these functions by recursively identifying each keyword using the verification path, which is obviously the stupidest and ineffective method.
I saw these possibilities from JsonSchema, are you interested? Or you have a good idea, thank you very much for your help and patience
from json-everything.
I also want to know the constraints of some fields in advance through verification. For example, when the A attribute is equal to false, the BCD attribute is disabled.
This kind of static analysis isn't available with this library (or any that I know of). There's some that is built into the internals of how evaluation is done, but it's not anything that can be output.
For now, that kind of analysis is just something you have to do manually.
from json-everything.
Related Issues (20)
- 'Required' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.RequiredAttribute' and 'Json.Schema.Generation.RequiredAttribute'? HOT 22
- Schema exception {"An item with the same key has already been added. Key: Child"} HOT 9
- Possibly wrong return type HOT 10
- Customizing the `format` generated for a field (without attributes) HOT 5
- Json.Schema.KeywordExtensions is not thread safe with custom keywords HOT 1
- Update Nuget package meta-data HOT 6
- Error "unknown context value" Raised After Checking for Existence HOT 2
- Online schema validator: Is successful validation of illegal json just a quirk or intentional HOT 5
- System.MissingMethodException for IReadOnlyCollection<JsonNode> after updating from JsonSchema.Net.Generation 4.3.0 to 4.3.0.2 HOT 5
- Can't get DiscriminatorKeyword working HOT 2
- Schema Evaluation fails with "Cannot resolve schema at {uri}" HOT 5
- Could not load file or assembly 'JsonPointer.Net, Version=5.0.0.0, Culture=neutral, PublicKeyToken=17ed63f672b0e278'. The system cannot find the file specified. HOT 10
- IndexOutOfRangeException occurred when evaluating schema with parallel execution. HOT 1
- jsonnet as sister of json-e HOT 3
- Escaping single quote in json path string HOT 8
- Extract data annotation attributes from JsonSchema.Net.Generation into separate project HOT 2
- Is there a way to do JSON validation that does case insensitive matching on property names? HOT 8
- Support language packs in schema playground
- sileo-featured.json
- How to export or save JsonSchema to plaintext? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-everything.