Skip to content

Commit 786ba4e

Browse files
committed
Add raw input validation for age attribute in UserParams
1 parent fe38d30 commit 786ba4e

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

docs/validation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Use `validates_raw` to validate the original input value before type casting.
3232
class UserParams < StructuredParams::Params
3333
attribute :age, :integer
3434

35+
# Validate raw input before type casting to avoid accepting partially numeric strings (e.g. "12x").
3536
validates_raw :age, format: { with: /\A\d+\z/, message: 'must be numeric string' }
3637
end
3738

sig/structured_params/params.rbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ module StructuredParams
1010
# Strong Parameters example (API):
1111
# class UserParams < StructuredParams::Params
1212
# attribute :name, :string
13+
# attribute :age, :integer
1314
# attribute :address, :object, value_class: AddressParams
1415
# attribute :hobbies, :array, value_class: HobbyParams
1516
# attribute :tags, :array, value_type: :string
17+
#
18+
# # Validate raw input before type casting (e.g., "12x" for integer fields)
19+
# validates_raw :age, format: { with: /\A\d+\z/, message: 'must be numeric string' }
1620
# end
1721
#
1822
# # In controller:

0 commit comments

Comments
 (0)