Chapter 13

Shared rules

shared/rules.ts is the single source of truth for what valid input looks like. It's plain TypeScript with no dependencies, so the browser bundle and the Node server both import it.

13.1 Rule schema (FieldRule)#

Property Applies to Meaning
label all Used in messages and as the control label
type all 'text' | 'secret' | 'boolean' | 'number' | 'choice' | 'date'
required, requiredMessage all For boolean, empty means false
minLength, maxLength text, secret In code points
charset text, secret Per-character allow-list (enforced while typing and on the server)
pattern, patternMessage text, secret Whole-value check
min, max, step number
options choice Allowed values
mustBeTrue boolean e.g. accept terms
matches, matchesMessage text, secret Must equal another field
password secret minLength, maxLength, minCharClasses, noTripleRepeat, notContainingFields, rejectCommon
inputMode text Mobile keyboard
withTime date Value must be YYYY-MM-DDTHH:mm instead of YYYY-MM-DD
minDate, maxDate date ISO date, 'today' or 'today±N'
minAge, maxAge date Whole years, for dates of birth
notInPast date Date (and time) must not be before now
weekdaysOnly date Monday to Friday

Fields with type: 'secret' must arrive PFE1-encrypted; the server refuses plaintext.

13.2 Adding a field end to end#

  1. Rule:
    company: { label: 'Company', type: 'text', required: false, maxLength: 100, charset: /[\p{L}\p{N} &.,'-]/u },
    
  2. Control:
    const company = new TextInput(ui, { ...fromRule(registrationRules, 'company'), placeholder: 'Acme Inc.' });
    form.add(company);   // and place it in a Column
    
  3. Server: nothing to do. validatePayload() now accepts and validates company, and still rejects any field that isn't declared.
  4. Persist it in your handler (finishRegistration in server/server.ts).

fromRule sets name, label, required, requiredMessage, maxLength, allow (from charset), inputMode and validators. Spread it first, then override anything.

13.3 Dates#

fromRule(rules, 'dateOfBirth') passes minDate/maxDate to the DatePicker as min/max, turns on the time row for withTime, and disables weekends for weekdaysOnly. The server applies the same checks (minAge, notInPast, …) and rejects impossible dates such as 2026-02-30. The demo uses dateOfBirth (age 16+) and onboardingCall (weekday slot in the next 60 days).

13.4 One-time codes#

otpRules.code (6 digits, OTP_LENGTH) drives both OtpInput (via fromRule(otpRules, 'code') plus length: OTP_LENGTH) and the server's /api/otp/verify validation.

13.5 Using the rules from other back ends#

The rule file is TypeScript. For a non-Node back end, either port the rules (they're small and declarative), or put the Node server in front as a validation gateway. Validation must happen on the server either way.