Skip to content

Constraint Traits

Matt Muller edited this page Oct 5, 2021 · 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

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. This trait does not influence code generation.

length trait

Constrains a shape to minimum and maximum number of elements or size.

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

This constraint is not validated.

pattern trait

Restricts string shape values to a specified regular expression.

@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.

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

This constraint is not validated.

required trait

Marks a structure member as required, meaning a value for the member MUST be present and not set to null.

structure MyStructure {
    @required
    foo: FooString,
}

This constraint is not validated.

uniqueItems trait

Indicates that the items in a List MUST be unique.

@uniqueItems
list MyList {
    member: String
}

This constraint is not validated.