Skip to content

Extract validators from App State in AsyncValidatedJson#104

Open
Tuntii wants to merge 2 commits intomainfrom
extract-validators-from-state-778271158241831590
Open

Extract validators from App State in AsyncValidatedJson#104
Tuntii wants to merge 2 commits intomainfrom
extract-validators-from-state-778271158241831590

Conversation

@Tuntii
Copy link
Owner

@Tuntii Tuntii commented Feb 6, 2026

Updated AsyncValidatedJson extractor to look for ValidationContext in request state. Enabled Clone on ValidationContext to allow extraction. Added test case verifying context extraction works with async validators.


PR created automatically by Jules for task 778271158241831590 started by @Tuntii

Co-authored-by: Tuntii <121901995+Tuntii@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 6, 2026 03:47
@google-labs-jules
Copy link
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Clone derive to ValidationContext and ValidationContextBuilder to enable extraction from request state
  • Updated AsyncValidatedJson::from_request to extract ValidationContext from 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

Comment on lines +382 to +386
let ctx = if let Some(ctx) = req.state().get::<ValidationContext>() {
ctx.clone()
} else {
ValidationContext::default()
};
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines 1754 to 1770
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()
}
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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()
}

Copilot uses AI. Check for mistakes.
Comment on lines 1754 to 1770
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()
}
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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()
}

Copilot uses AI. Check for mistakes.
Co-authored-by: Tuntii <121901995+Tuntii@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant