Generate schema from if-then-else condition

Hello, I am working on implementing complex json forms for registration page. The problem I have right now is that we need to handle conditional cases in both schema.json and uischema.json (field needs to be marked as required but also shown or hidden in uischema), and it is becoming increasingly more complicated to keep both in sync as project grows. Another issue is that fields that are hidden visually are still populated in data part.

Now I’ve been thinking about fixing all these issues by using custom renderers. If, for example we have if-then-else and through that we decide that some field should not be present in the schema, we would hide it in the renderer code.

The trick is how to figure out if certain field passes if-then-else condition? For example if I have schema like this:

{
  "type": "object",
  "properties": {
    "text1": {
      "type": "string"
    }
  },
  "if": {
    "properties": {
      "text1": {
        "const": "abc"
      }
    }
  },
  "then": {
    "properties": {
      "text2": {
        "type": "string"
      }
    }
  },
  "else": {
    "properties": {
      "text3": {
        "type": "string"
      }
    }
  }
}

Is it possible to run this schema through some built-in function by passing schema and data and then producing some kind of result schema that would look something like this:

"type": "object",
  "properties": {
    "text1": {
      "type": "string"
    }
   "text2": {
     "type": "string"
   }
  },

so basically, a schema where if-then-else is resolved and then apppended to properties?
Is this something that jsonforms does internally when evaluating if some field should be required or not? Thanks!

Hi @nino,

To fully support JSON Schema’s features for rendering the form you will need to use a full JSON Schema evaluation engine. For the validation use case there are quite some libraries out there, in JSON Forms we are delegating to AJV.

I’m not aware of any general purpose engine we could hook into, therefore JSON Forms does not support conditional rendering based on JSON Schemas and instead only offers rule support via the UI Schema.


However, in a real application often not the full feature set of JSON Schema needs to be supported but rather a limited subset, i.e. in most cases you know the general structure of the used JSON Schemas.

If that’s the case for you then it’s certainly feasible to implement a “mini” evaluation engine tailored for your use cases and then used in your custom renderers.

1 Like