Skip to content

Constraint Traits

Juli Tera edited this page Apr 16, 2024 · 8 revisions

This wiki contains a mapping between Smithy Constraint traits and generated Ruby code.

To prevent forwards incompatibility, these constraints are not validated on the client side.

enum trait

This trait is deprecated. An enum shape should be used instead.

Constrains the acceptable values of a string to a fixed set.

@enum([
    {
        value: "t2.nano",
        name: "T2_NANO",
        documentation: """
            T2 instances are Burstable Performance
            Instances that provide a baseline level of CPU
            performance with the ability to burst above the
            baseline.""",
        tags: ["ebsOnly"]
    },
    {
        value: "t2.micro",
        name: "T2_MICRO",
        documentation: """
            T2 instances are Burstable Performance
            Instances that provide a baseline level of CPU
            performance with the ability to burst above the
            baseline.""",
        tags: ["ebsOnly"]
    },
    {
        value: "m256.mega",
        name: "M256_MEGA",
        deprecated: true
    }
])
string MyString

We add documentation for enums on the structure shapes. This documentation is duplicated across all shapes that reference the enum.

# @!attribute
# @return [String] One of: ["t2.nano", "t2.micro", "m256.mega"]
Structure = Struct.new(
  :my_string,
  keyword_init: true
)

The Smithy documentation says: “Code generators MAY choose to represent enums as programming language constants.” The service types will define a module for each enum shape. Documentation is added to the module and the enum constants.

module SampleService
  module Types
    # Includes enums for MyString
    # One of: ["t2.nano", "t2.micro", "m256.mega"]
    module MyString
      # T2 instances are ...
      # Tags: ["ebsOnly"]
      T2_NANO = "t2.nano"
      # T2 instances are ...
      # Tags: ["ebsOnly"]
      T2_MICRO = "t2.micro"
      # @note This enum value is deprecated
      M256_MEGA = "m256.mega"
    end
  end
end

# usage
SampleService::Types::MyString::T2_NANO #=> "t2.nano"

idRef trait

Indicates that a string value MUST contain a valid absolute shape ID. The @idRef trait is used primarily when declaring trait shapes in a model. See Smithy Documentation for more details.

This trait does not influence code generation.

length trait

Constrains a shape to minimum and maximum number of elements or size. See Smithy Documentation for more details.

@length(min: 1, max: 10)
string MyString

This constraint is not validated.

pattern trait

Restricts string shape values to a specified regular expression. See Smithy Documentation for more details.

@pattern("\\w+")
string MyString

This constraint is not validated.

private trait

Prevents models defined in a different namespace from referencing the targeted shape. The Smithy Documentation states that this trait should not influence code generation.

range trait

Restricts allowed values of byte, short, integer, long, float, double, bigDecimal, and bigInteger shapes within an acceptable lower and upper bound. See Smithy Documentation for more details.

@range(min: 1, max: 10)
integer MyInt

This constraint is not validated.

uniqueItems trait

Indicates that the items in a list MUST be unique. See Smithy Documentation for more details.

@uniqueItems
list MyList {
    member: String
}

This constraint is not validated.