Skip to content

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 name
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")); // true

Rules 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.

Rules · requireNonEmpty · requireMatch · requireRegex · requireMinLength / requireMaxLength · requireIntegerRange · requireNumberRange · requireCustom · ruleCount · removeAllRules

Checking · validate · firstError · errors


void requireNonEmpty(String* message)

Something other than white space.

void requireMatch(String* pattern, String* message) throws

The whole text matches pattern. Throws a RegexError for a bad pattern.

void requireRegex(Regex* re, String* message)

The same with a Regex already compiled, with options say.

void requireMinLength(u32 n, String* message)
void requireMaxLength(u32 n, String* message)

In characters.

void requireIntegerRange(i64 lo, i64 hi, String* message)

A whole number from lo to hi.

void requireNumberRange(double lo, double hi, String* message)

Any number, whole or decimal, from lo to hi.

void requireCustom(callback check bool(String* text), String* message)

check, a bound method or a function, returns true for good text.

u32 ruleCount(void)
void removeAllRules(void)

↑ Topics

A null text is checked as "".

bool validate(String* text)

Whether text passes every rule.

String* firstError(String* text)

The message of the first rule text fails, or null: what a field shows under itself.

Array* errors(String* text)

The messages of every rule text fails, in order.

↑ Topics