Расширенный режим
Расширенный режим подразумевает более детальную работу с опцией атрибута.
Опция required input
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:, который является объектом с подготовленными данными.
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)
INFO
Начиная с версии 2.12.0 эта опция является динамической.
input :event_name,
type: String,
inclusion: {
in: %w[created rejected approved]
}internal :event_name,
type: String,
inclusion: {
in: %w[created rejected approved]
}output :event_name,
type: String,
inclusion: {
in: %w[created rejected approved]
}INFO
До версии 2.6.0 вместо service: использовалось service_class_name:. В релизе 2.6.0 этот атрибут был заменен на service:, который является объектом с подготовленными данными.
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
}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
}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 эта опция является динамической.
input :ids,
type: Array,
consists_of: {
type: String,
message: "ID can only be of String type"
}internal :ids,
type: Array,
consists_of: {
type: String,
message: "ID can only be of String type"
}output :ids,
type: Array,
consists_of: {
type: String,
message: "ID can only be of String type"
}input :ids,
type: Array,
# Тип элемента массива по умолчанию — String
consists_of: {
message: "ID can only be of String type"
}internal :ids,
type: Array,
# Тип элемента массива по умолчанию — String
consists_of: {
message: "ID can only be of String type"
}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)
INFO
Начиная с версии 2.12.0 эта опция является динамической.
input :payload,
type: Hash,
schema: {
is: {
request_id: { type: String, required: true },
# ...
},
message: "Problem with the value in the schema"
}internal :payload,
type: Hash,
schema: {
is: {
request_id: { type: String, required: true },
# ...
},
message: "Problem with the value in the schema"
}output :payload,
type: Hash,
schema: {
is: {
request_id: { type: String, required: true },
# ...
},
message: "Problem with the value in the schema"
}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
}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
}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 может работать только в расширенном режиме.
input :invoice_numbers,
type: Array,
consists_of: String,
must: {
be_6_characters: {
is: ->(value:, input:) { value.all? { |id| id.size == 6 } }
}
}internal :invoice_numbers,
type: Array,
consists_of: String,
must: {
be_6_characters: {
is: ->(value:, internal:) { value.all? { |id| id.size == 6 } }
}
}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:, который является объектом с подготовленными данными.
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
}
}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
}
}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
}
}