アトリビュートでのオプションの使い方
オプションtype input internal output
バリデーションオプションです。渡された値が指定された型(クラス)に一致するかをis_a?で検証します。
必須です。1つまたは複数のクラスを指定できます。
class NotificationsService::Create < ApplicationService::Base
input :user, type: User
input :need_to_notify, type: [TrueClass, FalseClass]
# ...
endclass NotificationsService::Create < ApplicationService::Base
# ...
internal :inviter, type: User
# ...
endclass NotificationsService::Create < ApplicationService::Base
# ...
output :notification, type: Notification
# ...
endオプションrequired input
バリデーションオプションです。渡された値が空でないことをpresent?で検証します。
デフォルトはtrueです。
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
バリデーション以外のオプションです。サービスに値が渡されなかった場合に値を割り当てます。
class UsersService::Create < ApplicationService::Base
# ...
input :middle_name,
type: String,
required: false,
default: "<unknown>"
# ...
endオプションas input
バリデーション以外のオプションです。サービス内部でのアトリビュートのエイリアスを指定します。元の名前はサービス内部で使用できなくなります。
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?で検証します。
class EventsService::Send < ApplicationService::Base
input :event_name,
type: String,
inclusion: %w[created rejected approved]
# ...
endclass EventsService::Send < ApplicationService::Base
# ...
internal :event_name,
type: String,
inclusion: %w[created rejected approved]
# ...
endclass 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です。
input :ids,
type: Array,
consists_of: Stringinternal :ids,
type: Array,
consists_of: Stringoutput :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コンフィグレーションでカスタム型を追加できます。
任意です。指定しない場合、バリデーションはスキップされます。デフォルト値はありません。
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 }
}
}
}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 }
}
}
}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 }
}
}
}期待される各ハッシュキーを以下の形式で記述してください:
{
request_id: { type: String, required: true }
}許可されるオプション: 必須のtype、required、および任意のdefault、prepare。defaultとprepareオプションはinput内でのみ使用可能です。
typeにHashを指定した場合、同じ形式でネストを記述してください。
オプションmust input internal (^2.2.0) output (^2.2.0)
バリデーションオプションです。カスタムバリデーションを作成します。
3.0.0以降
isラムダは真値ではなく、正確にtrueを返す必要があります。1、"string"、[]などの値はバリデーションに失敗します。
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 } }
}
}
# ...
endclass 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 } }
}
}
# ...
endclass 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アクションで適用する方がよいです。
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