Skip to main content

param_is_required

Function param_is_required 

Source
pub fn param_is_required(param: &ParamDef) -> bool
Expand description

Whether an absent value for param makes the request a 400.

This is the rule resolve enforces, exported so that a tool reporting requiredness cannot disagree with the router that enforces it. The OpenAPI generator’s required flag is computed from this; before it was extracted the same expression was written out in two crates with nothing relating them, and they agreed only by diligence.

The rule is syntactic, and uniform across every scalar type: a query parameter is required unless it declared a default. Requiredness is therefore readable straight off a routes! block — name: T is required, name: T = x is optional — without knowing what T is.

bool is deliberately NOT exempt, though the pull to exempt it is strong: confirm: bool is required, and an optional flag must be written confirm: bool = false. Two reasons. It keeps both meanings sayable — exempting bool would leave no way to declare a required one. And an absent bool and an explicit ?flag=false are genuinely distinguishable on the wire (see ExtractedParams::get_bool_optional), so reading absence as false would discard a distinction the request carries.

ParamType::StringArray is exempt for the opposite reason — its exemption is forced, not chosen: urlencoding cannot express “present but empty” (see Params::get_all_optional), so there is no required/optional distinction available to lose. ⇒ A new type earns an exemption only when the wire format denies it the distinction. That it merely has a natural zero value is not enough.