In sapi-nt, request validators are conditions which are checked for each incoming request before it is accepted by the API.
They can be configured uniquely for each endpoint, however multiple endpoints can share the same validator.
Requests which don’t satisfy the validation rules will result in a 400 “Bad Request” response.
Request validator specs are characterised by the following type values:
| Type | Meaning |
|---|---|
validator.parameter.required |
A required parameter validator, which asserts that at least one value is given for each of a set of parameters. |
validator.parameter.pattern |
A parameter pattern validator, which asserts that all of the values given for a parameter match a certain Regex pattern. |
On endpoint specs, request validators can be referenced by their name value, or defined inline.
Required parameter validators, having a type value of validator.parameter.required, assert that at least one value
is given for each of a set of parameters.
The list of parameters is given by the params property of the spec.
To define a validator which asserts that incoming requests must specify both label and title parameters,
you can use the following spec:
type: validator.parameter.required
name: labelRequired
params:
- label
- title
Parameter pattern validators, having a type value of validator.parameter.pattern, assert that all of the values
given for one or more parameters conform to a corresponding regular expression.
The parameter-pattern pairs are given as key-value pairs on the nested params property of the spec.
These values use standard regex syntax.
If no value is given for a parameter that is validated by this validator, no assertion will be performed.
To define a validator which asserts that the label parameter on incoming requests must have a value of foo or bar,
and the title parameter must have a value of Baz or baz, you can use the following spec:
type: validator.parameter.pattern
name: labelFormat
params:
label: "foo|bar"
title: "[Bb]az"