Skip to content

Constraint Traits

Juli Tera edited this page Apr 19, 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. This trait does not influence code generation.

See Smithy Documentation for more details.

length trait

Constrains a shape to minimum and maximum number of elements or size. This constraint is not validated.

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

See Smithy Documentation for more details.

pattern trait

Restricts string shape values to a specified regular expression. This constraint is not validated.

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

See Smithy Documentation for more details.

private trait

Prevents models defined in a different namespace from referencing the targeted shape.

@private
string PrivateString

See Smithy Documentation for more details.

range trait

Restricts allowed values of byte, short, integer, long, float, double, bigDecimal, and bigInteger shapes within an acceptable lower and upper bound. This constraint is not validated.

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

See Smithy Documentation for more details.

uniqueItems trait

Indicates that the items in a list MUST be unique based on Value equality. This constraint is not validated.

@uniqueItems
list MyList {
    member: String
}

See Smithy Documentation for more details.