package contract

  1. Overview
  2. Docs

Module Contract.ValidateSource

Result of matching a pure request against one endpoint.

Sourcetype validated = {
  1. endpoint : Endpoint.t;
    (*

    Percent-decoded values captured from the matched path template.

    *)
  2. path_values : (string * string) list;
  3. query_values : (string * string) list;
  4. body : Yojson.Safe.t option;
}
Sourcetype validated_response = {
  1. endpoint : Endpoint.t;
  2. status : int;
  3. body : Yojson.Safe.t option;
}
Sourceval request : Endpoint.t -> Request.t -> (validated, Error.t list) result

Validate method, route, declared parameters, and request body.

Undeclared query parameters and request bodies on body-less endpoints are ignored. If a query parameter appears more than once, validation and decoding use the first matching value. Declared path parameters must be present in the matched template values; declaring a path parameter that is not present in the template is a validation error. Body field strictness is controlled by the JSON codec.

Sourceval path : validated -> string -> 'a Codec.t -> ('a, Error.t) result

Decode a matched path parameter from a validated request.

Sourceval query : validated -> string -> 'a Codec.t -> ('a option, Error.t) result

Decode a query parameter from a validated request. Missing parameters return Ok None. Duplicate parameters use the first matching value.

Sourceval body : validated -> 'a Json_codec.t -> ('a option, Error.t) result

Decode the JSON body from a validated request. Missing bodies return Ok None.

Validate a pure response against the endpoint's declared responses.

The status must match a declared response. If that response declares a JSON body, the body must be present and decode with its codec. If it declares an empty response, a present body is rejected. Duplicate response status declarations use the first matching declaration.

Sourceval response_body : validated_response -> 'a Json_codec.t -> ('a option, Error.t) result

Decode the JSON body from a validated response. Missing bodies return Ok None. The caller supplies the codec because successful response validation preserves the response as JSON, not as an existential decoded value.