Validation

Whether a scenario gets selected by Stubr to be used for answering an incoming requests, is dependent on two
dimensions:

  • route and http method match
  • evaluation of validation function

The attribute validate is an optional one that if defined is supposed to receive simple boolean or a function as value which determines whether the scenario is to be considered as valid or not. The evaluation function optionally receives requestHeaders, requestBody and requestParams as parameters and must return a boolean. If the function returns true, the scenario is considered to be matched and thus used to resolve the response.

📘

If you omit assigning a value to attribute validate the scenario is considered as a match if incoming request's route and http method match.

The below example would only consider the scenario as valid if an Authorization header is present on the request.

validate: (requestHeaders, requestBody, requestParams) => {
    return requestHeaders.Authorization !== undefined;
}