Rules
Rules are declarative contracts attached to missions. They validate observations, claims, and outputs and enforce constraints at runtime with explicit reason codes.
Built-in Rules
R-Think includes a set of built-in rules covering common validation patterns for mission input and output.
- required — Ensures a field is present before the mission proceeds.
- type — Validates the type of a field.
- bounded — Validates numeric ranges.
- matches — Validates against a regular expression.
- enum — Validates against a set of allowed values.
- minLength / maxLength — Validates string or array length.
- custom — Defines validation logic with a function.
Defining Rules
Rules are defined as an array and attached to a mission. They are evaluated automatically before each step executes.
rules.ts typescript
import { Rule } from '@r-think/core';
const missionRules = [
Rule.required('observation'),
Rule.type('observation', 'string'),
Rule.minLength('observation', 1),
Rule.bounded('confidence', 0, 1),
Rule.enum('classifier', ['human', 'machine', 'unknown'])
]; Custom Rules
When built-in rules are insufficient, define custom rules using the Rule.custom factory. The validator function receives the value and context, returning true for pass or a string error message for fail.
custom-rule.ts typescript
Rule.custom('riskLevel', (value, ctx) => {
if (typeof value !== 'number') return 'Risk level must be a number';
if (value < 0 || value > 10) return 'Risk level must be between 0 and 10';
return true;
}, 'Risk level validation failed'); Rule Evaluation Order
Rules are evaluated in the order they are defined. If a required rule fails, subsequent rules for the same field are not evaluated.
Note
Rules evaluate at each adaptive depth with the same enforcement. L0 missions still require the required rule to pass before execution.