Расширенный режим
Расширенный режим подразумевает более детальную работу с опцией атрибута.
Опция required
input
ruby
input :first_name,
type: String,
required: {
is: true,
message: "Input `first_name` is required"
}
INFO
До версии 2.6.0
вместо service:
использовалось service_class_name:
. В релизе 2.6.0
этот атрибут был заменен на service:
, который является объектом с подготовленными данными.
ruby
input :first_name,
type: String,
required: {
message: lambda do |service:, input:, value:|
"Input `first_name` is required"
end
}
Опция inclusion
input internal (^2.2.0) output (^2.2.0)
ruby
input :event_name,
type: String,
inclusion: {
in: %w[created rejected approved]
}
ruby
internal :event_name,
type: String,
inclusion: {
in: %w[created rejected approved]
}
ruby
output :event_name,
type: String,
inclusion: {
in: %w[created rejected approved]
}
INFO
До версии 2.6.0
вместо service:
использовалось service_class_name:
. В релизе 2.6.0
этот атрибут был заменен на service:
, который является объектом с подготовленными данными.
ruby
input :event_name,
type: String,
inclusion: {
in: %w[created rejected approved],
message: lambda do |service:, input:, value:|
value.present? ? "Incorrect `#{input.name}` specified: `#{value}`" : "Event name not specified"
end
}
ruby
internal :event_name,
type: String,
inclusion: {
in: %w[created rejected approved],
message: lambda do |service:, internal:, value:|
value.present? ? "Incorrect `#{internal.name}` specified: `#{value}`" : "Event name not specified"
end
}
ruby
output :event_name,
type: String,
inclusion: {
in: %w[created rejected approved],
message: lambda do |service:, output:, value:|
value.present? ? "Incorrect `#{output.name}` specified: `#{value}`" : "Event name not specified"
end
}
Опция consists_of
input (^2.0.0) internal (^2.0.0) output (^2.0.0)
INFO
Начиная с версии 2.6.0
эта опция является динамической.
ruby
input :ids,
type: Array,
consists_of: {
type: String,
message: "ID can only be of String type"
}
ruby
internal :ids,
type: Array,
consists_of: {
type: String,
message: "ID can only be of String type"
}
ruby
output :ids,
type: Array,
consists_of: {
type: String,
message: "ID can only be of String type"
}
ruby
input :ids,
type: Array,
# Тип элемента массива по умолчанию — String
consists_of: {
message: "ID can only be of String type"
}
ruby
internal :ids,
type: Array,
# Тип элемента массива по умолчанию — String
consists_of: {
message: "ID can only be of String type"
}
ruby
output :ids,
type: Array,
# Тип элемента массива по умолчанию — String
consists_of: {
message: "ID can only be of String type"
}
Опция schema
input (^2.0.0) internal (^2.0.0) output (^2.0.0)
ruby
input :payload,
type: Hash,
schema: {
is: {
request_id: { type: String, required: true },
# ...
},
message: "Problem with the value in the schema"
}
ruby
internal :payload,
type: Hash,
schema: {
is: {
request_id: { type: String, required: true },
# ...
},
message: "Problem with the value in the schema"
}
ruby
output :payload,
type: Hash,
schema: {
is: {
request_id: { type: String, required: true },
# ...
},
message: "Problem with the value in the schema"
}
ruby
input :payload,
type: Hash,
schema: {
is: {
request_id: { type: String, required: true },
# ...
},
message: lambda do |input_name:, key_name:, expected_type:, given_type:|
"Problem with the value in the `#{input_name}` schema: " \
"`#{key_name}` has `#{given_type}` instead of `#{expected_type}`"
end
}
ruby
internal :payload,
type: Hash,
schema: {
is: {
request_id: { type: String, required: true },
# ...
},
message: lambda do |input_name:, key_name:, expected_type:, given_type:|
"Problem with the value in the `#{input_name}` schema: " \
"`#{key_name}` has `#{given_type}` instead of `#{expected_type}`"
end
}
ruby
output :payload,
type: Hash,
schema: {
is: {
request_id: { type: String, required: true },
# ...
},
message: lambda do |input_name:, key_name:, expected_type:, given_type:|
"Problem with the value in the `#{input_name}` schema: " \
"`#{key_name}` has `#{given_type}` instead of `#{expected_type}`"
end
}
Опция must
input internal (^2.2.0) output (^2.2.0)
INFO
Опция must
может работать только в расширенном режиме.
ruby
input :invoice_numbers,
type: Array,
consists_of: String,
must: {
be_6_characters: {
is: ->(value:, input:) { value.all? { |id| id.size == 6 } }
}
}
ruby
internal :invoice_numbers,
type: Array,
consists_of: String,
must: {
be_6_characters: {
is: ->(value:, internal:) { value.all? { |id| id.size == 6 } }
}
}
ruby
output :invoice_numbers,
type: Array,
consists_of: String,
must: {
be_6_characters: {
is: ->(value:, output:) { value.all? { |id| id.size == 6 } }
}
}
INFO
До версии 2.6.0
вместо service:
использовалось service_class_name:
. В релизе 2.6.0
этот атрибут был заменен на service:
, который является объектом с подготовленными данными.
ruby
input :invoice_numbers,
type: Array,
consists_of: String,
must: {
be_6_characters: {
is: ->(value:, input:) { value.all? { |id| id.size == 6 } },
message: lambda do |service:, input:, value:, code:|
"Wrong IDs in `#{input.name}`"
end
}
}
ruby
internal :invoice_numbers,
type: Array,
consists_of: String,
must: {
be_6_characters: {
is: ->(value:, internal:) { value.all? { |id| id.size == 6 } },
message: lambda do |service:, internal:, value:, code:, reason:|
"Wrong IDs in `#{internal.name}`"
end
}
}
ruby
output :invoice_numbers,
type: Array,
consists_of: String,
must: {
be_6_characters: {
is: ->(value:, output:) { value.all? { |id| id.size == 6 } },
message: lambda do |service:, output:, value:, code:|
"Wrong IDs in `#{output.name}`"
end
}
}