Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Data validation library for Rust

License

NotificationsYou must be signed in to change notification settings

baitcenter/accord

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build StatusCurrent Crates.io VersionAPI Documentationcodecov

Accord is a library for validating data according to rules likelength,contains,range andeither.

Accord is two fold, the first part being a set of validator-functions thatfor example tests that aString has a minimum of 5 characters or that ani32is either10 or20, and the second part being therules! macro which allows youto run a set of validators on a single piece of data, or a whole collection of dataand get back a set of errors which explains exactly what is wrong. The errors caneasily be serialized usingSerde and then be used in for example a REST API toreport to the user which of the data the user posted contains illegal values.

See theRocket example for how to use Accord withRocket to validate JSON inputand return explanations for any occuring error as JSON which then can beparsed by the requesting application and shown to the user to guide them inhow to fix their input values according to the applications rules.

Error messages uses numbered placeholders meaning that an error message couldbe"Must not be less than %1." with an accompanien list[5], which makesit easy to translate"Must not be less than %1." without having to deal with thevariable value5.

Usage tl;dr:

#[macro_use]externcrate accord;externcrate serde;externcrate serde_json;use accord::{Accord,ResultasAccordResult,Error,MultipleError,MultipleInvalid};use accord::validators::{length, contains, range};structAccount{pubname:String,pubemail:String,pubage:i8,}implAccordforAccount{fnvalidate(&self) ->AccordResult{rules!{"name" =>self.name =>[length(1,64)],"email" =>self.email =>[length(5,64), contains("@"), contains(".")],"age" =>self.age =>[range(12,127)]}}}fnmain(){let account =Account{name:"".to_string(),email:"test".to_string(),age:11,};// You can use the `rules!` macro on any value.// This way of using the the `rules!` macro returns a// `Result<(), Error>`.let _ =rules!(account.name,[length(1,64)]);let _ =rules!(account.email,[length(5,64), contains("@"), contains(".")]);let _ =rules!(account.age,[range(12,127)]);// You can also use the collection form of the `rules!` macro// again using any value you'd like.// This way of using the `rules!` macro returns a// `Result<(), MultipleError>`. Notice the string slices that has// been appended to the lines from last example. These string slices// are called tags and are used to distingues between the sets of errors// that are returned.let _ =rules!{"name" => account.name =>[length(1,64)],"email" => account.email =>[length(5,64), contains("@"), contains(".")],"age" => account.age =>[range(12,127)]};// And finally, since our `Account` has implemented the// `Accord` trait, we can simply do the following, which once// again returns `Result<(), MultipleError>`, which we then// serialize to JSON using Serde and print:ifletErr(multiple_error) = account.validate(){println!("Errors as json: {}",                 serde_json::to_string(&multiple_error).unwrap());}else{println!("No errors occured");}}

Documentation

  • Examples: Usage examples are available in theexamples/ directory
  • API Documentation: Documentation generated from the source code, comments and examples

Building locally

Stable

Building:cargo build

Testing:cargo test

Nightly + unstable features

Make sure you have an up-to-date version of rust nightly installed.

Building:cargo build

Testing:cargo test

You can add--features FEATURES TO ENABLE to bothcargo build andcargo test to build or test unstable features. The unstable features currently supported are:

  • inclusive_rangeRFC#1192 which enables one to use thelength andrange validators withinclusive ranges instead ofa, b-variables, as in the example above. An example crate using inclusive ranges can be foundhere.

Make

You can also usemake for doing more stuff in a simpler way. The Makefilerequires that you are using Rust viarustup.

  • make will build and test Accord and all examples on both stable and nightly, on nightly both with and without unstable features
  • make build will build everything on both stable and nightly
  • make build-stable will build everything on stable
  • make build-unstable will build everything on nightly with and without unstable features
  • make build-examples will build examples on both stable and nightly
  • make build-stable-examples
  • make build-unstable-examples
  • make build-stable-example-<NAME-OF-STABLE-EXAMPLE>
  • make build-unstable-example-<NAME-OF-UNSTABLE-EXAMPLE>
  • make test will test everything on both stable and nightly, on nightly with and without unstable features
  • make test-stable will test everything on stable
  • make test-unstable will test everything on nightly with and without unstable features
  • make test-examples will test examples on both stable and nightly
  • make test-stable-examples
  • make test-unstable-examples
  • make test-stable-example-<NAME-OF-STABLE-EXAMPLE>
  • make test-unstable-example-<NAME-OF-UNSTABLE-EXAMPLE>

Contributing

Contributions are absolutely, positively welcome and encouraged! Contributionscome in many forms. You could:

  1. Submit a feature request or bug report as anissue.
  2. Ask for improved documentation as anissue.
  3. Contribute code viapull requests.

To keep a high standard of quality, contributed code must be:

  • Commented: Public itemsmust be commented.
  • Documented: Exposed itemsmust have rustdoc comments withexamples, if applicable.
  • Styled: Your code should berustfmt'd when possible.
  • Simple: Your code should accomplish its task as simply andidiomatically as possible.
  • Tested: You must add (and pass) convincing tests for any functionality you add.
  • Focused: Your code should do what it's supposed to do and nothing more.

All pull requests are code reviewed and tested by the CI. Note that unless youexplicitly state otherwise, any contribution intentionally submitted forinclusion in Accord by you shall be MIT License without any additional terms or conditions.

Thanks toRocket for showing how to form a great contributing-section.

License

Accord is Copyright (c) 2017 Christoffer Buchholz. It is free software, andmay be redistributed under the terms specified in theLICENSE file.

About

Data validation library for Rust

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust94.3%
  • Makefile5.7%

[8]ページ先頭

©2009-2025 Movatter.jp