Extension SDK
Validators
Enforce domain rules JSON cannot capture. Plug into the constraint chain with structured pass, warn, or fail outcomes.
Key points
- Validators are constraint gates — they pass, warn, or fail with structured errors
- They run inline in the validation chain alongside JSON-declared constraints
- Default timeout is 10 seconds; hard fails short-circuit the rest of the chain
- Use them only when JSON expressions cannot encode the rule
The interface
Validators implement IBusinessRuleValidator. The shape mirrors calculators — name, version, async method — but the return type is structured around three outcomes:
IBusinessRuleValidatorValidates a request against a domain rule. Returns pass, warn, or fail. Plugs into the constraint chain alongside JSON-declared constraints.
Required properties
namestring— Stable identifier referenced from templatesversionstring— Semver; pinned in templates explicitly
validateEvaluate the rule against the provided input. Use ctx.pass(), ctx.warn(), or ctx.fail() to construct the result — never return a raw object.
validate(
ctx: ValidationContext,
input: TInput,
): Promise<ValidationResult>The runtime catches thrown errors and treats them as fail with code RUNTIME_ERROR. Always use ctx.fail() for known failures so the error is structured.
A complete example
The CarePlanValidator from the Home Care template — checks that a requested activity is in the participant’s approved care plan, with a soft warning if the plan is due for review:
import {
IBusinessRuleValidator,
ValidationContext,
ValidationResult,
} from '@flexischeduler/extension-sdk';
interface CarePlanValidatorInput {
event: ScheduledEvent;
participant: Entity;
}
export class CarePlanValidator implements IBusinessRuleValidator {
name = 'CarePlanValidator';
version = '1.0.0';
async validate(
ctx: ValidationContext,
{ event, participant }: CarePlanValidatorInput,
): Promise<ValidationResult> {
const carePlan = await ctx.netSmart.getCarePlan(
participant.attributes.care_plan_id,
);
// Hard fail — care plan blocks this activity
if (!carePlan.approved_activities.includes(event.activity_type)) {
return ctx.fail({
code: 'CARE_PLAN_MISMATCH',
message: `Activity "${event.activity_type}" not in approved_activities`,
suggestions: carePlan.approved_activities,
affected_entities: [participant.id],
});
}
// Soft warning — flagged but doesn't block
if (carePlan.review_due_within_days < 14) {
return ctx.warn({
code: 'CARE_PLAN_REVIEW_SOON',
message: 'Care plan is due for review within two weeks',
});
}
return ctx.pass();
}
}Pass, warn, fail
Validators always produce one of three outcomes through the context helpers. Don’t construct results manually — helpers ensure correct shape and capture trace metadata:
// Three result types
ctx.pass()
// → { status: 'pass' }
ctx.warn({ code, message, ... })
// → { status: 'warn', warning: {...} }
// The booking proceeds; the warning attaches to the response.
ctx.fail({ code, message, suggestions?, affected_entities? })
// → { status: 'fail', error: {...} }
// The booking is blocked. The error returns to the API caller.Failure shape
When you fail, structure the error so the caller knows what to do:
code— stable machine-readable identifier (e.g.CARE_PLAN_MISMATCH). Used by client code to handle specific cases.message— human-readable, can include template variables. Surfaced in error responses and the audit log.suggestions— optional. What the caller could try instead. Surfaced to the UI when present.affected_entities— optional. Which entities triggered the failure. Used for audit and analytics.
Be specific in failure codes
VALIDATION_FAILED code forces clients to parse messages. A precise CARE_PLAN_MISMATCH lets them branch. Treat codes as a stable API contract — they’re part of what your validator guarantees.Wiring into a template
Templates reference validators with type: “extension” and the extension ID from your manifest:
{
"constraints": [
{
"id": "care_plan_alignment",
"type": "extension",
"category": "hard",
"extension_id": "care_plan_validator",
"extension_inputs": {
"event": "$event",
"participant": "$primary_entity"
},
"scope": "per_event"
}
]
}Inputs are bound from the runtime context using template variables ($event, $primary_entity, etc.). The runtime marshals these into your validator’s typed input shape before calling.
Constraint chain ordering
Validators run alongside JSON-declared constraints. The engine orders the chain by cost — cheap constraints first, expensive ones last:
- Time-window and capacity checks (microseconds)
- Attribute matches against in-memory entity data (microseconds)
- Database lookups for related entity data (milliseconds)
- Validator extensions, especially those calling external services (tens to hundreds of milliseconds)
The chain short-circuits on the first hard failure. If a cheap constraint catches the issue, your validator never runs — which is the right outcome. Don’t design validators expecting to be the only check.
Caveats
- Validators must be idempotent. The same input must always produce the same outcome. The engine retries on transient errors and caches outcomes per request.
- External calls are part of your time budget. Calls to NetSmart, custom CRMs, or external services count against your timeout. Cache where you can; fail fast where you can’t.
- Don’t mutate state. Validators are read-only. State changes happen on the engine’s successful-create path, not in the validation chain.
- Always return through the helpers.
ctx.pass(),ctx.warn(),ctx.fail(). Never construct a result object directly — the helpers attach trace metadata you can’t see.