Crate rustapi_validate

Crate rustapi_validate 

Source
Expand description

§RustAPI Validation

Validation system for RustAPI framework. Provides declarative validation on structs using the #[derive(Validate)] macro.

§Example

use rustapi_validate::prelude::*;
use validator::Validate;

#[derive(Validate)]
struct CreateUser {
    #[validate(email)]
    email: String,
     
    #[validate(length(min = 3, max = 50))]
    username: String,
     
    #[validate(range(min = 18, max = 120))]
    age: u8,
}

§Validation Rules

  • email - Validates email format
  • length(min = X, max = Y) - String length validation
  • range(min = X, max = Y) - Numeric range validation
  • regex = "..." - Regex pattern validation
  • non_empty - Non-empty string/collection validation
  • nested - Validates nested structs

§Error Format

Validation errors return a 422 Unprocessable Entity with JSON:

{
  "error": {
    "type": "validation_error",
    "message": "Validation failed",
    "fields": [
      {"field": "email", "code": "email", "message": "Invalid email format"},
      {"field": "age", "code": "range", "message": "Value must be between 18 and 120"}
    ]
  }
}

Modules§

prelude
Prelude module for validation

Structs§

FieldError
A single field validation error.
ValidationError
Validation error containing all field errors.

Traits§

Validate
Trait for validatable types.
ValidatorValidate
This is the original trait that was implemented by deriving Validate. It will still be implemented for struct validations that don’t take custom arguments. The call is being forwarded to the ValidateArgs<'v_a> trait.

Derive Macros§

ValidatorValidate