File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -33,9 +33,16 @@ StructuredParams.register_types
3333### 1. API Parameter Validation
3434
3535``` ruby
36+ class AddressParams < StructuredParams ::Params
37+ attribute :street , :string
38+ attribute :city , :string
39+ end
40+
3641class UserParams < StructuredParams ::Params
3742 attribute :name , :string
3843 attribute :age , :integer
44+ attribute :tags , :array , value_type: :string # Primitive array
45+ attribute :address , :object , value_class: AddressParams # Nested object
3946
4047 validates :name , presence: true
4148 validates :age , numericality: { greater_than: 0 }
@@ -54,6 +61,20 @@ def create
5461end
5562```
5663
64+ #### Primitive arrays
65+
66+ StructuredParams supports primitive arrays via ` value_type ` . They are permitted using the Strong Parameters array format (` tags: [] ` ).
67+
68+ ``` ruby
69+ class UserParams < StructuredParams ::Params
70+ attribute :tags , :array , value_type: :string
71+ end
72+
73+ # Equivalent Strong Parameters:
74+ # params.permit(tags: [])
75+ ```
76+
77+
5778### 2. Form Object
5879
5980``` ruby
Original file line number Diff line number Diff line change @@ -33,9 +33,16 @@ StructuredParams.register_types
3333### 1. API パラメータバリデーション
3434
3535``` ruby
36+ class AddressParams < StructuredParams ::Params
37+ attribute :street , :string
38+ attribute :city , :string
39+ end
40+
3641class UserParams < StructuredParams ::Params
3742 attribute :name , :string
3843 attribute :age , :integer
44+ attribute :tags , :array , value_type: :string # プリミティブ配列
45+ attribute :address , :object , value_class: AddressParams # ネストオブジェクト
3946
4047 validates :name , presence: true
4148 validates :age , numericality: { greater_than: 0 }
You can’t perform that action at this time.
0 commit comments