Extract validators from App State in AsyncValidatedJson#104
Extract validators from App State in AsyncValidatedJson#104
Conversation
Co-authored-by: Tuntii <121901995+Tuntii@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR enables the AsyncValidatedJson extractor to extract validators from application state, completing the implementation of async validation with configurable validators. The changes add Clone trait derivation to ValidationContext and ValidationContextBuilder, update the extraction logic to check for validators in app state, and include a comprehensive test case.
Changes:
- Added
Clonederive toValidationContextandValidationContextBuilderto enable extraction from request state - Updated
AsyncValidatedJson::from_requestto extractValidationContextfrom app state with fallback to default - Added comprehensive test coverage for async validation with state-provided validators
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| crates/rustapi-validate/src/v2/context.rs | Added Clone derivation to ValidationContext and ValidationContextBuilder to enable cheap cloning via Arc |
| crates/rustapi-core/src/extract.rs | Updated AsyncValidatedJson to extract validators from app state, removed TODO comment, and added comprehensive test |
| crates/rustapi-core/Cargo.toml | Added async-trait to dev-dependencies for test support |
| let ctx = if let Some(ctx) = req.state().get::<ValidationContext>() { | ||
| ctx.clone() | ||
| } else { | ||
| ValidationContext::default() | ||
| }; |
There was a problem hiding this comment.
The pattern for extracting state values in this codebase uses .cloned() on the Option rather than .clone() on the inner value. This should be:
let ctx = req.state().get::<ValidationContext>().cloned()
.unwrap_or_default();This is more idiomatic and consistent with how other extractors like State<T> (line 564) and Extension<T> (line 800) handle state extraction.
crates/rustapi-core/src/extract.rs
Outdated
| fn validate_with_group(&self, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl AsyncValidate for TestUser { | ||
| async fn validate_async_with_group(&self, ctx: &ValidationContext, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | ||
| let mut errors = rustapi_validate::v2::ValidationErrors::new(); | ||
|
|
||
| let rule = AsyncUniqueRule::new("users", "email"); | ||
| if let Err(e) = rule.validate_async(&self.email, ctx).await { | ||
| errors.add("email", e); | ||
| } | ||
|
|
||
| errors.into_result() | ||
| } |
There was a problem hiding this comment.
There is extra whitespace (multiple spaces) before the function name. The indentation should be consistent with standard Rust formatting (4 spaces for the method inside the impl block).
| fn validate_with_group(&self, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | |
| Ok(()) | |
| } | |
| } | |
| #[async_trait] | |
| impl AsyncValidate for TestUser { | |
| async fn validate_async_with_group(&self, ctx: &ValidationContext, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | |
| let mut errors = rustapi_validate::v2::ValidationErrors::new(); | |
| let rule = AsyncUniqueRule::new("users", "email"); | |
| if let Err(e) = rule.validate_async(&self.email, ctx).await { | |
| errors.add("email", e); | |
| } | |
| errors.into_result() | |
| } | |
| fn validate_with_group(&self, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | |
| Ok(()) | |
| } | |
| } | |
| #[async_trait] | |
| impl AsyncValidate for TestUser { | |
| async fn validate_async_with_group(&self, ctx: &ValidationContext, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | |
| let mut errors = rustapi_validate::v2::ValidationErrors::new(); | |
| let rule = AsyncUniqueRule::new("users", "email"); | |
| if let Err(e) = rule.validate_async(&self.email, ctx).await { | |
| errors.add("email", e); | |
| } | |
| errors.into_result() | |
| } |
crates/rustapi-core/src/extract.rs
Outdated
| fn validate_with_group(&self, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl AsyncValidate for TestUser { | ||
| async fn validate_async_with_group(&self, ctx: &ValidationContext, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | ||
| let mut errors = rustapi_validate::v2::ValidationErrors::new(); | ||
|
|
||
| let rule = AsyncUniqueRule::new("users", "email"); | ||
| if let Err(e) = rule.validate_async(&self.email, ctx).await { | ||
| errors.add("email", e); | ||
| } | ||
|
|
||
| errors.into_result() | ||
| } |
There was a problem hiding this comment.
There is extra whitespace (multiple spaces) before the async function name. The indentation should be consistent with standard Rust formatting (4 spaces for the method inside the impl block).
| fn validate_with_group(&self, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | |
| Ok(()) | |
| } | |
| } | |
| #[async_trait] | |
| impl AsyncValidate for TestUser { | |
| async fn validate_async_with_group(&self, ctx: &ValidationContext, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | |
| let mut errors = rustapi_validate::v2::ValidationErrors::new(); | |
| let rule = AsyncUniqueRule::new("users", "email"); | |
| if let Err(e) = rule.validate_async(&self.email, ctx).await { | |
| errors.add("email", e); | |
| } | |
| errors.into_result() | |
| } | |
| fn validate_with_group(&self, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | |
| Ok(()) | |
| } | |
| } | |
| #[async_trait] | |
| impl AsyncValidate for TestUser { | |
| async fn validate_async_with_group(&self, ctx: &ValidationContext, _group: rustapi_validate::v2::ValidationGroup) -> Result<(), rustapi_validate::v2::ValidationErrors> { | |
| let mut errors = rustapi_validate::v2::ValidationErrors::new(); | |
| let rule = AsyncUniqueRule::new("users", "email"); | |
| if let Err(e) = rule.validate_async(&self.email, ctx).await { | |
| errors.add("email", e); | |
| } | |
| errors.into_result() | |
| } |
Co-authored-by: Tuntii <121901995+Tuntii@users.noreply.github.com>
Updated
AsyncValidatedJsonextractor to look forValidationContextin request state. EnabledCloneonValidationContextto allow extraction. Added test case verifying context extraction works with async validators.PR created automatically by Jules for task 778271158241831590 started by @Tuntii