Skip to content

WIP: Advanced validation proof of concept #26

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 26 commits into from

Conversation

brennj
Copy link
Collaborator

@brennj brennj commented Jul 5, 2023

Combining everything for internal presentation.

Sample data
{
  "properties": {
    "field_a": {
      "type": "number",
      "x-jsf-validations": {
        "a_greater_than_b": {
          "errorMessage": "Field A must be bigger than field B",
          "rule": {
            ">": [
              {
                "var": "field_a"
              },
              {
                "var": "field_b"
              }
            ]
          }
        }
      }
    },
    "field_b": {
      "type": "number"
    }
  },
  "required": ["field_a", "field_b"]
}
{
  "properties": {
    "field_a": {
      "type": "number",
      "x-jsf-validations": {
        "a_less_than_b": {
          "errorMessage": "Field A must be smaller than field B",
          "rule": {
            "<": [
              {
                "var": "field_a"
              },
              {
                "var": "field_b"
              }
            ]
          }
        }
      }
    },
    "field_b": {
      "type": "number"
    }
  },
  "required": ["field_a", "field_b"]
}
{
  "properties": {
    "field_a": {
      "type": "number"
    },
    "field_b": {
      "type": "number"
    },
    "field_c": {
      "type": "number",
      "x-jsf-validations": {
        "add_fields_together": {
          "errorMessage": "Must be at least (field_a + field_b) * 2",
          "rule": {
            ">=": [
              { "var": "field_c" },
              {
                "*": [
                  { "+": [{ "var": "field_a" }, { "var": "field_b" }] },
                  2
                ]
              }
            ]
          }
        }
      }
    }
  },
  "required": ["field_a", "field_b", "field_c"]
}

{
  "properties": {
    "field_a": {
      "type": "number"
    },
    "field_b": {
      "type": "number"
    },
    "field_c": {
      "type": "number"
    }
  },
  "required": ["field_a", "field_b"],
  "allOf": [
    {
      "if": {
        "x-jsf-validations": {
          "require_c": {
            "const": true
          }
        }
      },
      "then": {
        "required": ["field_c"],
        "properties": {
          "field_c": {
            "x-jsf-validations": {
              "add_fields_together": {
                "errorMessage": "Must be at least (field_a + field_b) * 2",
                "rule": {
                  ">=": [
                    { "var": "field_c" },
                    {
                      "*": [
                        { "+": [{ "var": "field_a" }, { "var": "field_b" }] },
                        2
                      ]
                    }
                  ]
                }
              }
            }
          }
        }
      },
      "else": {
        "properties": {
          "field_c": false
        }
      }
    }
  ],
  "x-jsf-validations": {
    "require_c": {
      "rule": {
        "and": [
          { "!==": [{ "var": "field_b" }, null] },
          { "!==": [{ "var": "field_a" }, null] },
          { ">": [{ "var": "field_a" }, { "var": "field_b" }] }
        ]
      }
    }
  }
}
{
  "properties": {
    "field_a": {
      "title": "Accept!",
      "const": "acknowledged",
      "type": "string",
      "x-jsf-presentation": {
        "inputType": "checkbox"
      }
    },
    "field_b": {
      "type": "number"
    },
    "field_c": {
      "type": "number"
    }
  },
  "required": ["field_a", "field_b"],
  "allOf": [
    {
      "if": {
        "properties": {
          "field_a": {
            "const": "acknowledged"
          }
        },
        "required": ["field_a"]
      },
      "then": {
        "required": ["field_c"],
        "properties": {
          "field_c": {
            "x-jsf-validations": {
              "add_fields_together": {
                "errorMessage": "Must be at least field_b * 2",
                "rule": {
                  ">=": [
                    { "var": "field_c" },
                    {
                      "*": [
                        { "var": "field_b" },
                        2
                      ]
                    }
                  ]
                }
              }
            }
          }
        }
      },
      "else": {
        "properties": {
          "field_c": false
        }
      }
    }
  ]
}

@brennj brennj changed the title WIP: Test validation storage WIP: Advanced validation proof of concept Jul 5, 2023
@jguddas
Copy link
Contributor

jguddas commented Jul 6, 2023

I'm kinda missing validation of the validation, and the JSON logic Schema that could be used for this is WIP since 2019.

jwadhams/json-logic-js#55

@brennj brennj closed this Jul 7, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants