Custom Validation Rules¶
Custom rules are Fleet's extension mechanism for CORE — start with Validation with CORE for the base engine setup and the official rule sets. When the standard CDISC CORE conformance rules do not cover a study-specific requirement — a sponsor SOP constraint, an FDA Technical Conformance Guide (TCG) gap, a protocol-specific range check, or ADaM checks while the official ADaMIG rule set remains unpublished — you author custom rules and run them next to the official rules.
Official and custom rules both run¶
CORE runs only the -lr rules when it gets a rules directory: one AE file
ran 430 official rules without -lr and only the 2 custom rules with it. So
Fleet runs the engine twice per standard when custom rules are set:
- one run without
-lr: the official CDISC rules, - one run with
-lr: the custom rules only.
The findings of both runs join into one report per dataset. Each finding
names its source, official or custom:
1. [CORE-000001] (official) AESTDTC is after AEENDTC
2. [FLEET-AE-001] (custom) AESEV is missing for a serious AE
The source is also on ValidationIssue.source, in the HTML reports, and in
the MCP fleet_validate output. Without custom rules, the engine runs once
per standard.
When to write custom rules¶
- Sponsor SOPs require checks not covered by CDISC CORE (e.g. mandatory SUPPQUAL variables).
- FDA TCG requirements that have not yet been formalised as CORE rules.
- Protocol-specific constraints such as custom reference ranges, visit windows, or dose limits.
- Interim quality gates during study setup before official validation.
Quick start¶
1. Configure the custom rules directory¶
In your study_config.yaml, set the custom_rules path:
fleet init cfg sets this up by default. The engine loads all .yaml files in this directory.
2. Scaffold a new rule¶
# Auto-increments ID: FLEET-AE-001, FLEET-AE-002, …
uv run fleet add-rule --domain AE --description "AESEV must be populated when AESER = Y"
# Custom prefix
uv run fleet add-rule --domain AE --prefix ACME --description "Severity required for serious AEs"
# Explicit ID (skips auto-increment)
uv run fleet add-rule --domain VS --id ACME-VS-010 --description "VSORRES range check"
# Output to a specific directory (overrides study_config)
uv run fleet add-rule --domain AE --description "Custom check" --output ./my_rules/
3. Edit the Check block¶
Open the generated YAML and fill in your validation logic in the Check: section:
Check:
all:
- name: AESER
operator: equal_to
value: "Y"
value_is_literal: true
- name: AESEV
operator: empty
4. Run validation¶
# Inline in a pipeline: inside the study, validation.custom_rules applies
ae = raw_df.sdtm.assign("DOMAIN", value="AE").validate("AE").to_frame()
# Or name the directory explicitly
ae = (
raw_df.sdtm.assign("DOMAIN", value="AE")
.validate("AE", custom_rules="metadata/custom_rules")
.to_frame()
)
# Or via the ValidationService
from fleet.validation.service import ValidationService
svc = ValidationService()
result = svc.validate_sdtm(ae, domain="AE", version="3.4", custom_rules="metadata/custom_rules")
result.print_report()
A relative rules path is relative to the working directory, as the dataset path is. Fleet resolves it before the engine starts in its own folder.
5. Read the rule counts¶
The custom rules run in their own engine run. The report counts what that run did with the rules, apart from the data findings:
fleet validate and the HTML report also name the rules:
Rule problems in the sdtmig engine run (not data findings; exit code kept):
Could not run (custom): FLEET-AE-002 (Rule contains invalid operator)
Not loaded (custom): broken.yaml
- Could not run: the engine reports
EXECUTION_ERRORfor the rule, for example an unknown operator. The reason comes from the engine. - Not loaded: a YAML file that does not parse, or a rule ID that is absent from the engine report.
- Skipped: the engine skips a rule when no dataset is in its scope, or when a variable the rule names is absent from the dataset. This is correct for a permissive variable that was not collected, so a skip is counted, never listed. Check the variable names of a rule that you expect to run.
Rule problems do not change the status or the exit code. A true finding is always kept, whatever its message says.
Rule anatomy¶
A custom rule follows the same YAML schema as CDISC CORE rules. Here is an annotated example:
Core:
Id: FLEET-AE-001 # Unique ID (auto-generated by fleet add-rule)
Status: Draft
Version: "1"
Rule Type: Record Data # Record-level check
Scope:
Classes:
Include: [ALL] # Every class; Domains below sets the scope
Domains:
Include: [AE] # Restrict to the AE domain
Check: # The validation logic (what you write)
all:
- name: AESER
operator: equal_to
value: "Y"
value_is_literal: true
- name: AESEV
operator: empty
Outcome:
Message: >-
AESEV is empty for a record where AESER = 'Y'. Serious adverse events
must have severity documented.
Output Variables: [USUBJID, AETERM, AESER, AESEV]
Description: >-
AESEV must be populated when AESER = 'Y'.
Authorities:
- Organization: Fleet
Standards:
- Name: SDTMIG
Version: "3.4"
References:
- Origin: Sponsor SOP
Rule Identifier:
Id: FLEET-AE-001
Version: "1"
Sensitivity: Record
Executability: Fully Executable
The check DSL¶
CDISC CORE rules use a JSON/YAML check DSL built from composable operators:
| Combinator | Meaning |
|---|---|
all |
All conditions must be true (AND) |
any |
At least one condition is true (OR) |
not |
Negates the enclosed condition |
Each leaf condition specifies a variable name, an operator, and optionally a value:
Check:
all:
- name: AESEV
operator: is_contained_by
value: ["MILD", "MODERATE", "SEVERE"]
- name: AESER
operator: equal_to
value: "Y"
value_is_literal: true
Available operators¶
| Operator | Description |
|---|---|
equal_to |
Exact match |
not_equal_to |
Not equal |
empty |
Variable is null/blank |
non_empty |
Variable has a value |
less_than / greater_than |
Numeric/date comparison |
less_than_or_equal_to / greater_than_or_equal_to |
Inclusive comparison |
contains / does_not_contain |
Substring check |
is_contained_by |
Value is in a list |
starts_with / ends_with |
Prefix/suffix check |
matches_regex |
Regex pattern match |
date_less_than / date_greater_than |
Date comparison |
For the full operator reference, see the CDISC CORE Rule Editor.
Configuration and resolution order¶
Fleet resolves custom rules from multiple sources. The first match wins:
- Explicit
custom_rules=parameter on.validate()orValidationServicemethods. validation.custom_rulesinstudy_config.yaml(team-wide default). The.validate()verb finds the study config from the working directory.- None — no custom rules are loaded.
A directory that does not exist is refused before the engine starts: the
verb raises ValidationSetupError. The report header shows the directory
used and its source.
Storing custom rules¶
Place custom rule files in the configured directory (default: metadata/custom_rules/):
my_study/
study_config.yaml
metadata/
custom_rules/ ← Your sponsor-specific rules (all .yaml files loaded)
FLEET-AE-001.yaml
FLEET-AE-002.yaml
ACME-VS-001.yaml
sdtm/
rules/ ← CDISC CORE rules (from fleet init rules)
adam/
rules/
Commit the directory to VCS so the complete rule inventory is versioned alongside the study.
ID naming convention¶
| Pattern | Example | Owner |
|---|---|---|
CORE-XXXXXX |
CORE-000001 |
CDISC (official — never reuse) |
<COMPANY>-<DOMAIN>-<NNN> |
ACME-AE-001 |
Your organisation |
FLEET-<DOMAIN>-<NNN> |
FLEET-DM-003 |
Default prefix |
The --prefix flag on fleet add-rule controls the prefix. Auto-increment scans existing
rules in the target directory to determine the next available sequence number.
Uniqueness guarantee¶
fleet add-rule checks all existing .yaml files in the target directory and refuses to
create a rule if the Core.Id already exists. This prevents accidental overwrites.
Validating rule schema locally¶
Before running rules through the engine, you can validate them against the vendored CDISC JSON Schema:
import json
import yaml
from jsonschema import Draft202012Validator
from pathlib import Path
schema_path = Path("src/fleet/validation/schemas/cdisc_rule_schema.json")
schema = json.loads(schema_path.read_text())
with open("metadata/custom_rules/FLEET-AE-001.yaml") as f:
rule = yaml.safe_load(f)
validator = Draft202012Validator(schema)
errors = list(validator.iter_errors(rule))
# errors == [] means the rule is schema-valid
Contributing upstream¶
If your custom rule addresses a gap that would benefit the broader CDISC community, consider contributing it back via the CDISC Rule Editor or the CORE GitHub repository.