Skip to content

Commit f643795

Browse files
committed
Update README to include examples for nested objects and primitive arrays in API parameter validation
1 parent 2b50657 commit f643795

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
3641
class 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
5461
end
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

README_ja.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
3641
class 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 }

0 commit comments

Comments
 (0)