Skip to content

APIエンドポイントとリクエストバリデーションを実装#12

Merged
en-Barry merged 7 commits into
developfrom
feature/2605/en-Barry/api-endpoint
May 12, 2026
Merged

APIエンドポイントとリクエストバリデーションを実装#12
en-Barry merged 7 commits into
developfrom
feature/2605/en-Barry/api-endpoint

Conversation

@en-Barry
Copy link
Copy Markdown
Owner

@en-Barry en-Barry commented May 6, 2026

要約

Issue #7GET /api/v1/electricity_bill_simulations エンドポイントの TDD 実装)を解決する。

  • ElectricityBillSimulationParams(Form Object)でリクエストパラメータの検証と JSON:API エラー配列生成を担い、コントローラは薄く保つ
  • i18n は ActiveModel 標準機構(activemodel.errors.models.*)で ja.yml に一元管理

Closes #7

実装してみて発生した追加判断

  • ActiveModel::Attributes を使わず attr_writer + reader override を採用: attribute :kwh, :integer を使うと "100.5"100 に黙示切り捨てされ「小数を弾く」テストが失敗する。cast_to_integerInteger("100.5")ArgumentError を捕捉し生値を返すことで、numericality:not_an_integer を立てる設計にした
  • VALID_AMPERES / MAX_KWHElectricityBillConstants モジュールに切り出し: 同じ定数が ElectricityBillSimulationParamsElectricityBillSimulator に重複・名称不統一で定義されており、片方だけ変更したときの不整合を防ぐため app/models/concerns/electricity_bill_constants.rb に一元化した

テストケース一覧

spec/forms/electricity_bill_simulation_params_spec.rb(14 examples)

ケース 入力 期待
有効値(ampere=30)は valid ampere=30, kwh=400 valid
上限ぎりぎり kwh=9999 は valid ampere=30, kwh=9999 valid
AMPERE_VALUES 外(25)は inclusion エラー ampere=25, kwh=400 errors[:ampere]
ampere 欠落は blank エラー ampere=nil, kwh=400 errors[:ampere]
ampere が非数値文字列("abc")は inclusion エラー ampere=abc, kwh=400 errors[:ampere]
kwh 欠落は blank エラー ampere=30, kwh=nil errors[:kwh]
負の kwh(-1)は greater_than_or_equal_to エラー ampere=30, kwh=-1 errors[:kwh]
小数("100.5")は not_an_integer エラー ampere=30, kwh=100.5 errors[:kwh]
上限超過(10000)は less_than_or_equal_to エラー ampere=30, kwh=10000 errors[:kwh]
非数値文字列("abc")は not_a_number エラー ampere=30, kwh=abc errors[:kwh]
両方不正は 2 件のエラー ampere=25, kwh=-1 errors 2件
両方欠落は 2 件のエラー ampere=nil, kwh=nil errors 2件
jsonapi_errors が正しいキー構造を返す ampere=25 status/title/detail/source.parameter
jsonapi_errors が全件返す ampere=nil, kwh=nil 2件

spec/requests/api/v1/electricity_bill_simulations_spec.rb(3 examples)

ケース パラメータ 期待
正常系 ampere=30&kwh=400 status 200 / data 配列に provider_name / plan_name / price
単一エラー ampere=25&kwh=400 status 400 / errors[0] が JSON:API 形式
パラメータなし (なし) status 400 / errors が 2 件

@en-Barry en-Barry linked an issue May 7, 2026 that may be closed by this pull request
@en-Barry en-Barry changed the title feat: APIエンドポイントとリクエストバリデーションを実装 APIエンドポイントとリクエストバリデーションを実装 May 8, 2026
@en-Barry en-Barry linked an issue May 8, 2026 that may be closed by this pull request
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/electricity-bill-simulator branch from a640b2c to 8169e8d Compare May 11, 2026 05:01
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from 6b21fba to d0dde37 Compare May 11, 2026 05:01
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/electricity-bill-simulator branch from 8169e8d to 64807d7 Compare May 11, 2026 05:57
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from d0dde37 to 118f057 Compare May 11, 2026 05:57
Repository owner deleted a comment from vercel Bot May 11, 2026
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/electricity-bill-simulator branch from 64807d7 to ba0c72a Compare May 11, 2026 08:56
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from 118f057 to df370f2 Compare May 11, 2026 08:57
Repository owner deleted a comment from vercel Bot May 11, 2026
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/electricity-bill-simulator branch from ba0c72a to 8a9a3cd Compare May 11, 2026 10:03
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from df370f2 to 12ed79a Compare May 11, 2026 10:06
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/electricity-bill-simulator branch from 8a9a3cd to ce9f7d0 Compare May 12, 2026 06:06
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from 12ed79a to 8270ee7 Compare May 12, 2026 06:08
Base automatically changed from feature/2605/en-Barry/electricity-bill-simulator to develop May 12, 2026 10:29
@en-Barry en-Barry force-pushed the feature/2605/en-Barry/api-endpoint branch from 8270ee7 to 6f996eb Compare May 12, 2026 10:36
@en-Barry en-Barry merged commit cede476 into develop May 12, 2026
@en-Barry en-Barry deleted the feature/2605/en-Barry/api-endpoint branch May 12, 2026 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

コントローラ + リクエストスペック(TDD) 電気料金シミュレーションAPI実装(PR1: 基本要件)

1 participant