Skip to content

Latest commit

 

History

History
82 lines (63 loc) · 3.68 KB

README.md

File metadata and controls

82 lines (63 loc) · 3.68 KB

Validation

validation checks values and collects field errors. The implementation note is validation/readme.md.

Errors

ValidationError has Field, Rule, Message, and Params. Error returns Message when Field is empty, and otherwise Field: Message.

ValidationErrors joins its items with "; ". An empty slice returns "". HasErrors reports a non-empty slice. Add appends a field and message without a rule. AddError appends a ValidationError. Merge appends another slice. ForField returns the messages for one field. Fields returns the field names that appear. ByField returns the first message for a field, or "". First returns the first error, or a zero error when the slice is empty. AsMap groups messages by field. NewSingleError and NewError build a one-item slice. IsEmpty is true when Field, Rule, and Message are empty.

Validator is Validate() ValidationErrors. Combine appends the results of each validator.

Whole values

ValidateEmail rejects more than 254 UTF-8 bytes, a local part longer than 64 bytes, and a value that does not match one @ plus the ASCII email pattern. ValidatePassword requires 8 through 128 UTF-8 bytes, one uppercase letter, one lowercase letter, one digit, and one punctuation or symbol character. ValidateUsername requires 3 through 32 characters matching [a-zA-Z0-9_-]{3,32}. NormalizeEmail trims space and lowercases the value.

The sentinel errors are ErrEmailInvalid, ErrEmailTooLong, ErrEmailLocalTooLong, ErrPasswordTooShort, ErrPasswordTooLong, ErrPasswordNoUppercase, ErrPasswordNoLowercase, ErrPasswordNoDigit, ErrPasswordNoSpecial, ErrUsernameTooShort, ErrUsernameTooLong, and ErrUsernameInvalid.

These legacy scalar password rules are independent of the current authentication candidate policy. That policy normalizes NFC, counts code points and invokes its configured checker; it does not require these character classes.

Fields

Field, UUIDField, IntField, and FloatField collect errors for one value. An empty string skips MinLength, MaxLength, Email, Password, Phone, NoHTML, Equal, Alphanumeric, OneOf and OneOfWithLabels. Required trims space and adds is required when the result is empty. Later rules still run.

ValidateEmailField, ValidatePhoneField, and NoHTML return an empty error for an empty value. Phone validation strips spaces, hyphens, dots, and parentheses, then requires 7 through 15 characters and a match of the original value against the phone pattern.

String length rules count UTF-8 bytes. Equal compares exact strings; Alphanumeric accepts ASCII letters and digits. OneOfWithLabels compares values and uses labels only for feedback when their count matches the values. NoHTML rejects <...> patterns; it neither escapes nor sanitizes content. Keep HTML escaping at the rendering boundary.

UUIDField.Required rejects uuid.Nil. Integer and float fields provide Min, Max, Positive and NonNegative; integers also provide InRange. Float comparisons do not reject NaN, so validate finite values in the domain when required.

ValidateAll appends the errors from each FieldErrors value.

Text

NormalizeText turns control characters into spaces, trims, and collapses whitespace. NormalizeOptionalText returns nil for a nil pointer or a result that is empty. SanitizeStringSlice normalizes each item, drops empty items, and keeps the first occurrence of each remaining value. A nil slice stays nil.