GithubHelp home page GithubHelp logo

Comments (10)

wszwc avatar wszwc commented on May 28, 2024 1

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.

gregsdennis avatar gregsdennis commented on May 28, 2024

I'm sorry, I don't follow your explanation. Could you show an example?

from json-everything.

wszwc avatar wszwc commented on May 28, 2024

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.
258ff981-7129-4e73-b695-4482678bdff3

from json-everything.

wszwc avatar wszwc commented on May 28, 2024

对不起,我不明白你的解释。你能举个例子吗?

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.

elgonzo avatar elgonzo commented on May 28, 2024

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.

gregsdennis avatar gregsdennis commented on May 28, 2024

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.

wszwc avatar wszwc commented on May 28, 2024

您可以轻松查看哪些子模式/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.

gregsdennis avatar gregsdennis commented on May 28, 2024

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.

wszwc avatar wszwc commented on May 28, 2024

这没关系。输出仍然会显示每个子模式的验证结果(通过/失败)。请回答“哪个子模式通过了”这个问题,您需要浏览输出。

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.

gregsdennis avatar gregsdennis commented on May 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.