domainstack-derive
Derive macros for the domainstack full-stack validation ecosystem.
Provides two derive macros that share the same unified rich syntax:
#[derive(Validate)]- Runtime validation#[derive(ToSchema)]- OpenAPI schema generation
Usage
Add this to your Cargo.toml:
[]
= { = "1.0", = ["derive"] } # Includes Validate
= "1.0" # Adds ToSchema derive
= "1.0" # Schema builder utilities
Validation Only
use Validate;
Validation + Schema Generation (Unified Syntax)
NEW: Write validation rules ONCE, get BOTH runtime validation AND OpenAPI schemas:
use ;
// Runtime validation works
user.validate?;
// Schema generation works
let schema = schema;
// → email: { type: "string", format: "email", maxLength: 255, ... }
// → age: { type: "integer", minimum: 18, maximum: 120 }
Available Attributes
Validation Rules (Unified Rich Syntax)
Both Validate and ToSchema support these validation rules:
String Rules:
#[validate(email)]- Email format#[validate(url)]- URL format#[validate(min_len = n)]- Minimum length#[validate(max_len = n)]- Maximum length#[validate(alphanumeric)]- Alphanumeric only#[validate(ascii)]- ASCII only#[validate(alpha_only)]- Letters only#[validate(numeric_string)]- Digits only#[validate(non_empty)]- Not empty#[validate(non_blank)]- Not blank (no whitespace)#[validate(matches_regex = "pattern")]- Custom regex- Plus:
contains,starts_with,ends_with,no_whitespace
Numeric Rules:
#[validate(range(min = a, max = b))]- Range validation#[validate(positive)]- Positive numbers#[validate(negative)]- Negative numbers#[validate(non_zero)]- Not zero#[validate(multiple_of = n)]- Multiple of n- Plus:
min,max,finite,equals,not_equals
Collection Rules:
#[validate(min_items = n)]- Minimum items#[validate(max_items = n)]- Maximum items#[validate(unique)]- All items unique
Composite Rules:
#[validate(nested)]- Validate nested struct#[validate(each(nested))]- Validate each item in collection (for nested types)#[validate(each(rule))]- Validate each item with any rule (for primitives)#[validate(custom = "function")]- Custom validation function
Collection Item Validation:
The each(rule) syntax allows validating each item in a collection:
Error paths include array indices: tags[0], author_emails[1], etc.
Legacy Syntax (Still Supported):
#[validate(length(min = a, max = b))]- String length (prefermin_len/max_len)
Schema Hints
For ToSchema, add documentation metadata:
Struct-Level Validation
Documentation
This is a proc macro implementation crate. For complete documentation, examples, and usage guides, see:
License
Apache 2.0