Skip to content

Validation

Devlish provides several mechanisms for checking values, enforcing business rules, and writing tests. Validation failures stop execution unless caught by a Try block.

The must keyword applies a rule to a named value:

liability_cap must be at least 1000000
termination_days must be at most 30
status must equal "approved"
notes must contain "signed"
invoice_code must match "INV-*"
contact_email must be present
legacy_field must be missing
status must be one of list of "approved", "pending"
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.

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 present

Verify is an alias for validation that reads naturally in some contexts:

Verify contract_value is at least 10000

Use Expect for test-style assertions. Each assertion gets a named identifier:

amount equals 1200
Expect 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.

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.

Run with --test to make failed assertions produce a non-zero exit code:

Terminal window
devlish run tests/pricing_rules.dvl --test

In 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.

# Validate an invoice record
Read JSON from "invoice.json" as invoice
amount equals amount of invoice
customer equals customer of invoice
due_date equals due_date of invoice
# Business rule validations
amount must be at least 100
amount must be at most 10000000
customer must be present
due_date must be present
# Conditional requirement
Require amount > 50000 otherwise fail with "Invoices over 50k need approval"
# Test assertions for CI
Expect amount equals 1200 as "invoice-amount"
Expect customer equals "Acme Corp" as "invoice-customer"
Export assertions to "tmp/invoice-assertions.json"