Skip to main content

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,
}

§V2 Validation Engine

The v2 module provides a custom validation engine with async support:

use rustapi_validate::v2::prelude::*;

#[derive(Validate)]
struct CreateUser {
    #[validate(email, message = "Invalid email format")]
    email: String,
     
    #[validate(length(min = 3, max = 50))]
    username: String,
     
    #[validate(async_unique(table = "users", column = "email"))]
    unique_email: String,
}

§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
  • url - URL format validation
  • required - Non-empty string/option validation
  • async_unique(table, column) - Database uniqueness check
  • async_exists(table, column) - Database existence check
  • async_api(endpoint) - External API validation

§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"}
    ]
  }
}

Re-exports§

pub use v2::Validate;

Modules§

custom
prelude
Prelude module for validation
v2
V2 validation engine with async support.

Structs§

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

Derive Macros§

DeriveValidate
Derive macro for implementing Validate and AsyncValidate traits