← all lessons

Lesson 3

Structures

Structures

A structure is Smithy’s basic record type — a named bag of typed members. It’s the shape you’ll reach for most often: request bodies, response bodies, and nested data are all structures.

structure City {
    @required
    name: String

    population: Integer
}

Each member has a name and a target — the shape it points at (String, Integer, or another shape you’ve defined). The @required trait marks a member the model must always provide; without it, a member is optional.

Fill in the blanks

This first exercise is a fill-in-the-blank: most of the snippet is locked, and you only complete the highlighted gaps. Give City a @required member name targeting String.

The checks assert the member exists, targets String, and is @required — so the type and the trait both have to be right.

Now from scratch

The same task, but this time you write the whole model. You’ll also need the two things every model needs at the top: a $version control statement and a namespace.