Skip to content

fix: allow validators to capture external variables#79

Draft
kimjune01 wants to merge 1 commit into
ynqa:mainfrom
kimjune01:fix-validator-closure-capture
Draft

fix: allow validators to capture external variables#79
kimjune01 wants to merge 1 commit into
ynqa:mainfrom
kimjune01:fix-validator-closure-capture

Conversation

@kimjune01
Copy link
Copy Markdown

Summary

  • Change validator type from Fn(&str) -> Result<(), String> to accept FnMut, and box the closure to allow validators that capture and mutate external state
  • This enables use cases like validators that check input against a database connection or accumulate state across validations

Fixes #29

Test plan

  • Validators with captured variables compile and execute correctly
  • Existing validator functionality unchanged

Change Validator and ErrorMessageGenerator from function pointers
(fn(&T) -> bool) to boxed closures (Box<dyn Fn + Send + Sync>),
enabling validators to capture external state like file lists
or shared counters.

The validator() method signatures now accept impl Fn, so existing
code passing function pointers continues to compile without changes.
@kimjune01 kimjune01 marked this pull request as draft May 18, 2026 06:06
@kimjune01
Copy link
Copy Markdown
Author

PR lacks a test exercising the change, so drafting. Please close or take it over.

@ynqa
Copy link
Copy Markdown
Owner

ynqa commented May 18, 2026

@kimjune01 Sorry, I haven’t had a chance to review it in detail yet, but it looks like there are some test cases already. Is my understanding correct that the concern is that the existing tests are insufficient?

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.

Validator type doesn't allow capture of external variables

2 participants