innoave / valid Goto Github PK
View Code? Open in Web Editor NEWcomposable validation functions for custom types
License: Other
composable validation functions for custom types
License: Other
Currently the constraints Contains
and Length
are implemented for Vec
, HashSet
and HashMap
. Implement these constraints for (all) other types from the std::collections
module the std-lib as well, such as
I'm following the docs and trying to call context.into to get the actual state object. But I just get unknown. Was I supposed to impl Into for my State type? Your docs don't seem to indicate that.
pub struct StreamConstraint;
pub struct StreamState;
impl StreamState {
pub fn is_stream(val: Option<String>) -> bool {
if let None = val {
return false;
}
let stream = JsValue::from(val.clone());
if stream.is_null() || !stream.is_object() {
return false;
}
let emitter_result = serde_json::from_str::<EventEmitter>(val.unwrap().as_str());
match emitter_result {
Ok(_) => true,
_ => false
}
}
}
impl<'a> Validate<StreamConstraint, State<&'a StreamState>> for String {
fn validate(self, context: impl Into<State<&'a StreamState>>, constraint: &StreamConstraint) -> Validation<StreamConstraint, Self> {
if context.into().is_stream(Some(&self)) {
return Validation::success(self);
}
Validation::failure(vec![invalid_state("invalid-stream", vec![])])
}
}
Currently the closures in the methods Validation::and_then
and Validation::map` get passed nested tuples which can become really ugly with more and more arguments.
We should find a way to flatten the values collected so far by the Validation
type, so that the closures either get a list of arguments passed or one flat tuple with all the values.
This will be a breaking change in the API, but I think the advantage outweighs any pain with changing existing implementations.
The Parameter
struct used in constraint violations for the State
context is not public outside of the crate.
The name of the constraint FromTo
is very ugly. It should be renamed to something more meaningful. The suggested name is MustDefineRange
because it says exactly what the constraint is about.
The current implementation of Validate
for the constraints Length
and CharCount
might panic. The limits are of type u32
and to perform the checks the limit values are casted to usize
. If an actual value does not fit into usize it will panic. This might be the case on 16bit or 8bit targets.
How can I customize the default message?
I want it to come out in another language
When validating a value for the constraint CharCount::MinMax
that has to few characters the error code in the resulting constraint violation is "invalid-length-min" but should be "invalid-char-count-min".
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.