You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, wanted to talk about the plans for a validation feature. Saw a closed PR #387 which had some good ideas but needs some discussion first.
The serde_valid crate is a good reference for rust validation. Being built around json schema does constrain it's validation methods and output so I don't think its the best choice to build off directly but is a helpful example of how a topcoat library could be built.
The usage in serde_valid and #387 both use a Validate trait applied to structs, with serde_valid also providing proc-macros.
For serde_valid the Validate trait is sync only while #387 has sync plus async variant to do extra validation like db lockups. Looking at other validation features from different frameworks most are only focusing on sync validation using the input data. Keeping validation simple with only the sync option seems like a reasonable place to start and could be all that is needed.
Accessing validated data in handlers is done well in #387, by creating Validated<T> extractor that wires Validate into the request pipeline makes it easy to use.
Example for API routes where you want the route handler render errors explicitly you could use the extractor. This would send HTTP 422 with json representation of the errors.
serde_valid generates errors into json_schema output, while #387 uses a generic json error format. I think a better fit for the default error response to be Problem Details for HTTP APIs which is used by some of the major frameworks.
To implement the validation checks at a minimum all that's required is plain functions but I think the framework should consider future goals for OpenAPI export or other schema exports that could be tied into front end validation, so the checks should include all the meta data to describe them.
Rules could be created something like this:
pubtraitRule<V>{/// Checks `value`, returning `true` when it is valid.fncheck(&self,value:&V) -> bool;/// Returns the stable machine code used in error responses.fncode(&self) -> &'staticstr;/// Returns the human-readable message for a failure on `field`.fnmessage(&self,field:&str) -> String;/// Returns the constraint as schema keywords for exporters.fnmeta(&self) -> Vec<RuleMeta>;/// Returns whether this rule marks its field as required in object schemas.fnmarks_required(&self) -> bool{false}}implRule<V>forEmail{fncheck(&self,value:&V) -> bool{
rules::email(value.as_ref())}fncode(&self) -> &'staticstr{CODE_INVALID_EMAIL// "invalid_email"}fnmessage(&self,field:&str) -> String{format!("{field} is invalid")}fnmeta(&self) -> Vec<RuleMeta>{vec![RuleMeta::Format("email")]}}
What are your thoughts on this so far?
I would be happy to continue fleshing this out and share a draft PR if this seems like the right direction.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Hi, wanted to talk about the plans for a validation feature. Saw a closed PR #387 which had some good ideas but needs some discussion first.
The serde_valid crate is a good reference for rust validation. Being built around json schema does constrain it's validation methods and output so I don't think its the best choice to build off directly but is a helpful example of how a topcoat library could be built.
The usage in serde_valid and #387 both use a
Validatetrait applied to structs, with serde_valid also providing proc-macros.Making example usage:
For serde_valid the Validate trait is sync only while #387 has sync plus async variant to do extra validation like db lockups. Looking at other validation features from different frameworks most are only focusing on sync validation using the input data. Keeping validation simple with only the sync option seems like a reasonable place to start and could be all that is needed.
Accessing validated data in handlers is done well in #387, by creating
Validated<T>extractor that wiresValidateinto the request pipeline makes it easy to use.Example for API routes where you want the route handler render errors explicitly you could use the extractor. This would send HTTP 422 with json representation of the errors.
For html forms you could call validate and get the errors to render your form with the error state.
serde_valid generates errors into json_schema output, while #387 uses a generic json error format. I think a better fit for the default error response to be Problem Details for HTTP APIs which is used by some of the major frameworks.
To implement the validation checks at a minimum all that's required is plain functions but I think the framework should consider future goals for OpenAPI export or other schema exports that could be tied into front end validation, so the checks should include all the meta data to describe them.
Rules could be created something like this:
What are your thoughts on this so far?
I would be happy to continue fleshing this out and share a draft PR if this seems like the right direction.
All reactions