Types and Files Rules
Type and file rules validate the runtime shape of the bound value. They are commonly used before content-specific validation so clients receive clear type or file errors instead of later rule failures.
String
Requires the value to be a string.
[String]RuleFor(x => x.DisplayName)
.String();Array
Requires the value to be an enumerable value that is not a string.
[Array]RuleFor(x => x.Tags)
.Array();List
Requires the value to be an array or list-like value.
[List]RuleFor(x => x.Items)
.List();Boolean
Requires the value to be a boolean value.
[Boolean]RuleFor(x => x.Enabled)
.Boolean();Enum
Requires the value to name a member the enum actually defines. The enum's own type, a case-sensitive member name, and an integral value all qualify; anything else fails rather than being converted first, so a bool or a fractional number does not pass as the member its conversion would land on. When the enum type is omitted, the validator uses the request property type.
[Enum(Type? enumType = null)]
[Enum<TEnum>]
[Enum]
[Enum(typeof(DayOfWeek))]
[Enum<OrderStatus>]RuleFor(x => x.Status)
.Enum<OrderStatus>();File
Requires the value to be an uploaded file.
[File]RuleFor(x => x.Avatar)
.File();Uploaded
Requires the uploaded file to be present and non-empty.
[Uploaded]RuleFor(x => x.Avatar)
.Uploaded();Extensions
Allows only files with one of the provided file extensions.
[Extensions(params string[] extensions)]
[Extensions("jpg", "png", "webp")]RuleFor(x => x.Avatar)
.Extensions("jpg", "png", "webp");Mimes
Allows only files matching the provided MIME extension aliases.
[Mimes(params string[] mimes)]
[Mimes("jpg", "jpeg", "png", "webp")]RuleFor(x => x.Avatar)
.Mimes("jpg", "jpeg", "png", "webp");MimeTypes
Allows only files with one of the provided MIME types.
[MimeTypes(params string[] mimeTypes)]
[MimeTypes("image/jpeg", "image/png", "image/webp")]RuleFor(x => x.Avatar)
.MimeTypes("image/jpeg", "image/png", "image/webp");Image
Requires the uploaded file to be an image, decided by the bytes it opens with rather than by its name or content type. PNG, GIF, JPEG, WebP, BMP, TIFF, AVIF, HEIC, and ICO are recognized; anything else is rejected, so a file named photo.png holding arbitrary content does not pass.
[Image]RuleFor(x => x.Avatar)
.Image();Dimensions
Requires the uploaded image to match the exact width and height.
[Dimensions(int width, int height)]
[Dimensions(512, 512)]RuleFor(x => x.Avatar)
.Dimensions(512, 512);MinDimensions
Requires the uploaded image to be at least the provided width and height.
[MinDimensions(int width, int height)]
[MinDimensions(512, 512)]RuleFor(x => x.Avatar)
.MinDimensions(512, 512);MaxDimensions
Requires the uploaded image to be no larger than the provided width and height.
[MaxDimensions(int width, int height)]
[MaxDimensions(1024, 1024)]RuleFor(x => x.Avatar)
.MaxDimensions(1024, 1024);