Spectral.Codec.String (Spectral v0.12.0)

View Source

Built-in codec for String.t().

Validates that encoded and decoded values are UTF-8 binaries, and optionally enforces constraints supplied via type_parameters in a spectral annotation placed before the type definition:

  • min_length — minimum codepoint length (inclusive)
  • max_length — maximum codepoint length (inclusive)
  • pattern — regular expression the string must match

When no params are present the codec acts as a transparent pass-through, accepting any binary and producing it unchanged.

Note: type_parameters are only accessible when the codec is invoked directly from a Spectral entry point (e.g. Spectral.encode/decode called with the type's own module). They are not propagated during mid-traversal dispatch.

Registration

Register this codec in your application's config/config.exs or application.ex start callback before encoding/decoding String.t() values:

Application.put_env(:spectra, :codecs, %{
  {String, {:type, :t, 0}} => Spectral.Codec.String
})