laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
//! Validation for the input that requests submit.
//!
//! Describe a request's input as a struct with rules on its fields, derive [`Validate`] for it,
//! and take [`Valid<T>`] in a handler. The handler runs only when the input passes. When it
//! fails, a JSON request gets a `422` response, and a browser form gets a redirect back with the
//! errors and old input.
//!
//! [`Rule`] lists the built-in rules, [`ValidationRule`] lets you write your own, and [`Rules`]
//! builds rules at runtime. The
//! [validation guide](https://github.com/laravel/rust-framework/blob/0.x/docs/validation.md)
//! walks through each step.
//!
//! ```no_run
//! use laravel::validation::{Valid, Validate};
//! use serde::Deserialize;
//!
//! #[derive(Deserialize, Validate)]
//! #[validate(anyone)]
//! struct NewUser {
//!     #[validate(required, max_length = 100, label = "display name",
//!         messages(required = "Please enter your :attribute."))]
//!     name: String,
//!     #[validate(required, email)]
//!     email: String,
//!     #[validate(url("https"))]
//!     website: Option<String>,
//!     #[validate(between(0.0, 5.0))]
//!     rating: Option<f64>,
//! }
//!
//! async fn store(Valid(user): Valid<NewUser>) -> String {
//!     format!("Hello, {}", user.name)
//! }
//! ```
//!
//! # Input
//!
//! [`Valid`] reads the query string and the request body, after input middleware such as
//! `TrimStrings` and `ConvertEmptyStringsToNull` have run. Rules check the submitted values before
//! serde defaults fill in missing fields, so `accepted` on a `#[serde(default)]` checkbox fails when
//! the box is unchecked. The errors list decoding errors and rule errors together, so a bad type in
//! one field doesn't hide a rule error in another. Validation stops once 1,000 fields have failed,
//! so the errors for a very long list of bad items name only the first items.
//!
//! The derive supports named structs and serde's `rename`, `rename_all`, `alias`, `default`, and
//! `skip` attributes. It recognizes `String`, `Option`, `Vec`, [`Sometimes`], and
//! [`Password`](crate::hash::Password) fields by the type name written in the struct. A field that
//! uses a type alias, `flatten`, or `deserialize_with` needs a manual [`Validate`] implementation.
//! Field names can't contain dots, because a dot means nesting in a rule path.
//!
//! Serde buffers the input for `untagged` enums and `flatten` fields, and the buffering may convert
//! numbers through floating point before a [`Decimal`](crate::numeric::Decimal) field sees them.
//! Send decimals on those paths as quoted strings.
//!
//! # Field references
//!
//! Rules that name another field, such as `required_if`, `gte`, and `after`, resolve the name
//! relative to the object that declares the rule. In a list of objects, the name stays within the
//! same entry. A dotted name reaches into a child object, and a wildcard doesn't expand.
//!
//! In `#[validate(..)]`, name fields by their Rust names. The derive resolves each name to the
//! field's input name, after serde renames, and a name that isn't a field of the struct doesn't
//! compile. [`Rules`] that you build by hand name fields by their input names.
//!
//! `required_if` does nothing when the other field is missing. The other value conditions treat a
//! missing field as null. Numeric and date comparisons fail when the other field is missing, null,
//! or malformed.
//!
//! # Old input
//!
//! When a failed validation or [`invalid!`] redirects back, or when a handler calls
//! [`Redirect::with_input`](crate::http::Redirect::with_input) or
//! [`View::with_input`](crate::view::View::with_input), the old input leaves out these values:
//!
//! - The top-level `password`, `password_confirmation`, and `current_password` fields.
//! - Uploaded files.
//! - Every `Password` field of the validated type, at any depth, and its `_confirmation` field.
//!
//! # Rules that read the request
//!
//! These rules read the request, such as its route parameters or its logged-in user:
//!
//! - `unique` with `ignore = user` or `ignore = route::<Team>("team")`.
//! - `unique` and `exists` with a `filter` that reads `route::<Project>("project")`.
//! - `current_password`.
//!
//! [`Rules::validate`] has no request, so these rules fail there with a `500`. `ignore = user` and
//! `current_password` read the session guard's user. On a route that has neither a session nor a
//! request guard, they fail with a `500` whose message names the fix.

