✔ A collection of useful validation rules for FluentValidation and Entity Framework Core ✨
This NuGet package provides two useful validation rules for FluentValidation when used with Entity Framework Core.
The Unique
validation rule checks whether the given value is unique (i.e. does not exist) in the given database context, for the given entity property. For example, you can validate whether the email of a user is unique when they register.
Usage
// Check if an e-mail is unique in the given DbContext for the User entity's Email property
RuleFor(x => x.Email)
.Unique(context, (User user) => user.Email)
// Optionally, apply a transformation to the value you are validating before it is compared
RuleFor(x => x.Email)
.Unique(context, (User user) => user.NormalizedEmail, value => value.ToUpper())
The Exists
validation rules check whether the given value exists in the given database context as the given entity property. For example, you can validate whether the country code a user submitted is valid.
Usage
// Check if country code exists in the given DbContext as the Country entity's Code property
RuleFor(x => x.CountryCode)
.Exists(context, (Country country) => country.Code)
// Optionally, apply a transformation to the value you are validating before it is compared
RuleFor(x => x.CountryCode)
.Exists(context, (Country country) => country.Code, value => value.ToUpper())
- .NET 6
- FluentValidation 10.3.6 or higher
- Entity Framework Core 6.0.1 or higher
Install with NuGet
Licensed under MIT without trademark grant.
Copyright © 2021 Cloudey IT Ltd
Cloudey® is a registered trademark of Cloudey IT Ltd. Use of the Cloudey trademark is NOT GRANTED as part of the license of this repository and/or package, notwithstanding any notice in the license or other published material. Users of the code or material contained and/or distributed as part of this repository ARE NOT granted a license to use the Cloudey trademark by virtue of this repository and package being license under the MIT license.