Skip to main content

Rules

alt text

The RuleContext middleware evaluates incoming requests against defined rules. If a request fails validation, an error is returned; otherwise, the request proceeds to the next step.

Rule Types and Definitions

Rule TypeDescriptionExample
HEADERMatches the exact value of a specific HTTP header.Header('Content-Type', 'application/json') matches Content-Type: application/json.
HEADER_PREFIXMatches when the header value starts with the given prefix.HeaderPrefix('User-Agent', 'Mozilla') matches User-Agent: Mozilla/5.0.
HEADER_REGEXMatches the header value using a regular expression.HeaderRegex('X-Custom-Header', '^[a-zA-Z0-9]{10}$') matches a 10-character alphanumeric string.
QUERYMatches an exact query parameter key-value pair.Query('status', 'active') matches ?status=active.
QUERY_PREFIXMatches when the query parameter value starts with the specified prefix.QueryPrefix('filter', 'user_') matches ?filter=user_123.
QUERY_REGEXMatches the query parameter value using a regex.QueryRegex('id', '\\d+') matches ?id=123.
PATHMatches the exact request path.Path('/tasks') matches /tasks.
PATH_PREFIXMatches paths with the specified prefix.PathPrefix('/api') matches /api/tasks.
PATH_REGEXMatches the path using a regex.PathRegex('/user/[a-z]+') matches /user/john.
HOSTMatches the exact hostname.Host('example.com') matches example.com.
HOST_PREFIXMatches when the hostname starts with the specified prefix.HostPrefix('api.') matches api.example.com.
HOST_REGEXMatches the hostname using a regex.HostRegex('.*\\.example\\.com') matches sub.example.com.

Configuring Rules

proxies:
- path: /task
service: tasksApi
middleware:
basicAuth: 'basicAuth:roleB'
rule: "(Header('Content-Type', 'application/json') && HeaderPrefix('User-Agent', 'Mozilla')) || HeaderRegex('X-Custom-Header', '^[a-zA-Z0-9]{10}$')"
ttl: 10000
service:
- name: tasksApi
url: http://localhost:5173/template/1on1-meeting-agenda/tasks.json
methods: ['GET', 'POST', 'PUT']

The rule in the configuration defines the conditions that must be met for a request to proceed:

  • It checks that the Content-Type header equals application/json AND the User-Agent header starts with Mozilla.
  • OR it validates that the X-Custom-Header matches the regex pattern ^[a-zA-Z0-9]10$ (exactly 10 alphanumeric characters).