Skip to content
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

Inconsistent Validation Result Output Makes it Difficult to Identify Applied Composition Rules #143

Open
slucero opened this issue Jul 18, 2022 · 1 comment

Comments

@slucero
Copy link

slucero commented Jul 18, 2022

Background

I've been using this library to work with JSON schemas and do some traversal of the schema alongside configuration data to match it. As part of this need, I need to be able to identify both the configuration value being processed at a certain level, and the related schema property the value maps to. In most cases, this works pretty well as I can traverse the properties of the schema and the configuration data in parallel, but it gets more difficult whenever I encounter anything in the schema with composition rules like anyOf or oneOf.

Whenever I'm traversing the schema and encounter one of these rules, the best solution I've come to was passing the data at that level into the schema for validation and checking the document path on the result to determine which rule was used. This works for complex data when an ObjectItem is returned and the document path is available, but when simpler values, like a string, are passed in, the value itself is returned and a document path is unavailable.

Examples

   $schema_json = <<<JSON
    {
      "\$schema": "http://json-schema.org/draft-04/schema#",
      "title": "Example",
      "type": "object",
      "properties": {
        "text": {
          "title": "Text",
          "type": "string",
          "options": {
            "grid_columns": 4
          }
        },
        "nested_items": {
          "title": "Nested items of various types",
          "type": "array",
          "items": {
            "anyOf": [
              {
                "title": "Simple object",
                "type": "object"
              },
              {
                "title": "Simple string",
                "type": "string"
              }
            ]
          }
        }
      }
    }
    JSON;

    $string_value = [
      'my_string',
    ];

    $object_value = [
      (object) [
        'my_object' => 'my_value',
      ],
    ];

    // Import the whole schema to start with.
    $schema = Schema::import(json_decode($schema_json));
    // Traverse to a lower property containing composition rules to be tested.
    $nested = $schema->getProperties()->nested_items;

    // Test a more complex value. This will return an ObjectItem.
    $object_result = $nested->in($object_value);
    echo $object_result[0]->getDocumentPath(); // "#->items[0]:0->anyOf[0]"

    // Test a simple value. This will return the value itself.
    $string_result = $nested->in($string_value);
    echo $string_result[0] // "my_string"

Based on this example, is there something I'm doing wrong or a better way to determine which composition rule(s) apply to a value? Currently I'm testing if the result is an instance of ObjectItemContract, and if it is I'm doing my own cycling through the composition rules to test, but ideally this step wouldn't be required if the return value for a validation result were consistent.

@slucero
Copy link
Author

slucero commented Nov 7, 2022

Would it be possible to get some insight on this at all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant