tricu

A lazy functional language based on Tree Calculus
Log | Files | Refs | README | LICENSE

registration.tri (1877B)


      1 !import bd2c14d6db6b69308a5252a8e839705a55d30e815f012900b094315263e08d81 *
      2 !import 3f70d95cf1bae0928ee89acd2a9e0cdd8287796efae32a9f08d33e3dd0149c55 *
      3 !export main
      4 
      5 -- Registration validator: name, email, age, termsAccepted are separate arguments.
      6 -- Returns one field-specific message per input, plus an accepted Boolean.
      7 -- Try result = main "James" "james@example.org" 21 true.
      8 -- Try changing the age to 17 or the email to "bad" to see field messages.
      9 -- Name must be nonempty scalar text. Age must be a natural from 18 to 120.
     10 -- Email uses a deliberately small rule: nonempty text containing @ and a dot.
     11 -- Terms must be the canonical true Boolean. This is a teaching policy, not SMTP validation.
     12 -- Browser and server can pin this same executable instead of duplicating rules.
     13 -- Records use [fieldName tag value]: 0 = text, 1 = natural, 2 = Boolean.
     14 -- Base supplies arithmetic/Booleans; hostString? checks Unicode scalar text.
     15 
     16 nameMessage name = case hostString? name of
     17   ^true = case name of
     18     [] = "Name is required."
     19     _ = ""
     20   _ = "Name must be scalar text."
     21 
     22 emailMessage email = case hostString? email of
     23   ^true = case and? (contains? "@" email) (contains? "." email) of
     24     ^true = ""
     25     _ = "Email must contain @ and a dot."
     26   _ = "Email must be scalar text."
     27 
     28 ageMessage age = case isNat? age of
     29   ^true = case and? (lte_? 18 age) (lte_? age 120) of
     30     ^true = ""
     31     _ = "Age must be from 18 to 120."
     32   _ = "Age must be a canonical natural."
     33 
     34 termsMessage ^true = ""
     35 termsMessage _ = "Accept the terms."
     36 
     37 report name email age terms = [
     38   ["accepted" 2 (and? (equal? name "") (and? (equal? email "") (and? (equal? age "") (equal? terms ""))))]
     39   ["nameError" 0 name] ["emailError" 0 email]
     40   ["ageError" 0 age] ["termsError" 0 terms]]
     41 
     42 main name email age terms = report (nameMessage name) (emailMessage email) (ageMessage age) (termsMessage terms)