Skip to main content

Module validation

Module validation 

Source
Expand description

Generic entity validation — composable field rules with typed errors.

Generated services get a type alias:

ⓘ
// Generated:
pub type StoredFileValidator = EntityValidator<StoredFile>;

Custom decorators then add field rules without touching generated code:

ⓘ
// Custom:
impl StoredFileValidator {
    pub fn with_business_rules() -> Self {
        EntityValidator::new()
            .rule(RequiredString::new("name", |e: &StoredFile| &e.name))
            .rule(MaxLength::new("name", |e: &StoredFile| &e.name, 255))
            .rule(NonNegative::new("size_bytes", |e: &StoredFile| e.size_bytes))
    }
}

Structs§

EntityValidator
Composable validator for any entity type E.
MaxLength
Fails if a string field exceeds max_len Unicode scalar values.
NonNegative
Fails if a numeric field is negative.
OptionalNotBlank
Fails if an Option<String> field is Some("") or Some(" ").
Regex
Fails if a string field doesn’t match the given regex pattern.
RequiredString
Fails if a string field is empty or whitespace-only.
RequiredUuid
Fails if a UUID string field is empty or not a valid v4 UUID.
ValidationError
A single field-level validation failure.
ValidationErrors
All validation errors collected from a single validate() call.

Traits§

FieldRule
A single validation rule applied to an entity.