Table of Contents

Class StringLengthAttribute

Namespace
Trellis
Assembly
Trellis.Primitives.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

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