registration.tri (1759B)
1 !import bd2c14d6db6b69308a5252a8e839705a55d30e815f012900b094315263e08d81 * 2 !import 3f70d95cf1bae0928ee89acd2a9e0cdd8287796efae32a9f08d33e3dd0149c55 as A 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 -- Name must be nonempty scalar text. Age must be a natural from 18 to 120. 8 -- Email uses a deliberately small rule: nonempty text containing @ and a dot. 9 -- Terms must be the canonical true Boolean. This is a teaching policy, not SMTP validation. 10 -- Browser and server can pin this same executable instead of duplicating rules. 11 -- Records use [fieldName tag value]: 0 = text, 1 = natural, 2 = Boolean. 12 -- Base supplies arithmetic/Booleans; A.hostString? checks Unicode scalar text. 13 14 nameMessage name = case A.hostString? name of 15 ^true = (case name of 16 [] = "Name is required." 17 _ = "") 18 _ = "Name must be scalar text." 19 20 emailMessage email = case A.hostString? email of 21 ^true = (case and? (contains? "@" email) (contains? "." email) of 22 ^true = "" 23 _ = "Email must contain @ and a dot.") 24 _ = "Email must be scalar text." 25 26 ageMessage age = case isNat? age of 27 ^true = (case and? (lte_? 18 age) (lte_? age 120) of 28 ^true = "" 29 _ = "Age must be from 18 to 120.") 30 _ = "Age must be a canonical natural." 31 32 termsMessage ^true = "" 33 termsMessage _ = "Accept the terms." 34 35 report name email age terms = [ 36 ["accepted" 2 (and? (equal? name "") (and? (equal? email "") (and? (equal? age "") (equal? terms ""))))] 37 ["nameError" 0 name] ["emailError" 0 email] 38 ["ageError" 0 age] ["termsError" 0 terms]] 39 40 main name email age terms = report (nameMessage name) (emailMessage email) (ageMessage age) (termsMessage terms)