Table of Contents

Class StringLengthAttribute

Namespace
Trellis
Assembly
Trellis.Core.dll

Specifies the minimum and maximum length of characters that are allowed in a RequiredString<TSelf>-derived value object.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class StringLengthAttribute : Attribute
Inheritance
StringLengthAttribute
Inherited Members
Extension Methods

Examples

Maximum length only:

[StringLength(50)]
public partial class FirstName : RequiredString<FirstName> { }

// Generated TryCreate validates:
// - Not null/empty/whitespace
// - Length <= 50

Both minimum and maximum length:

[StringLength(100, MinimumLength = 3)]
public partial class Description : RequiredString<Description> { }

// Generated TryCreate validates:
// - Not null/empty/whitespace
// - Length >= 3
// - Length <= 100

Remarks

When applied to a partial class inheriting from RequiredString<TSelf>, the source generator automatically includes length validation in the generated TryCreate method. The length check runs after the null/empty/whitespace validation, so the length is checked on a non-whitespace string.

This attribute is designed specifically for Trellis value objects and is processed at compile time by the PrimitiveValueObjectGenerator source generator. It does not rely on runtime reflection.

Constructors

StringLengthAttribute(int)

Initializes a new instance of the StringLengthAttribute class with the specified maximum length.

public StringLengthAttribute(int maximumLength)

Parameters

maximumLength int

The maximum length, inclusive. Must be at least 1.

Examples

[StringLength(50)]
public partial class FirstName : RequiredString<FirstName> { }

Exceptions

ArgumentOutOfRangeException

Thrown when maximumLength is less than 1.

Properties

Code

Gets or sets an application-defined reason code that replaces the framework default on every failure this attribute produces.

public string? Code { get; set; }

Property Value

string

A non-empty code, or null to keep the framework default. Setting it to an empty or whitespace string is a generator error (TRLS060).

Remarks

The framework vocabulary is frozen so that one client catalog entry works across every Trellis service. An application that owns both ends of its own contract — it wrote the declaration and it writes the catalog key — may name the failure in its own terms instead, and nothing it does here can invalidate another party's catalog. An override carries no warning and no penalty.

MaximumLength

Gets the maximum allowable length of the string.

public int MaximumLength { get; }

Property Value

int

The maximum length, inclusive.

MinimumLength

Gets or sets the minimum allowable length of the string.

public int MinimumLength { get; set; }

Property Value

int

The minimum length, inclusive. Defaults to 0 (no minimum beyond non-empty). Negative values are treated as 0 by the source generator (no minimum length check is emitted).

See Also