简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM