PropertyValidator 1.0.4-patch
This is a prerelease version of PropertyValidator.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package PropertyValidator --version 1.0.4-patch
NuGet\Install-Package PropertyValidator -Version 1.0.4-patch
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PropertyValidator" Version="1.0.4-patch" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PropertyValidator" Version="1.0.4-patch" />
<PackageReference Include="PropertyValidator" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PropertyValidator --version 1.0.4-patch
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PropertyValidator, 1.0.4-patch"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PropertyValidator@1.0.4-patch
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PropertyValidator&version=1.0.4-patch&prerelease
#tool nuget:?package=PropertyValidator&version=1.0.4-patch&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
A simple library to help you validate properties of classes that implements INotifyPropertyChanged.
Setup
- Available on NuGet: PropertyValidator
- Available on GitHub: PropertyValidator
Service interface
The interface is pretty simple and self-documenting:
// (yet with comments)
public interface IValidationService
{
// For registration
RuleCollection<TNotifiableModel> For<TNotifiableModel>(TNotifiableModel notifiableModel)
where TNotifiableModel : INotifyPropertyChanged;
// Retrieve error messages per property
string GetErrorMessage<TNotifiableModel>(
TNotifiableModel notifiableModel,
Expression<Func<TNotifiableModel, object>> expression)
where TNotifiableModel : INotifyPropertyChanged;
// Manually trigger the validation
bool Validate();
// Subscribe to error events (cleared/raised)
event EventHandler<ValidationResultArgs> PropertyInvalid;
}
Usage:
- Create the validation rule models by extending the
ValidationRule<T>orMultiValidationRule<T>, whereTis the type of the target property.
// For email address
public class EmailFormatRule : ValidationRule<string>
{
public override string ErrorMessage => "Not a valid email format";
public override bool IsValid(string value)
{
if (string.IsNullOrEmpty(value))
return false;
const string pattern = @"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
return regex.IsMatch(value);
}
}
// For required field
public class RequiredRule : ValidationRule<string>
{
public override string ErrorMessage => "Izz required!";
public override bool IsValid(string value)
{
return !string.IsNullOrEmpty(value);
}
}
// If you want to limit the string to a certain length
public class LengthRule : ValidationRule<string>
{
public override string ErrorMessage => string.Format(Strings.MaxCharacters, max);
private readonly int max;
public LengthRule(int max)
{
this.max = max;
}
public override bool IsValid(string value)
{
if (string.IsNullOrEmpty(value))
return true;
return value.Length < max;
}
}
// A multi-property validation model. You can also reuse other ValidationRules here!
public class AddressRule : MultiValidationRule<Address>
{
protected override RuleCollection<Address> ConfigureRules(RuleCollection<Address> ruleCollection)
{
return ruleCollection
.AddRule(e => e.City, new RequiredRule())
.AddRule(e => e.CountryIsoCode, new CountryIsoCodeRule())
.AddRule(e => e.PostalCode, new PostalCodeRule())
.AddRule(e => e.StreetAddress, new RequiredRule(), new LengthRule(100));
}
}
- Use the validation rules in our classes that implements (implicitly from the base class)
INotifyPropertyChanged. The example below is used in Xamarin Forms along with the Prism to register the service in the Dependency Injection, but it can be used also in other platforms supported by the .NET family.
public class ItemsPageViewModel : BaseViewModel, IInitialize
{
private readonly IValidationService validationService;
public ItemsPageViewModel(INavigationService navigationService, IValidationService validationService) : base(navigationService)
{
this.validationService = validationService;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public Address PhysicalAddress { get; set; } = new Address();
public string FirstNameError { get; set; }
public string LastNameError { get; set; }
public string EmailAddressError { get; set; }
public string PhysicalAddressError { get; set; }
// You must do this only once in the initialization part of your class model.
public void Initialize(INavigationParameters parameters)
{
validationService.For(this)
.AddRule(e => e.FirstName, new RequiredRule())
.AddRule(e => e.LastName, new LengthRule(50))
.AddRule(e => e.EmailAddress, new RequiredRule(), new LengthRule(100), new EmailFormatRule())
// The error message have been overriden to "Deez nuts" since an aggregated error messages is awful.
.AddRule(e => e.PhysicalAddress, "Deez nuts", new AddressRule());
validationService.PropertyInvalid += ValidationService_PropertyInvalid;
}
private void ValidationService_PropertyInvalid(object sender, ValidationResultArgs e)
{
switch (e.PropertyName)
{
case nameof(FirstName):
FirstNameError = e.FirstError;
break;
case nameof(LastName):
LastNameError = e.FirstError;
break;
case nameof(EmailAddress):
EmailAddressError = e.FirstError;
break;
case nameof(PhysicalAddress):
PhysicalAddressError = e.FirstError;
break;
}
// To retrieve all the error message of the property, use:
var errorMessages = e.ErrorMessages;
// If you have a bunch of error properties, skip the tall switch-case and be more productive by using this:
e.FillErrorProperty(this);
// This will basically auto-fill the error properties you have in the target instance but,
// you must follow this convention: "<PropertyName>" + "Error"
}
}
- If you wish not to use
PropertyInvalidevent to check every time the property have changed, you can also invoke manually theValidationService#Validate(), check the return, if it's false, find the error message usingValidationService#GetErrorMessage(...)
private void ShowValidationResult()
{
FirstNameError = validationService.GetErrorMessage(this, e => e.FirstName);
LastNameError = validationService.GetErrorMessage(this, e => e.LastName);
EmailAddressError = validationService.GetErrorMessage(this, e => e.EmailAddress);
PhysicalAddressError = validationService.GetErrorMessage(this, e => e.PhysicalAddress);
}
private void Register()
{
if (!validationService.Validate())
{
ShowValidationResult();
return;
}
...
}
Result

Support
Feel free to contribute if you find some issues or you have more ideas to add 😃
ETH: 0x11bafdeCfb4Aa03D029ef10c9cE8DCB41B83aFb1
| 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on PropertyValidator:
| Package | Downloads |
|---|---|
|
PropertyValidator.ValidationPack
Contains common validation rules for PropertyValidator |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.1.1 | 578 | 12/25/2023 |
| 1.1.0.1 | 476 | 12/18/2023 |
| 1.1.0 | 636 | 9/19/2023 |
| 1.0.6.7 | 1,020 | 5/9/2021 |
| 1.0.6.6 | 1,081 | 5/9/2021 |
| 1.0.6.5 | 1,039 | 5/9/2021 |
| 1.0.6.4 | 993 | 4/10/2021 |
| 1.0.6.3 | 1,022 | 4/10/2021 |
| 1.0.6.2 | 1,014 | 4/10/2021 |
| 1.0.6.1 | 1,032 | 4/10/2021 |
| 1.0.6 | 1,123 | 11/1/2020 |
| 1.0.4 | 1,195 | 10/31/2020 |
| 1.0.4-patch | 1,099 | 10/31/2020 |
| 1.0.3 | 1,117 | 9/23/2020 |
| 1.0.1 | 1,181 | 9/5/2020 |
Partial fix for error aggregations and bug fixes