Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.7 KiB · at f30ac63

Write validateSignup(form, today). form is a raw object from a request body; today is the current date as YYYY-MM-DD (always valid).

General rule: a field whose value is null or undefined (or that is not in form) is missing. A present value that is not a string gets code "format". Each field reports at most one error: the first failing check below.

  • email: trim it. Missing or blank after trimming: required. Otherwise it must match /^[^\s@]+@[^\s@]+\.[^\s@]+$/, else format. The result holds the trimmed, lowercased email.
  • password: used exactly as given (no trimming). Missing or "": required. Fewer than 10 characters: too_short. No ASCII digit 0-9: no_digit.
  • confirmPassword: checked only when password is a non-empty string. mismatch unless it is exactly equal to password.
  • birthDate: missing or "": required. Must be exactly YYYY-MM-DD and a real calendar date (Gregorian leap years), else format. The person must be at least 13 years old on today, else too_young: they turn 13 on the same month and day 13 years after birth, except that someone born on February 29 has that birthday on March 1 in a non-leap year.
  • referralCode: optional. Missing, or a string that is blank after trimming, means absent. Otherwise the trimmed value must match /^[A-Z]{3}-\d{4}$/, else format.

On success return { email, password, birthDate, referralCode } (referralCode absent when not given; the others as described). Other keys of form are ignored and form is not modified.

If any field has an error, fail with { kind: "invalid", errors }, where errors lists every failing field as { field, code } in the field order above.