#[cfg(feature = "database")]
mod database;

#[doc(hidden)]
pub use laravel_http::validation::invalid;
#[cfg(feature = "database")]
pub(crate) use laravel_http::validation::*;
#[doc(inline)]
pub use laravel_http::validation::{
    Field, InputObject, InputValue, IntoRules, InvalidInput, Message, Number, PasswordRule, Rule, RuleError, RuleValue,
    Rules, Sometimes, Valid, ValidRequest, Validate, ValidatesRequest, ValidationError, ValidationRule, regex,
};

/// Derives [`Validate`] from `#[validate(..)]` attributes on a named struct's fields.
///
/// A `String` field accepts only strings, and an `Option` field accepts null. The derive reads
/// serde's field names and aliases, and a field attribute may name a custom rule expression. A
/// field whose type implements `Validate` (including nested structs, `Option`, `Box`, `Vec`,
/// `HashMap`, `BTreeMap`, `IndexMap`, and type aliases of them) applies that type's rules
/// recursively. `#[laravel::input]` derives `Validate` together with `Deserialize`.
///
/// These attributes go on the struct itself:
///
/// - `#[validate(anyone)]`, `#[validate(authenticated)]`, and `#[validate(authorize(..))]`
///   implement [`ValidatesRequest`].
/// - `#[validate(form = "login")]` sets [`Validate::form`].
///
/// ```
/// use laravel::validation::Validate;
///
/// #[derive(Validate)]
/// struct Address {
///     #[validate(required)]
///     city: String,
/// }
///
/// #[derive(Validate)]
/// struct Profile {
///     #[validate(required)]
///     address: Option<Address>,
///     previous_addresses: Vec<Address>,
/// }
/// ```
///
/// `messages(..)` may also override the errors that come from decoding a field's type:
///
/// - `integer`, `numeric`, `boolean`, `string`, `character`, and `array`, for a value of the wrong
///   type.
/// - `between`, for an integer outside its type's bounds.
/// - `finite`, for a number that overflows `f32`.
/// - `max_bytes`, for a `Password` over 72 bytes.
/// - `required`, for a missing field that isn't optional.
/// - `selected`, for an unknown enum choice.
/// - `prohibited`, for an unknown field that serde rejects.
/// - `invalid`, for any other decoding failure.
///
/// When a field has both a rule error and a decoding error, the rule error is reported.
///
/// `messages(invalid = ..)` also sets the message of every check that the field's type implies,
/// unless the field overrides that check's own message. Those checks are the type errors above,
/// except `required`, `prohibited`, and `max_bytes`, and the format rules that the derive infers
/// from types such as `Uuid`, `IpAddr`, `Date`, and `Timestamp`. A rule that the field declares,
/// such as `uuid = 7`, keeps its own message.
///
/// A bare `#[validate]` requires the field's type to implement `Validate`, such as a field whose
/// type is a generic type parameter:
///
/// ```compile_fail
/// #[derive(laravel::validation::Validate)]
/// struct Profile {
///     #[validate]
///     address: Address,
/// }
/// struct Address {
///     city: String,
/// }
/// ```
pub use laravel_rust_validation_macros::Validate;

#[doc(hidden)]
#[cfg(feature = "database")]
pub use self::database::{__exists_field, __unique_field};
#[cfg(feature = "database")]
pub use self::database::{DatabaseRules, Exists, Unique};

#[doc(hidden)]
pub mod __private {
    pub use laravel_http::validation::__private::{Children, UnvalidatedChildren, ValidatedChildren, serde, value};
    pub use laravel_http::validation::Lenient;
    pub use laravel_rust_validation_macros::QueryInput;

    #[cfg(feature = "database")]
    pub use super::database::{RouteCondition, route_key};
}