Skip to content

アトリビュートでのオプションの使い方

オプションtype input internal output

バリデーションオプションです。渡された値が指定された型(クラス)に一致するかをis_a?で検証します。

必須です。1つまたは複数のクラスを指定できます。

ruby
class NotificationsService::Create < ApplicationService::Base
  input :user, type: User
  input :need_to_notify, type: [TrueClass, FalseClass]

  # ...
end
ruby
class NotificationsService::Create < ApplicationService::Base
  # ...

  internal :inviter, type: User

  # ...
end
ruby
class NotificationsService::Create < ApplicationService::Base
  # ...

  output :notification, type: Notification

  # ...
end

オプションrequired input

バリデーションオプションです。渡された値が空でないことをpresent?で検証します。

デフォルトはtrueです。

ruby
class UsersService::Create < ApplicationService::Base
  input :first_name,
        type: String

  input :middle_name,
        type: String,
        required: false

  input :last_name,
        type: String

  # ...
end

オプションdefault input

バリデーション以外のオプションです。サービスに値が渡されなかった場合に値を割り当てます。

ruby
class UsersService::Create < ApplicationService::Base
  # ...

  input :middle_name,
        type: String,
        required: false,
        default: "<unknown>"

  # ...
end

オプションas input

バリデーション以外のオプションです。サービス内部でのアトリビュートのエイリアスを指定します。元の名前はサービス内部で使用できなくなります。

ruby
class NotificationsService::Create < ApplicationService::Base
  input :user,
        as: :recipient,
        type: User

  # ...

  def create!
    outputs.notification =
      Notification.create!(recipient: inputs.recipient)
  end
end

オプションinclusion input internal (^2.2.0) output (^2.2.0)

INFO

バージョン2.12.0以降、このオプションはダイナミックです。

ダイナミックバリデーションオプションです。渡された値が指定された配列に含まれるかをinclude?で検証します。

ruby
class EventsService::Send < ApplicationService::Base
  input :event_name,
        type: String,
        inclusion: %w[created rejected approved]

  # ...
end
ruby
class EventsService::Send < ApplicationService::Base
  # ...

  internal :event_name,
           type: String,
           inclusion: %w[created rejected approved]

  # ...
end
ruby
class EventsService::Send < ApplicationService::Base
  # ...

  output :event_name,
         type: String,
         inclusion: %w[created rejected approved]

  # ...
end

オプションconsists_of input (^2.0.0) internal (^2.0.0) output (^2.0.0)

INFO

バージョン2.6.0以降、このオプションはダイナミックです。

ダイナミックバリデーションオプションです。コレクション内の各値がネストされた値も含めて指定された型(クラス)に一致するかをis_a?で検証します。

ArrayおよびSet型でのみ動作します。collection_mode_class_namesコンフィグレーションでカスタム型を追加できます。

任意です。デフォルトはStringです。

ruby
input :ids,
      type: Array,
      consists_of: String
ruby
internal :ids,
         type: Array,
         consists_of: String
ruby
output :ids,
       type: Array,
       consists_of: String

オプションschema input (^2.0.0) internal (^2.0.0) output (^2.0.0)

INFO

バージョン2.12.0以降、このオプションはダイナミックです。

ダイナミックバリデーションオプションです。アトリビュートの値構造を記述するハッシュ値が必要です。

Hash型でのみ動作します。hash_mode_class_namesコンフィグレーションでカスタム型を追加できます。

任意です。指定しない場合、バリデーションはスキップされます。デフォルト値はありません。

ruby
input :payload,
      type: Hash,
      schema: {
        request_id: { type: String, required: true },
        user: {
          type: Hash,
          required: true,
          first_name: { type: String, required: true },
          middle_name: { type: String, required: false, default: "<unknown>" },
          last_name: { type: String, required: true },
          pass: {
            type: Hash,
            required: true,
            series: { type: String, required: true },
            number: { type: String, required: true }
          }
        }
      }
ruby
internal :payload,
         type: Hash,
         schema: {
           request_id: { type: String, required: true },
           user: {
             type: Hash,
             required: true,
             first_name: { type: String, required: true },
             middle_name: { type: String, required: false, default: "<unknown>" },
             last_name: { type: String, required: true },
             pass: {
               type: Hash,
               required: true,
               series: { type: String, required: true },
               number: { type: String, required: true }
             }
           }
         }
ruby
output :payload,
       type: Hash,
       schema: {
         request_id: { type: String, required: true },
         user: {
           type: Hash,
           required: true,
           first_name: { type: String, required: true },
           middle_name: { type: String, required: false, default: "<unknown>" },
           last_name: { type: String, required: true },
           pass: {
             type: Hash,
             required: true,
             series: { type: String, required: true },
             number: { type: String, required: true }
           }
         }
       }

期待される各ハッシュキーを以下の形式で記述してください:

ruby
{
  request_id: { type: String, required: true }
}

許可されるオプション: 必須のtyperequired、および任意のdefaultpreparedefaultprepareオプションはinput内でのみ使用可能です。

typeHashを指定した場合、同じ形式でネストを記述してください。

オプションmust input internal (^2.2.0) output (^2.2.0)

バリデーションオプションです。カスタムバリデーションを作成します。

3.0.0以降

isラムダは真値ではなく、正確にtrueを返す必要があります。1"string"[]などの値はバリデーションに失敗します。

ruby
class PaymentsService::Create < ApplicationService::Base
  input :invoice_numbers,
        type: Array,
        consists_of: String,
        must: {
          be_6_characters: {
            is: ->(value:, input:) { value.all? { |id| id.size == 6 } }
          }
        }

  # ...
end
ruby
class EventsService::Send < ApplicationService::Base
  # ...

  internal :invoice_numbers,
           type: Array,
           consists_of: String,
           must: {
             be_6_characters: {
               is: ->(value:, internal:) { value.all? { |id| id.size == 6 } }
             }
           }

  # ...
end
ruby
class EventsService::Send < ApplicationService::Base
  # ...

  output :invoice_numbers,
         type: Array,
         consists_of: String,
         must: {
           be_6_characters: {
             is: ->(value:, output:) { value.all? { |id| id.size == 6 } }
           }
         }

  # ...
end

オプションformat input (^2.4.0) internal (^2.4.0) output (^2.4.0)

ダイナミックバリデーションオプション(メインオプションには含まれません)。詳細をご覧ください。

オプションmax input (^2.4.0) internal (^2.4.0) output (^2.4.0)

ダイナミックバリデーションオプション(メインオプションには含まれません)。詳細をご覧ください。

オプションmin input (^2.4.0) internal (^2.4.0) output (^2.4.0)

ダイナミックバリデーションオプション(メインオプションには含まれません)。詳細をご覧ください。

オプションtarget input (^3.0.0) internal (^3.0.0) output (^3.0.0)

Class型のアトリビュート用のダイナミックバリデーションオプション(メインオプションには含まれません)。詳細をご覧ください。

オプションprepare input

バリデーション以外のオプションです。渡された値を準備します。

WARNING

prepareは簡単な準備処理にのみ慎重に使用してください。複雑なロジックはmakeアクションで適用する方がよいです。

ruby
class PaymentsService::Create < ApplicationService::Base
  input :amount_cents,
        as: :amount,
        type: Integer,
        prepare: ->(value:) { Money.from_cents(value, :USD) }

  # ...

  def create!
    outputs.payment = Payment.create!(amount: inputs.amount)
  end
end