ValidatR 0.0.1-preview6
dotnet add package ValidatR --version 0.0.1-preview6
NuGet\Install-Package ValidatR -Version 0.0.1-preview6
<PackageReference Include="ValidatR" Version="0.0.1-preview6" />
paket add ValidatR --version 0.0.1-preview6
#r "nuget: ValidatR, 0.0.1-preview6"
// Install ValidatR as a Cake Addin #addin nuget:?package=ValidatR&version=0.0.1-preview6&prerelease // Install ValidatR as a Cake Tool #tool nuget:?package=ValidatR&version=0.0.1-preview6&prerelease
ValidatR
Simple attribute based validations where validation rule values are fetched through Func to let the consumer decide the values based on field, validationtype and a parameter which can be sent in the ValidateAsync or fetched through a parameter resolver.
Use cases
- Validation rules needs to be different based on model property value (using parameter resolver)
- Example: All validation rules needs to be different based on a property value
- Example: Validation rules differ for CreateCustomerRequest depending on country
- Validation rules needs to be different based on external value (the parameter is provided in the validate method)
- Example: Vary rules based on countryCode sent in route to controller
ValidateAttribute
To add validation rules to a model use the [Validate("id", ValidatorType | ValidatorType)]
attribute.
Example:
public class CreateCustomerRequest
{
[Validate("CreateCustomerRequest.FirstName", ValidatorType.Regex | ValidatorType.Required)]
public string FirstName { get; set; }
[Validate("CreateCustomerRequest.Address", ValidatorType.Required)]
public Address Address { get; set; }
...
}
public class Address
{
[Validate("Address.Street", ValidatorType.Required | ValidatorType.MaxLength)]
public string Street { get; set; }
}
Validate attribute can also be used on complex types, but in that case only required is supported on the nested type in the class used to trigger validation. OBS! Validation will only be performed on properties using the Validate attribute, and no exception will be thrown if no Validate attribute exists in a class used in the ValidateAsync method
Example registration
builder.Services.AddValidatR<string>().AddParameterResolver<CreateCustomerRequest>(x => x.LastName);
In the above example, we also add a parameter resolver to be able to resolve a model without specifying the parameter value to send to the func when retrieving the validation rule value.
If there i a need to use a service in the func (as in the example application), use the IApplicationBuilder extension below (The func in the AddValidatR is optional).
app.UseValidatR<string>()
.AddMinLengthValidator(id, parameter) =>
{
var storageService = app.Services.GetRequiredService<IStorageService>();
return storageService.GetValidationRuleValue(id, ValidatorType.MinLength, parameter);
});
Validation
There are 3 ways to validate
- Injecting
IValidator<TParameter>
, where TParameter is the type of the key used in the func - Injecting
IValidator
, requires adding Parameter resolvers for the types being validated otherwise an exception will be thrown. - Using the middleware.
Using the middleware
The middleware validates the request model and returns badrequest with a HttpValidationProblemDetails response if errors are found. When using the AddValidatorMiddleware applicationBuilder extension the extension method scans all loaded assemblies for classes with properties using the ValidateAttribute to automatically register a middleware for each of the classes. To explicitly tell ValidatR which classes to use, the extension methods supports params Type[] to specify the types to load middlewares for.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on ValidatR:
Package | Downloads |
---|---|
ValidatR.DependencyInjection
Validator extensions for registering in .net IoC container |
|
ValidatR.AspNet
Validator middleware |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.0.1-preview6 | 131 | 1/1/2023 |
0.0.1-preview5 | 110 | 12/30/2022 |
0.0.1-preview4 | 344 | 12/29/2022 |
0.0.1-preview3 | 104 | 12/29/2022 |
0.0.1-preview2 | 112 | 12/28/2022 |
0.0.1-preview1 | 116 | 12/19/2022 |