Validator
Validator is a list of rules a field’s text must pass, each with the message
to show when it fails: the check behind a text field’s live validation and a
form’s Submit button. From 0.72.
#import "Validator.xc" // not in the Foundation umbrella: import it by nameOverview
Section titled “Overview”Validator* v = new Validator();v.requireNonEmpty(String.withCString("Enter a name."));v.requireMaxLength((u32)40, String.withCString("At most 40 characters."));v.requireMatch(String.withCString("[A-Za-z' -]+"), String.withCString("Letters only."));v.firstError(String.withCString("")); // "Enter a name."v.validate(String.withCString("Ada")); // trueRules are checked in the order they were added. Lengths count characters (UTF-8
sequences), not bytes. A pattern must match the whole text (see
Regex). The integer and number rules read the text,
trimmed of white space, as JSON writes a number.
Topics
Section titled “Topics”Rules · requireNonEmpty · requireMatch · requireRegex · requireMinLength / requireMaxLength · requireIntegerRange · requireNumberRange · requireCustom · ruleCount · removeAllRules
Checking · validate · firstError · errors
requireNonEmpty
Section titled “requireNonEmpty”void requireNonEmpty(String* message)Something other than white space.
requireMatch
Section titled “requireMatch”void requireMatch(String* pattern, String* message) throwsThe whole text matches pattern. Throws a RegexError for a bad pattern.
requireRegex
Section titled “requireRegex”void requireRegex(Regex* re, String* message)The same with a Regex already compiled, with options say.
requireMinLength / requireMaxLength
Section titled “requireMinLength / requireMaxLength”void requireMinLength(u32 n, String* message)void requireMaxLength(u32 n, String* message)In characters.
requireIntegerRange
Section titled “requireIntegerRange”void requireIntegerRange(i64 lo, i64 hi, String* message)A whole number from lo to hi.
requireNumberRange
Section titled “requireNumberRange”void requireNumberRange(double lo, double hi, String* message)Any number, whole or decimal, from lo to hi.
requireCustom
Section titled “requireCustom”void requireCustom(callback check bool(String* text), String* message)check, a bound method or a function, returns true for good text.
ruleCount
Section titled “ruleCount”u32 ruleCount(void)removeAllRules
Section titled “removeAllRules”void removeAllRules(void)Checking
Section titled “Checking”A null text is checked as "".
validate
Section titled “validate”bool validate(String* text)Whether text passes every rule.
firstError
Section titled “firstError”String* firstError(String* text)The message of the first rule text fails, or null: what a field shows under
itself.
errors
Section titled “errors”Array* errors(String* text)The messages of every rule text fails, in order.