← All examples
26

Canonical Grid model

Cell Metadata & Validation

Declared presentation on a guarded expense-entry sheet: VALIDATE write contracts, static assertions, FORMAT declarations, ## doc notes, and tag-driven rendering.

Scale
Medium
Source
26-cell-metadata-validation.grid
Length
58 lines
Collection
Explainability & trust
Level
Beginner
Runtime
Portable
Version
1.0.0

Watch it in Grid

See this model in motion.

Watch the model respond in the product, then inspect the exact source and checkpoints on this page.

Companion film

The input has a contract

Attempt an invalid write, keep the last valid state with an authored explanation, then correct the input and settle the model.

44 secGrid 0.61.0
Open film page

What this model gives you

A guarded expense entry whose write contracts, static assertions, and format layers remain separate and inspectable.

20 min to study · Source reviewed 2026-08-25

Continue with guided practice

What to notice

  • Input validation contracts
  • Compile-time assertions
  • Tag and cell format precedence
  • Rule format overrides and reset

Requirements

  • Portable
  • Write-capable editor or API
  • No network

Expected checkpoint

A known state for this walkthrough.

At the authored inputs with AuditMode false, before attempting invalid writes.

Subtotal
120Subtotal
Budget share
0.005BudgetShare
Net amount
114.00NetAmount, declared two-decimal format
Approval
auto-approvedApprovalStatus
01 · Guard

Validation rejects bad writes at the boundary

Quantity, price, category, discount, and invoice reference keep their stored values when a write violates the authored contract.

02 · Prove

The same syntax can constrain computed cells

Input intervals let the compiler prove Subtotal and BudgetShare stay inside their declared ranges.

03 · Present

Format layers never change the value

AuditMode changes precision and rule overrides, then RESET reveals the declared tag policy again.

26-cell-metadata-validation.grid
Get Grid
MODEL "Cell Metadata & Validation"
DESCRIPTION "Declared presentation on a guarded expense-entry sheet: VALIDATE write contracts, static assertions, FORMAT declarations, ## doc notes, and tag-driven rendering."
VERSION "1.0.0"
AUTHOR "Grid Team"
TAGS "canonical", "presentation", "validation", "metadata", "format"

# Tag policy: every percentage-tagged cell renders with one decimal place
# unless a stronger layer (per-cell clause, rule override) takes over.
FORMAT percentage "0.0%"

# Guarded inputs — a VALIDATE clause on an input is a write contract that
# gates every write surface (grid UI, API, connectors) and reports MESSAGE.
input Units = 3 VALIDATE 1..48   ## range sugar for BETWEEN 1 AND 48

## List price per unit from the vendor quote, before any discount.
input UnitPrice = 40
  VALIDATE BETWEEN 0 AND 500 MESSAGE "Unit price must be between $0 and $500."

## Vendor-negotiated discount. Procurement caps it at 30%.
## Renders as 0.0% via the percentage tag policy above.
input DiscountRate IS percentage = 5pct
  VALIDATE BETWEEN 0pct AND 30pct MESSAGE "Discount must be between 0% and 30%."

input Category = "travel"
  VALIDATE IN ["travel", "software", "meals", "hardware"] MESSAGE "Pick an approved expense category."

input InvoiceRef = "INV-1042"
  VALIDATE STARTS WITH "INV-" MESSAGE "Invoice references begin with INV-."

input AuditMode = FALSE FORMAT "switch"   ## boolean renders as a toggle switch

# Computed cells — here VALIDATE is a static assertion. The write contracts
# on Units and UnitPrice seed interval analysis, so the compiler proves both
# assertions below at compile time and records them in module metadata.
Subtotal = Units * UnitPrice VALIDATE 0..24000

## Fraction of the $24,000 per-claim cap this entry consumes.
BudgetShare = Subtotal / 24000 VALIDATE 0..1

# Type-tag-driven presentation: the currency tag says what the value *is*;
# the FORMAT clause says exactly how it renders. The format operand is an
# expression, so it re-derives whenever AuditMode flips.
NetAmount IS currency = Subtotal * (1 - DiscountRate)
  FORMAT IF(AuditMode, "$#,##0.0000", "$#,##0.00")

ApprovalStatus = NetAmount > 5000 THEN "needs-approval" ELSE "auto-approved"

# Rule-driven overrides sit above the declared layer: FORMAT actions write
# the override, RESET FORMAT clears it so the tag policy governs again.
WHEN AuditMode THEN
  FORMAT DiscountRate "0.000%"
END

WHEN NOT AuditMode THEN
  RESET FORMAT DiscountRate
END

END MODEL