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

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

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

Monteth
Copy link
Member

@Monteth Monteth commented Aug 11, 2023

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"
};

@github-actions github-actions bot added Area: Bridge Affects some of the bridge packages Bridge: JSON Schema Affects the uniforms-bridge-json-schema package labels Aug 11, 2023
@Monteth Monteth added this to the v4.0 milestone Aug 18, 2023
@kestarumper kestarumper self-requested a review September 1, 2023 10:13
@kestarumper kestarumper linked an issue Sep 29, 2023 that may be closed by this pull request
@Monteth Monteth marked this pull request as ready for review April 19, 2024 14:13
@Monteth Monteth requested a review from wadamek65 as a code owner April 19, 2024 14:13
Copy link

codecov bot commented Apr 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.46%. Comparing base (ba4c593) to head (5878bf3).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1282      +/-   ##
==========================================
- Coverage   95.53%   95.46%   -0.07%     
==========================================
  Files         175      175              
  Lines        2867     2868       +1     
  Branches      773      773              
==========================================
- Hits         2739     2738       -1     
- Misses         59       61       +2     
  Partials       69       69              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kestarumper
Copy link
Member

kestarumper commented May 27, 2024

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.

@github-actions github-actions bot added the Area: Infra Affects the repository itself (e.g., CI, dependencies) label Aug 9, 2024
@Monteth Monteth marked this pull request as draft August 23, 2024 09:07
@Monteth
Copy link
Member Author

Monteth commented Jan 19, 2025

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).

I've tried to implement that, but since it is a little more complicated than we would like, I want to merge this PR without this check and (eventually) implement it under a dedicated issue.

I'd also reconsider if we really need that because I've spotted only one issue related to JSONSchema infinite loop (#596) that was fixed. This PR fixes one more case where it could take place so that it will be even less helpful but still will have an impact on the package footprint and render performance.

@kestarumper

@Monteth Monteth marked this pull request as ready for review January 19, 2025 17:36
Copy link
Collaborator

@piotrpospiech piotrpospiech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have some leftovers from trying to implement loops detection.

package.json Outdated
@@ -20,13 +20,16 @@
"@testing-library/user-event": "14.4.3",
"@types/jest": "26.0.20",
"@types/node": "18.19.50",
"buffer": "^5.5.0||^6.0.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why two different versions? It is dev depenendency, not a peer dependency. Why is this needed? Where is this used?

package.json Outdated
"eslint-config-vazco": "7.4.0",
"eslint-import-resolver-alias": "1.1.2",
"eslint-import-resolver-typescript": "2.3.0",
"husky": "8.0.1",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"json-schema-traverse": "1.0.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it added here? It was already added to the json bridge package.

package.json Outdated
"lint-staged": "13.0.3",
"process": "^0.11.10",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

@@ -37,6 +37,7 @@
},
"dependencies": {
"invariant": "^2.0.0",
"json-schema-traverse": "^1.0.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did you use it?

@github-actions github-actions bot removed the Area: Infra Affects the repository itself (e.g., CI, dependencies) label Jan 23, 2025
@Monteth Monteth requested a review from piotrpospiech January 23, 2025 10:46
@Monteth
Copy link
Member Author

Monteth commented Jan 23, 2025

I think you have some leftovers from trying to implement loop detection.

Yes, indeed. Sorry for that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Bridge Affects some of the bridge packages Bridge: JSON Schema Affects the uniforms-bridge-json-schema package
Projects
Status: Review
Development

Successfully merging this pull request may close these issues.

Support for lazy recursive JSON schemas
5 participants