Instantiate validators at definition time#2657
Draft
ericproulx wants to merge 1 commit intomasterfrom
Draft
Conversation
Danger ReportNo issues found. |
5d145a5 to
f87920f
Compare
Contributor
Author
|
Missing UPGRADING notes. Working on it |
2ecb403 to
cf04c9d
Compare
Member
|
Is there a tradeoff with things like |
Contributor
Author
e16efcf to
0b9e34b
Compare
a503556 to
2b16ba1
Compare
b744723 to
30b09ea
Compare
52b7862 to
adac9ed
Compare
2a39a4d to
2d51b32
Compare
Validators are now instantiated once at route definition time rather than per-request, eliminating repeated allocation overhead. Instances are frozen to make them safe for sharing across requests. Freezing strategy: inputs (attrs, options, opts) are frozen at the DSL boundary before entering the validator, so subclass ivars derived from them are frozen by construction. Base.new reduces to super.freeze. Remove freeze_state! and ValidatorFactory. ParamsScope: precompute full_path via build_full_path before instance_eval so child scopes can read the parent path immediately. Simplify meets_hash_dependency? with all? and dependency.first. Validators::Base: add validation_error! helper to replace repeated Grape::Exceptions::Validation.new calls across single-attr validators. Fix DefaultValidator to always dup duplicable default values regardless of frozen state, preserving per-request isolation. Add DeepFreeze utility (freezes Hash/Array/String recursively, skips Procs, coercers, and classes). Add specs for DeepFreeze, SameAsValidator, and ExceptValuesValidator.
2d51b32 to
177506d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Validators are now instantiated once at route definition time rather than per-request via `ValidatorFactory`. This eliminates repeated object allocation on every request and moves expensive setup (option parsing, converter building, message formatting) out of the hot path.
Because validator instances are shared across all concurrent requests they are frozen after initialization, enforced by a `Validators::Base.new` override that calls `super.freeze`. All inputs (`options`, `opts`, `attrs`) arrive pre-frozen from the DSL boundary via `DeepFreeze` and `Array#freeze`, so subclass ivars derived from them are frozen by construction. A new `Grape::Util::DeepFreeze` helper recursively freezes Hash/Array/String values while intentionally leaving Procs, coercers, Classes, and other mutable objects unfrozen.
Changes
Core
`Validators::Base`
`Grape::Util::DeepFreeze`
Validator-level eager initialization
Specs
Test plan