PropertyValidator 1.0.1
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.1
NuGet\Install-Package PropertyValidator -Version 1.0.1
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.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PropertyValidator" Version="1.0.1" />
<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.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PropertyValidator, 1.0.1"
#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.1
#: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.1
#tool nuget:?package=PropertyValidator&version=1.0.1
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.
Service interface
The interface is pretty simple and self-documenting:
public interface IValidationService
{
RuleCollection<TNotifiableModel> For<TNotifiableModel>(TNotifiableModel notifiableModel) where TNotifiableModel : INotifyPropertyChanged;
string GetErrorMessage<TNotifiableModel>(TNotifiableModel notifiableModel, Expression<Func<TNotifiableModel, object>> expression) where TNotifiableModel : INotifyPropertyChanged;
bool Validate();
event EventHandler<ValidationResultArgs> PropertyInvalid;
}
Usage:
- Create the validation rule models by extending the
ValidationRule<T>, where T is 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;
}
}
- 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 library to register the service in the Dependency Injection library, but it can be used also in other .NET supported platforms.
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 string FirstNameError { get; set; }
public string LastNameError { get; set; }
public string EmailAddressError { 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())
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;
}
// To retrieve all the error message of the property, use:
var errorMessages = e.ErrorMessages;
}
}
- If you wish not to use
PropertyInvalidevent to check every time the property have changed, you can also invoke manually theIValidationService.Validate(), check the return, if it's false, find the error message usingIValidationService.GetErrorMessage(...)
private void ShowValidationResult()
{
ErrorFirstName = validationService.GetErrorMessage(this, e => e.FirstName);
ErrorLastName = validationService.GetErrorMessage(this, e => e.LastName);
ErrorEmailAddress = validationService.GetErrorMessage(this, e => e.EmailAddress);
}
private void Register()
{
if (!validationService.Validate())
{
ShowValidationResult();
return;
}
...
}
Result

Feel free to contribute if you find some issues or you have more ideas to add 😃
| 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 | 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- 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 | 610 | 12/25/2023 |
| 1.1.0.1 | 495 | 12/18/2023 |
| 1.1.0 | 654 | 9/19/2023 |
| 1.0.6.7 | 1,037 | 5/9/2021 |
| 1.0.6.6 | 1,109 | 5/9/2021 |
| 1.0.6.5 | 1,056 | 5/9/2021 |
| 1.0.6.4 | 1,011 | 4/10/2021 |
| 1.0.6.3 | 1,038 | 4/10/2021 |
| 1.0.6.2 | 1,031 | 4/10/2021 |
| 1.0.6.1 | 1,052 | 4/10/2021 |
| 1.0.6 | 1,143 | 11/1/2020 |
| 1.0.4 | 1,214 | 10/31/2020 |
| 1.0.4-patch | 1,117 | 10/31/2020 |
| 1.0.3 | 1,137 | 9/23/2020 |
| 1.0.1 | 1,201 | 9/5/2020 |
Initial Release