Skip to content

Advanced mode

Advanced mode involves more detailed work with the attribute option.

Option required input

ruby
input :first_name,
      type: String,
      required: {
        is: true,
        message: "Input `first_name` is required"
      }

INFO

Before version 2.6.0, service_class_name: was used instead of service:. In the 2.6.0 release, this attribute was replaced by service:, which is an object with prepared data.

ruby
input :first_name,
      type: String,
      required: {
        message: lambda do |service:, input:, value:|
          "Input `first_name` is required"
        end
      }

Option 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

Before version 2.6.0, service_class_name: was used instead of service:. In the 2.6.0 release, this attribute was replaced by service:, which is an object with prepared data.

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
       }

Option consists_of input (^2.0.0) internal (^2.0.0) output (^2.0.0)

INFO

Since version 2.6.0 this option is dynamic.

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,
      # The default array element type is String
      consists_of: {
        message: "ID can only be of String type"
      }
ruby
internal :ids,
         type: Array,
         # The default array element type is String
         consists_of: {
           message: "ID can only be of String type"
         }
ruby
output :ids,
       type: Array,
       # The default array element type is String
       consists_of: {
         message: "ID can only be of String type"
       }

Option 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
       }

Option must input internal (^2.2.0) output (^2.2.0)

INFO

The must option can work only in advanced mode.

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

Before version 2.6.0, service_class_name: was used instead of service:. In the 2.6.0 release, this attribute was replaced by service:, which is an object with prepared data.

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
         }
       }