You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Currently, there is no easy way to enforce a schema validation requirement across multiple fields.
Consider, for example, an event scheduling API, where users can book locations and/or staff.
A user may wish to schedule an event onsite with support staff, and so would schedule both a location and staff member.
A user may wish to schedule just the location, and provide their own support.
A user may wish to schedule staff support, but for an offsite event (hence, no location attribute)
But it would be invalid for a user to schedule neither support staff, nor a location, since this would not be a meaningful event for the system to track.
Currently, setting optional within the schema validation will bypass any other checks if the field is missing, which makes implementing rules across optional fields difficult.
I have identified two workarounds:
Inserting a custom check in a separate required field, which validates the presence of at least one of the two optional fields, and throws a custom error message if not.
This is non-ideal, however, as the error field that comes back is not one of the two optional fields.
Calling a separate function to validate the optional components outside the schema.
This is non-ideal, because the schema should describe the entirety of the validation requirements.
Describe the solution you'd like
I would like a mechanism added to the schema to define optional rules across multiple fields. For example, a require attribute in the optional options, which accepts oneOf or allOf. The require attribute would take effect if the current field is omitted.
An alternate mechanism is to add a custom function to the optional component, which may be easier for maintenance of express-validator. The function would return true if the field optional, false if not.
constrcMiddleware=[express.json(),body('location').custom((value,{ req })=>{if(!value&&!req.body.staff){thrownewError('At least one of location or staff must be provided');}returntrue;}),v.validate(rcschema),];router.post("/reservation",rcMiddleware,function(req,res){schedule.createReservation(req.auth,req.body).then(d=>res.send(d));});
Is your feature request related to a problem? Please describe.
Currently, there is no easy way to enforce a schema validation requirement across multiple fields.
Consider, for example, an event scheduling API, where users can book locations and/or staff.
But it would be invalid for a user to schedule neither support staff, nor a location, since this would not be a meaningful event for the system to track.
Currently, setting optional within the schema validation will bypass any other checks if the field is missing, which makes implementing rules across optional fields difficult.
I have identified two workarounds:
Describe the solution you'd like
I would like a mechanism added to the schema to define optional rules across multiple fields. For example, a
require
attribute in the optional options, which acceptsoneOf
orallOf
. Therequire
attribute would take effect if the current field is omitted.An alternate mechanism is to add a custom function to the optional component, which may be easier for maintenance of express-validator. The function would return
true
if the field optional,false
if not.The text was updated successfully, but these errors were encountered: