vazco / uniforms

A React library for building forms from any schema.
https://uniforms.tools
MIT License
1.94k stars 239 forks source link

Recursive schema evaluation - stop evaluating initial value for empty array #1282

Open Monteth opened 1 year ago

Monteth commented 1 year ago

Uniforms go through the schema to get starting values for the following fields. Therefore, during the initial load, it must go through all the 'leaves' of the schema tree. This eager approach prevents us from processing recursive schemas. However, this doesn't have to be the case.

When we think about different types of recursive schemas, they only make sense when they can be completed in a finite amount of time. This requires specific points where the evaluation can stop. An array has a built-in mechanism for this. It can contain one or multiple values to continue expanding the tree, or it can have 0 elements to close off a branch. This enables us to evaluate a recursive schema and create a finite model. In this model, the 'leaves' can consist of empty arrays.

Consider this schema, where we can add multiple children on different levels, and it will only evaluate the next tree level when we add a new value to the model (+ button). So the schema of the family tree is potentially infinite, but the model is finite.

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "children": {
      "type": "array",
      "items": { "$ref": "#" }
    }
  }
}
const model = {
  "children": [
    {
      "name": "Emma",
      "children": [
        {
          "name": "Olivia"
        },
        {
          "name": "Sophia",
          "children": [
            {
              "name": "Zoe"
            }
          ]
        }
      ]
    }
  ],
  "name": "Alice"
};
codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 94.50%. Comparing base (49715f4) to head (124e714). Report is 2 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1282 +/- ## ========================================== - Coverage 94.61% 94.50% -0.11% ========================================== Files 231 231 Lines 3823 3824 +1 Branches 1030 1030 ========================================== - Hits 3617 3614 -3 - Misses 82 83 +1 - Partials 124 127 +3 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

kestarumper commented 3 months ago

We talked internally with @Monteth, and we also want to include a basic cycle detection mechanism to prevent freezing the browser tab (prevent infinite loops). Here's the proof of concept https://runkit.com/monteth/json-schema-cycles-detector.