#validation #iron #forms #request #laravel #params #valid

iron_valid

Request validation library for iron, based on Laravel's validation

7 releases (4 breaking)

Uses old Rust 2015

0.5.0 Mar 19, 2017
0.4.1 Feb 25, 2017
0.3.1 Nov 29, 2016
0.3.0 Oct 17, 2016
0.1.0 Jun 15, 2016

#184 in Email

MIT license

115KB
2.5K SLoC

iron_valid

Build Status Version License

Overview

iron_valid is a form validation library for iron that integrates with the params crate. It is based on Laravel's validation, and works in a similar way.

If you encounter an issue, please report it via the GitHub issues tab. Include as many details as possible.

Usage

An example of using iron_valid to validate a form request is as follows:

use iron::prelude::*;
use iron::status;
use iron_valid::{Rule, validate};
use params::{Params, Value};
use std::collections::BTreeMap;

pub fn register(req: &mut Request) -> IronResult<Response> {
    let params = match req.get::<Params>() {
        Ok(p) => p,
        Err(_) => {
            return Ok(Response::with((status::BadRequest)));
        }
    };

    let mut rules = BTreeMap::new();
    rules.insert("email", vec![Rule::Required, Rule::Email]);
    rules.insert("password", vec![Rule::Required, Rule::Confirmed, Rule::Min(8)]);

    match validate(rules, params) {
        Ok(ref values) => {
            // Save the user to the database
            let email = values.find(&["email"]).unwrap();

            // ...

            Ok(Response::with((status::Ok)))
        }
        Err(_) => Ok(Response::with((status::BadRequest))),
    }
}

This example would parse the request parameters using the params crate, then validate the email and password fields from the request using the specified rules for each.

You can also validate nested fields within a request body. For example, to validate a struct { user: { name: 'foo', email: 'a@b.com' } }, you could pass in user.name or user.email as the key on the rules passed in to validate. This also works for Rules that accept a parameter for a second field.

Full documentation, along with a list of validation rules, is available here.

iron_valid follows Semantic Versioning.

Contributing

Any contributions are welcome and will be accepted via pull request on GitHub. Bug reports can be filed via GitHub issues. If you have the capability to submit a fix with the bug report, it is preferred that you do so via pull request, however you do not need to be a Rust programmer to contribute. Other contributions (such as improving documentation or translations) are also welcome via GitHub.

License

iron_valid is open-source software, distributed under the MIT license.

Dependencies

~11MB
~225K SLoC