smithy4s icon indicating copy to clipboard operation
smithy4s copied to clipboard

Extend renderValidatedNewtypes to Support Collection Types

Open ghostbuster91 opened this issue 2 months ago • 6 comments

Smithy4s currently supports validated newtypes for constrained primitive shapes when smithy4sRenderValidatedNewtypes metadata key is set to true.

This proposal extends that mechanism to also include collection types (list and map).

Motivation

The existing renderValidatedNewtypes feature ensures that constructing a constrained primitive newtype (e.g., a NonEmptyString) performs validation at the point of creation, returning either a valid instance or an error. This helps prevent invalid values from being introduced.

However, collection types remain unvalidated at construction time, even when they have constraints such as @length. This means users can create invalid in-memory objects (e.g., List.empty) that only fail later during the decoding on the other side of the wire.

Extending renderValidatedNewtypes to collections would ensure that all constrained shapes behave consistently and benefit from the same safety guarantees.

Implementation details

Similarly to validated newtypes it should be possible to enable this feature on particular shapes by using validateNewtype trait or globally via smithy the metadata key.

This would require adding maps and lists to the set of valid targets of validateNewtype trait.

Examples:

use smithy4s.meta#validateNewtype

@validateNewtype
@length(min: 5)
list Names {
  member: String
}

metadata smithy4sRenderValidatedNewtypes = true

@length(min: 5)
list Names {
  member: String
}

ghostbuster91 avatar Oct 13 '25 13:10 ghostbuster91