Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node express JSON-Schema multiple fields validation

Using express-jsonschema

How to validation two fields, for example:

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

like image 974
Beni Gazala Avatar asked Nov 08 '22 04:11

Beni Gazala


1 Answers

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.

like image 176
erosb Avatar answered Nov 14 '22 21:11

erosb