Node express JSON-Schema multiple fields validation

498 Views Asked by At

Using express-jsonschema

How to validation two fields, for example:

("quantity" == 0 && "actualQuantity" == 0) || ("quantity" > 0 && "actualQuantity" > 0)

1

There are 1 best solutions below

0
On BEST ANSWER

Just tested, this will do the job:

{
    "anyOf" : [
        {
            "properties" : {
                "quantity" : {
                    "minimum" : 0,
                    "maximum" : 0
                },
                "actualQuantity" : {
                    "minimum" : 0,
                    "maximum" : 0
                }
            }
        },
        {
            "properties" : {
                "quantity" : {
                    "minimum" : 1
                },
                "actualQuantity" : {
                    "minimum" : 1
                }
            }
        }
    ]
}

You could also use "oneOf" instead of "anyOf", but "anyOf" is faster with most implementations.