Skip to content

Validation

Iridium is a SSR UI framework, meaning validation is typically written once on the backend & validated on certain requests, like a when saving your form to a model.

Iridum supports basic client side validation based on HTML input rules.

Iridium has some built in validation functions you can use based on Laravel's validation rules.

Feel free to attach these to your form elements, or write your own. Your form lifecycle also offers hooks where additional validation rules can be written.

WARNING

Please remember when using client side validation to include the same rule(s) on the backend. It is trivial for an attacker to skip your client side validation.

Rules

Golang natively interprets all your form elements as arrays of strings. Iridium's validation operates over these string arrays to apply your validation rules.

We use some conviencence wrappers to convert a []string to string for elements that do not support multi-values, like text inputs, or switches, for certain callback functions, but please know behind the scences, for both single and multi-value fields all values are handled as []string. This is important when writing your own custom rules.

Type Generation

If you're using type generation, you can access all your rules with the prefix of Rule

Applying Rules

You can apply rule(s) to your form element with the .Rules() method. An example for a text input is provided

go
FormInput("name").
    Rules(
        RuleAscii(),
        RuleEndsWith("hi"),
    )

Index

SectionRules
GeneralRequired · Required If · Required Unless · Required With · Required With All · Required Without · Required Without All · Sometimes · Any Of · Bail · Filled · Missing · Missing If · Missing Unless · Missing With · Missing With All · Prohibited · Prohibited If · Prohibited Unless · Prohibits
ArraysArray Between · Array Contains · Array Doesn’t Contain · Distinct · In Array · Min Count · Max Count · Count Between · Array Max · Array Min · Array Size · Array Distinct
BooleansAccepted · Accepted If · Boolean · Declined · Declined If
DatesDate Format · After · After or Equal · Before · Before or Equal · Date Equals · Different Date Field · Timezone
NumbersNumeric · Decimal · Integer Rule · Between Number · Different Number Field · Digits · DigitsBetween · Greater Than · Greater Than Or Equal · Less Than · Less Than Or Equal · Max Number · Max Digits · Min Number · Min Digits · Multiple Of · Size Number
StringsAlpha · Alpha Numeric · Email · In · Not In · Matches · URL · UUID · Active URL · AlphaDash · Ascii · Confirmed · Different Field · Doesn’t Start With · Doesn’t End With · Ends With · Hex Colour · IP Address · Json · Mac Address · Not Regex Rule · Same Field · Starts With Rule · Uppercase · ULID
DriverComing soon
FilesComing soon

General

Required

Ensures the field has at least one non-empty value (trimmed). Empty or all-whitespace values fail.

Required If

Conditional Required when another field’s first value equals a specific string.

Required Unless

Required unless another field’s first value equals a specific string.

Required With

Required if any of the listed fields are present (non-empty after trimming).

Required With All

Required if all of the listed fields are present (non-empty after trimming).

Required Without

Required if any of the listed fields are missing or empty.

Required Without All

Required if all of the listed fields are missing or empty.

Sometimes

Runs the inner rule only when this field is present (len(values) > 0). If absent, passes.

Any Of

Logical OR across rules. Passes if any rule passes; otherwise returns the last failure message.

Bail

Runs rules in order and stops at the first failure (logical AND with short-circuit).

Filled

Passes if at least one value is non-empty (trimmed). Otherwise fails with “Must be filled”.

Missing

Passes only if the field is entirely absent/empty. Any non-empty value fails with “Must be missing”.

Missing If

Requires this field to be missing when another field equals a specific value.

Missing Unless

Requires this field to be missing unless another field equals a specific value.

Missing With

Requires this field to be missing when any of the listed fields are present.

Missing With All

Requires this field to be missing when all of the listed fields are present.

Prohibited

Field must not be sent. Any non-empty value fails with “This field is prohibited”.

Prohibited If

Applies Prohibited when another field equals a specific value.

Prohibited Unless

Applies Prohibited unless another field equals a specific value.

Prohibits

Mutual exclusion helper: if this field is present (non-empty), the “other” field must be empty.

Arrays

All array rules operate over the []string as submitted, counting only non-empty items where applicable.

Array Between

Alias of Count Between.

Array Contains

Passes if any submitted value equals the provided needle (exact string match).

Array Doesn’t Contain

Fails if any submitted value equals the provided needle.

Distinct

Non-empty trimmed values must be unique.

In Array

