Reference a property outside of the array

I’ve created this issues, but maybe here there is a bigger visibility.

I need a way to reference a property outside of an array while evaluating a condition inside of the array.

Here is my example:

schema

{
  "type": "object",
  "properties": {
    "a": {
      "type": "string",
      "enum": ["Yes", "No"]
    },

    "b": {
      "type": "array",
      "minItems": 1,
      "maxItems": 5,
      "items": {
        "type": "object",
        "properties": {
          "c": { "type": "string" },
          "d": { "type": "string" }
        },
        "required": ["c"]
      }
    }
  },
  "required": ["a", "b"]
}

uischema

{
  "type": "VerticalLayout",
  "elements": [
    {
      "type": "Control",
      "scope": "#/properties/a" <-- How to reference this value from inside of the array?
    },
    {
      "type": "Control",
      "scope": "#/properties/b",
      "options": {
        "detail": {
          "type": "VerticalLayout",
          "elements": [
            { "type": "Control", "scope": "#/properties/c" },
            {
              "type": "Control",
              "scope": "#/properties/d",
              "rule": {
                "effect": "SHOW",
                "condition": {
                  "scope": "#/properties/a", <-- I want the condition to be here
                  "schema": { "const": "Yes" }
                }
              }
            }
          ]
        }
      }
    }
  ]
}

I was able to reference other values in the same item of the array, which I will need as well, but is there a way to reference values outside of the array item? It looks like that all paths in the array are scoped to that specific array item.

I’ve found this thread, the comment says it is a limitation. But this was more than 2 years ago. Is there any update on that issue? @sdirix

Could we replace the pointer implementation with a library like this that supports relative json pointers? I think it could solve the problem.

Just out of curiosity, but why do we reference the schema in the condition scope? Could we reference the data directly?

Hi @luiz290788,

I’ve found this thread, the comment says it is a limitation. But this was more than 2 years ago. Is there any update on that issue? @sdirix

Conceptually there were no changes here. We still only hand over the data which is scoped to the respective array/object, therefore you can’t use any outside references.

Could we replace the pointer implementation with a library like this that supports relative json pointers? I think it could solve the problem.

The concrete problem here is not the pointer implementation. Conceptually we only hand in the local data of the object/array so no matter what the pointer is, it can’t resolve to “outside” data. As we use the same AJV instance throughout JSON Forms you could even today construct an outside pointer via id based $refs. They just have no meaning as the corresponding data is not looked at.

Just out of curiosity, but why do we reference the schema in the condition scope? Could we reference the data directly?

JSON Schema $ref and Ui Schema scopes are on the same level of abstraction, so for consistency it makes sense to use the same kind of pointer for the rule condition scope. Using a data level pointer would not really help here as discussed above because the outside data is not used anyway.


To support this generically we would need an extension of the rules, for example to allow to define a rootCondition or parentCondition which would then be executed in that context.

If you need this support right now you can easily add such a condition in your UI Schema. It will be ignored by the off-the-shelf renderer sets, so you could add support for your custom renderers to also check these customized conditions.

Thanks for the reply

Could you be more specific on how to use AJV and $refs. That is not clear to how that would be implemented.

Do you have any plans to support this?

$ref in JSON Schema can be used to refer to different parts of the same JSON Schema (well defined), but also to outside schemas (implementation defined).

You can check AJV’s documentation to see what they support in regards of separate schema $ref. As we hand over the condition schema to the same AJV instance you could construct a $ref to a different schema.

But as outlined above, it doesn’t help here at all as the handed over data to validate is still scoped to the object/array in question and therefore referencing outside the schema will just apply whatever is referenced to the same scoped data.

There are no plans from our side to support this in the foreseeable future as we don’t need this feature (yet) in our own projects. At the moment this would only be added via a high quality community contribution or a professional support option.