SunnySoldier357.ModelValidation
1.0.0
dotnet add package SunnySoldier357.ModelValidation --version 1.0.0
NuGet\Install-Package SunnySoldier357.ModelValidation -Version 1.0.0
<PackageReference Include="SunnySoldier357.ModelValidation" Version="1.0.0" />
paket add SunnySoldier357.ModelValidation --version 1.0.0
#r "nuget: SunnySoldier357.ModelValidation, 1.0.0"
// Install SunnySoldier357.ModelValidation as a Cake Addin
#addin nuget:?package=SunnySoldier357.ModelValidation&version=1.0.0
// Install SunnySoldier357.ModelValidation as a Cake Tool
#tool nuget:?package=SunnySoldier357.ModelValidation&version=1.0.0
Model Validation
Deprecated - see new NuGet Package!!!
A NuGet Package to aid with validating Models through the use of Attributes.
Use
The Model
Any model or class that wants to be validatable should inherit from
ValidatableObject
. This adds the Validate()
method to the class, which
automatically validates the class according to the attributes assigned to its
properties.
Attributes
The base attribute is ValidatablePropertyAttribute
which defines its own
Validate()
method to validate a property based on a single rule. This project
defines several attributes that inherit from ValidatablePropertyAttribute
which can be used.
MaximumLength
AttributeEnsures that the property does not exceed the specified maximum length
MinimumLength
AttributeEnsures that the property exceeds the specified minimum length
ContainsCharacter
AttributeEnsures that the property contains the specified character. (Useful for checking that an email has an '@' symbol)
DoesNotContainCharacter
AttributeEnsures that the property does not contain the specified container. (Useful for checking that a property has no spaces or
' '
)PropertyValueMatch
AttributeEnsures that the property's value matches with the value of the other specified property (Useful for checking that passwords match -
Password
&ConfirmPassword
properties have to match)
Custom Attributes
Any class can extend from the base ValidatablePropertyAttribute
and define
custom validation attributes.
Example Model
using ModelValidation;
public class TestModel : ValidatableObject
{
[MinimumLength(2)]
[MaximumLength(64)]
public string UserName { get; set; }
[ContainsCharacter('@')]
[DoesNotContainCharacter(' ')]
[MinimumLength(5)]
[MaximumLength(254)]
public string Email { get; set; }
[MinimumLength(8)]
[MaximumLength(64)]
public string Password { get; set; }
[PropertyValueMatch(nameof(Password))]
public string ConfirmPassword { get; set; }
}
Calling TestModel.Validate()
validates the model and stores any error messages
into the Errors
property, which is a List<string>
.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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 was computed. |
.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. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 559 | 6/2/2019 |
Initial Release