Skip to content

Commit e0beea3

Browse files
authored
Merge pull request #15 from Syati/feature/nest_i18n
Enhance i18n support for nested attributes and update documentation
2 parents f874407 + f6fd2af commit e0beea3

29 files changed

+1001
-511
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ plugins:
66
AllCops:
77
NewCops: enable
88

9+
Style/OneClassPerFile:
10+
Exclude:
11+
- spec/support/test_classes/**/*
12+
913
Layout/LeadingCommentSpace:
1014
Enabled: false
1115

.rubocop_rbs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ plugins:
77
# ========= RBS ===========
88
Style/RbsInline/MissingTypeAnnotation:
99
EnforcedStyle: method_type_signature
10-
10+
Exclude:
11+
- 'spec/**/*'
1112
Style/RbsInline/UntypedInstanceVariable:
1213
Exclude:
1314
- 'lib/structured_params/params.rb'

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ end
112112
- **[Strong Parameters](docs/strong-parameters.md)** - Automatic permit list generation
113113
- **[Error Handling](docs/error-handling.md)** - Flat and structured error formats
114114
- **[Serialization](docs/serialization.md)** - Converting parameters to hashes and JSON
115-
- **[Gem Comparison](docs/comparison.md)** - Comparison with typed_params, dry-validation, and reform
116-
115+
- **[Form Objects](docs/form-objects.md)** - Form object pattern with Rails views
117116

118117
## Contributing
119118

README_ja.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,17 @@ def create
6565
end
6666
```
6767

68-
#### 型変換前の生入力をバリデーションする
68+
#### プリミティブ配列
6969

70-
ActiveModel の型変換前の入力値を検証したい場合は `validates_raw` を使います
70+
`value_type` を使うとプリミティブ型の配列を扱えます。Strong Parameters では配列フォーマット(`tags: []`)で許可されます
7171

7272
```ruby
7373
class UserParams < StructuredParams::Params
74-
attribute :age, :integer
75-
76-
validates_raw :age, format: { with: /\A\d+\z/, message: 'must be numeric string' }
74+
attribute :tags, :array, value_type: :string
7775
end
76+
77+
# 相当する Strong Parameters:
78+
# params.permit(tags: [])
7879
```
7980

8081
### 2. フォームオブジェクト
@@ -106,11 +107,11 @@ end
106107

107108
- **[インストールとセットアップ](docs/installation.md)** - StructuredParams の始め方
108109
- **[基本的な使用方法](docs/basic-usage.md)** - パラメータクラス、ネストオブジェクト、配列
109-
- **[バリデーション](docs/validation.md)** - ネスト構造でのActiveModelバリデーション使用
110-
- **[Strong Parameters](docs/strong-parameters.md)** - 自動permit リスト生成
110+
- **[バリデーション](docs/validation.md)** - ネスト構造での ActiveModel バリデーション使用
111+
- **[Strong Parameters](docs/strong-parameters.md)** - 自動 permit リスト生成
111112
- **[エラーハンドリング](docs/error-handling.md)** - フラットと構造化エラーフォーマット
112-
- **[シリアライゼーション](docs/serialization.md)** - パラメータのハッシュとJSON変換
113-
- **[Gem比較](docs/comparison.md)** - typed_params、dry-validation、reformとの比較
113+
- **[シリアライゼーション](docs/serialization.md)** - パラメータのハッシュと JSON 変換
114+
- **[フォームオブジェクト](docs/form-objects.md)** - Rails ビューとのフォームオブジェクトパターン
114115

115116

116117
## コントリビューション

docs/basic-usage.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Basic Usage
22

3+
Define a parameter class by inheriting from `StructuredParams::Params`. Declare typed attributes with `attribute` and use standard ActiveModel validations.
4+
5+
## Table of Contents
6+
7+
- [Basic Parameter Class](#basic-parameter-class)
8+
- [Nested Objects](#nested-objects)
9+
- [Arrays](#arrays)
10+
- [Array of Primitive Types](#array-of-primitive-types)
11+
- [Array of Nested Objects](#array-of-nested-objects)
12+
313
## Basic Parameter Class
414

515
```ruby
@@ -25,6 +35,8 @@ end
2535

2636
## Nested Objects
2737

38+
Use `attribute :name, :object, value_class: SomeParams` to define a nested object.
39+
2840
```ruby
2941
class AddressParams < StructuredParams::Params
3042
attribute :street, :string
@@ -48,41 +60,47 @@ params = {
4860
}
4961

5062
user_params = UserParams.new(params)
51-
user_params.address # => AddressParams instance
52-
user_params.address.city # => "New York"
63+
user_params.address # => AddressParams instance
64+
user_params.address.city # => "New York"
5365
```
5466

5567
## Arrays
5668

69+
Use `attribute :name, :array, ...` to define an array attribute. Specify `value_type` for scalar elements or `value_class` for object elements.
70+
5771
### Array of Primitive Types
5872

73+
Use `value_type` for arrays of scalar values.
74+
5975
```ruby
6076
class UserParams < StructuredParams::Params
61-
attribute :tags, :array, value_type: :string
77+
attribute :tags, :array, value_type: :string
6278
attribute :scores, :array, value_type: :integer
6379
end
6480

6581
# Usage
6682
params = {
67-
tags: ["ruby", "rails", "programming"],
83+
tags: ["ruby", "rails", "programming"],
6884
scores: [85, 92, 78]
6985
}
7086

7187
user_params = UserParams.new(params)
72-
user_params.tags # => ["ruby", "rails", "programming"]
88+
user_params.tags # => ["ruby", "rails", "programming"]
7389
user_params.scores # => [85, 92, 78]
7490
```
7591

7692
### Array of Nested Objects
7793

94+
Use `value_class` for arrays of objects.
95+
7896
```ruby
7997
class HobbyParams < StructuredParams::Params
80-
attribute :name, :string
98+
attribute :name, :string
8199
attribute :level, :string
82100
end
83101

84102
class UserParams < StructuredParams::Params
85-
attribute :name, :string
103+
attribute :name, :string
86104
attribute :hobbies, :array, value_class: HobbyParams
87105
end
88106

@@ -91,11 +109,11 @@ params = {
91109
name: "Alice",
92110
hobbies: [
93111
{ name: "Photography", level: "beginner" },
94-
{ name: "Cooking", level: "intermediate" }
112+
{ name: "Cooking", level: "intermediate" }
95113
]
96114
}
97115

98116
user_params = UserParams.new(params)
99-
user_params.hobbies # => [HobbyParams, HobbyParams]
100-
user_params.hobbies.first.name # => "Photography"
117+
user_params.hobbies # => [HobbyParams, HobbyParams]
118+
user_params.hobbies.first.name # => "Photography"
101119
```

0 commit comments

Comments
 (0)