Every non-empty value must appear in another field’s values (set membership against the other field).

Min Count

Requires at least N non-empty values.

Max Count

Requires at most N non-empty values.

Count Between

Non-empty count must be within [min, max].

Array Max

Alias of Max Count.

Array Min

Alias of Min Count.

Array Size

Exactly N non-empty values (Count Between with same bounds).

Array Distinct

Alias of Distinct.

Booleans

Empty values are skipped for these checks.

Accepted

Every non-empty value must be one of: yes, on, 1, true (case-insensitive).

Accepted If

Applies Accepted only when another field equals a specific value.

Boolean

Every non-empty value must be one of: true, false, 1, 0, on, off, yes, no (case-insensitive).

Declined

Every non-empty value must be one of: no, off, 0, false (case-insensitive).

Declined If

Applies Declined only when another field equals a specific value.

Dates

Each non-empty value is parsed using a Go time layout you provide (e.g., 2006-01-02).

Date Format

Must parse with the provided layout. Fails with “Invalid date”.

After

Parsed date must be strictly after the provided min (same layout).

After or Equal

Parsed date must be >= min.

Before

Parsed date must be strictly before max.

Before or Equal

Parsed date must be <= max.

Date Equals

Parsed date must equal the provided exact date.

Different Date Field

Parsed date must differ from the other field’s first parsed date (same layout).

Timezone

Must be a valid IANA timezone (loadable via time.LoadLocation).

Numbers

Empty values are skipped unless noted; comparisons parse as float64 unless the rule states string comparison.

Numeric

Digits only (0–9).

Decimal

Optional sign, digits, optional fractional part with up to maxDecimalPlaces.

Integer Rule

Optional sign; digits only.

Between Number

Parsed number within [min, max].

Different Number Field

Must differ from the other field’s first value (string compare).

Digits

Exactly N digits.

DigitsBetween

Digit length within [min, max].

Greater Than

Parsed number > threshold.

Greater Than Or Equal

Parsed number >= threshold.

Less Than

Parsed number < threshold.

Less Than Or Equal

Parsed number <= threshold.

Max Number

Convenience alias of ≤ max.

Max Digits

At most N digits.

Min Number

Convenience alias of ≥ min.

Min Digits

At least N digits.

Multiple Of

Parsed number must be an exact multiple of k.

Size Number

Parsed number must equal exact.

Strings

Format/style checks skip empty values.

Alpha

Letters A–Z only.

Alpha Numeric

Letters and digits only.

Email

Basic email pattern check.

In

Every value must be in the allowed set (exact match).

Not In

No value may be in the forbidden set.

Matches

Must match the provided regex. Error message can be customized.

URL

Must start with http(s)😕/ and include a host.

UUID

Canonical UUID v1–v5.

Active URL

Basic “active-style” URL: http(s) scheme + host.

AlphaDash

Letters, hyphen, underscore.

Ascii

ASCII printable characters only.

Confirmed

Must equal the other field’s first value (e.g., password confirmation).

Different Field

Must differ from the other field’s first value (string compare).

Doesn’t Start With

May not start with any of the provided prefixes.

Doesn’t End With

May not end with any of the provided suffixes.

Ends With

Must end with at least one of the provided suffixes.

Hex Colour

#RGB, #RRGGBB, or #RRGGBBAA.

IP Address

Valid IPv4 or IPv6 textual address.

Json

Must be valid JSON (any JSON type).

Mac Address

Standard hex-byte forms (e.g., 01:23:45:67:89:ab).

Not Regex Rule

Must NOT match the provided regex.

Same Field

Must equal the other field’s first value (string compare).

Starts With Rule

Must start with at least one of the provided prefixes.

Uppercase

Must be all uppercase.

ULID

Valid 26-char Crockford Base32; first char not 0.

Driver

coming soon...

Files

coming soon...


Custom Rules

Custom rules allow you define your own rules on for your form element. As discussed above, all validation is done against []string which is golangs default way of parsing form data.

Your custom rule must return a boolean and string. Boolean indicates if the validation passed (true == pass), and the string is the message to display on a failure.

Example

go
.Rules(
    rules.Custom[models.User](
        func(val []string, ctx *context.FieldContext[models.User]) (bool, string) {
            if model, err := ctx.GetModel(); err != nil && model.Name != "Joe Smith" {
                return false, "Need to be Joe Smith"
            }
            return true, ""
        },
    ),
)

Released under the MIT License.