πŸ“Common

Common validations to all types

The only rule enforced by Typely is that the value object cannot be created with a null value when the underlying type is a reference type. In this case, an ArgumentNullException will be thrown. Default values are still allowed like 0 for int or "" for string. If you want the type to be set to a null value, make it nullable (MyType?) or make the type a reference type with AsClass().

NotEmpty Validator

Ensures that the value is not an empty string or whitespace or the default value for value types, for example, 0 for int.

Example:

builder.OfString().For("Name").NotEmpty();

Example error: 'Name' must not be empty.

String format args:

  • Name: Name of the type being validated

  • Value: Current value of the property

NotEqual Validator

Ensures that the value is not equal to the specified value.

Example:

builder.OfString().For("Name").NotEqual("value");

Example error: 'Name' should not be equal to 'value'.

String format args:

  • Name: Name of the type being validated

  • Value: Current value of the property

  • ComparisonValue: Value that should not equal

Must Validator

Ensures that the value meets the specified condition.

Example:

builder.OfString().For("Name").Must(x => x.StartsWith("abc"));

Example error: The specified condition was not met for '{Name}'.

String format args:

  • Name: Name of the type being validated

  • Value: Current value of the property

Last updated