Skip to content

Conversation

@antoniocapelo
Copy link
Collaborator

@antoniocapelo antoniocapelo commented May 2, 2025

Objective

This PR aims to allow x-jsf-logic schema to affect properties of fields.

Approach

After a brainy convo with @lukad we decided on a new approach to this:

  • whenever we're about to interpret a schema (either on start or on change), if this schema has computed values defined, we make a copy of it, where all the computed attributes are correctly replaced by their computed values, and use that instead.
  • this makes field manipulatio via x-jsf-logic way easier and it also removes the need for the validateJsonLogicComputedValues validation — since the schema has now the correct values in place, it can operate as with any other schema (hence some code being removed!)

Note for reviewers

You'll see more than a 1000 lines being added but this is because I ported all json-logic tests (and their fixtures) from v0 codebase. Please skip reviewing them as they're a copy-paste from v0 (except the part where I removed some of the tests as they were testing unused features that we dropped for v1)

Some examples that should work
{
  "properties": {
    "amount": {
      "title": "Total amount",
      "type": "number",
      "x-jsf-logic-computedAttrs": {
        "x-jsf-presentation": {
          "currency": "currency_selected"
        }
      },
      "x-jsf-presentation": {
        "currency": "---",
        "inputType": "money"
      }
    },
    "currency": {
      "oneOf": [
        {
          "const": "AED",
          "title": "AED - United Arab Emirates Dirham"
        },
        {
          "const": "AFN",
          "title": "AFN - Afghanistani Afghani"
        }
      ],
      "title": "Currency",
      "type": "string",
      "x-jsf-presentation": {
        "inputType": "select"
      }
    }
  },
  "title": "Standard Template",
  "type": "object",
  "x-jsf-logic": {
    "computedValues": {
      "currency_selected": {
        "rule": {
          "var": "currency"
        }
      }
    }
  }
}
{
  "properties": {
    "field_a": {
      "type": "number"
    },
    "field_b": {
      "type": "number"
    }
  },
  "x-jsf-logic": {
    "computedValues": {
      "a_plus_ten": {
        "rule": {
          "+": [
            {
              "var": "field_a"
            },
            10
          ]
        }
      }
    },
    "validations": {
      "greater_than_a_plus_ten": {
        "errorMessage": "Must be greater than Field A + 10",
        "rule": {
          ">": [
            {
              "var": "field_b"
            },
            {
              "+": [
                {
                  "var": "field_a"
                },
                10
              ]
            }
          ]
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "field_a": {
            "const": 20
          }
        },
        "required": [
          "field_a"
        ]
      },
      "then": {
        "properties": {
          "field_b": {
            "x-jsf-logic-computedAttrs": {
              "title": "Must be greater than {{a_plus_ten}}."
            },
            "x-jsf-logic-validations": [
              "greater_than_a_plus_ten"
            ]
          }
        }
      }
    }
  ]
}

@antoniocapelo antoniocapelo force-pushed the devxp-2547-affect-field-properties branch 2 times, most recently from e05a913 to 37c7950 Compare May 20, 2025 16:37
@antoniocapelo antoniocapelo self-assigned this May 20, 2025
@antoniocapelo antoniocapelo requested review from dragidavid, eng-almeida, lukad and sandrina-p and removed request for dragidavid May 20, 2025 16:46
@antoniocapelo antoniocapelo marked this pull request as ready for review May 20, 2025 16:47
@antoniocapelo antoniocapelo force-pushed the devxp-2547-affect-field-properties branch from 37c7950 to f01bb59 Compare May 20, 2025 16:49
Copy link
Collaborator

@dragidavid dragidavid left a comment

Choose a reason for hiding this comment

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

Hey @antoniocapelo - left a few comments!

Took me some time but couldn't see anything major! Good stuff!

@dragidavid
Copy link
Collaborator

Seems good to me! We'll find out soon enough if I missed anything here 😅

@antoniocapelo antoniocapelo merged commit 2d80c2d into main May 27, 2025
2 checks passed
@antoniocapelo antoniocapelo deleted the devxp-2547-affect-field-properties branch May 27, 2025 11:00
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

Successfully merging this pull request may close these issues.

5 participants