Skip to content

Field required based on condition #539

Closed
@brunodymios

Description

@brunodymios

It is very common to have data structures in which a field becomes required only if a particular condition holds. For example, having a field A being required only if field B has been specified is something easily achievable with draft-07 specification using dependencies or if-then-else constructs.

However, very commonly, a field is required only if a particular boolean condition is true. More in general, let's suppose to have numeric fields A, B and C and that field A is required only if B > C.

How to accomplish that? My understanding about draft-07 specification is that this is not possible, however it could be very useful.

I think that the best solution could be to change the current specification of the required property: "required": { "$ref": "#/definitions/stringArray" } to something more complex, supporting the specification of objects instead of a stringArray, giving the possibility to specify boolean conditions. Something like the following:

{
    "title": "test",
    "type": "object",
    "properties": {
      "A": { "type": "number" },
      "B": { "type": "number" },
      "C": { "type": "number" },
    },
    "required": [
      { 
        "key": "A",
        "condition": "B > C"
      },
    ]
}

Do you think this makes sufficiently sense to work on it and open a dedicated PR?

Thank You

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions