Skip to content

Extend renderValidatedNewtypes to Support Collection Types #1843

@ghostbuster91

Description

@ghostbuster91

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions