Validation
Devlish provides several mechanisms for checking values, enforcing business rules, and writing tests. Validation failures stop execution unless caught by a Try block.
Value Validation (must)
Section titled “Value Validation (must)”The must keyword applies a rule to a named value:
liability_cap must be at least 1000000termination_days must be at most 30status must equal "approved"notes must contain "signed"invoice_code must match "INV-*"contact_email must be presentlegacy_field must be missingstatus must be one of list of "approved", "pending"Validation Rules Reference
Section titled “Validation Rules Reference”| Rule | Meaning |
|---|---|
must be at least N |
Numeric minimum |
must be at most N |
Numeric maximum |
must equal V |
Exact equality |
must contain S |
Substring match |
must match P |
Glob pattern match |
must be present |
Not null or empty |
must be missing |
Must be null or empty |
must be one of L |
Must appear in the given list |
Failed validations record structured results and halt the program.
Require
Section titled “Require”Use Require for arbitrary boolean conditions with a custom error message:
Require review_status is "approved" otherwise fail with "Review must be approved"Require deal_value > 100000 otherwise fail with "Deal too small for processing"Without the otherwise clause, Require still halts on failure with a generic message:
Require contact_email is presentVerify
Section titled “Verify”Verify is an alias for validation that reads naturally in some contexts:
Verify contract_value is at least 10000Assertions (Expect)
Section titled “Assertions (Expect)”Use Expect for test-style assertions. Each assertion gets a named identifier:
amount equals 1200Expect amount equals 1200 as "amount-is-1200"Expect status equals "active" as "status-check"Assertions record pass/fail results without stopping execution, making them suitable for test suites.
Exporting Assertion Results
Section titled “Exporting Assertion Results”Write all assertion results to a JSON file for CI or reporting:
Export assertions to "tmp/assertions.json"The output is a JSON array of assertion records with id, passed, expected, and actual fields.
Test Mode
Section titled “Test Mode”Run with --test to make failed assertions produce a non-zero exit code:
devlish run tests/pricing_rules.dvl --testIn normal mode, failed assertions are recorded but do not halt execution. In test mode, any failed assertion causes the program to exit with status 1.
Complete Validation Example
Section titled “Complete Validation Example”# Validate an invoice recordRead JSON from "invoice.json" as invoice
amount equals amount of invoicecustomer equals customer of invoicedue_date equals due_date of invoice
# Business rule validationsamount must be at least 100amount must be at most 10000000customer must be presentdue_date must be present
# Conditional requirementRequire amount > 50000 otherwise fail with "Invoices over 50k need approval"
# Test assertions for CIExpect amount equals 1200 as "invoice-amount"Expect customer equals "Acme Corp" as "invoice-customer"Export assertions to "tmp/invoice-assertions.json"