Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/linkdotnet/validationextensions
Small library to extend validation attributes on blazor forms
https://github.com/linkdotnet/validationextensions
attributes blazor csharp forms validation
Last synced: 4 months ago
JSON representation
Small library to extend validation attributes on blazor forms
- Host: GitHub
- URL: https://github.com/linkdotnet/validationextensions
- Owner: linkdotnet
- License: mit
- Created: 2022-02-15T19:46:53.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-07T07:43:51.000Z (over 1 year ago)
- Last Synced: 2024-09-30T21:07:46.241Z (4 months ago)
- Topics: attributes, blazor, csharp, forms, validation
- Language: C#
- Homepage:
- Size: 76.2 KB
- Stars: 14
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# ValidationExtensions
[![.NET](https://github.com/linkdotnet/ValidationExtensions/actions/workflows/dotnet.yml/badge.svg)](https://github.com/linkdotnet/ValidationExtensions/actions/workflows/dotnet.yml)
[![nuget](https://img.shields.io/nuget/v/LinkDotNet.ValidationExtensions)](https://www.nuget.org/packages/LinkDotNet.ValidationExtensions)The motivation behind this small project is simple. Just imagine you have the following model in Blazor:
```csharp
public class MyModel
{
[Required]
public string Title { get; set; }[Required]
public string Content { get; set; }[Required]
public bool IsPublished { get; set; }
}
```Now as a consumer you have to provide all of those 3 values. That is all good and nice, but what if we want to say:
"Okay as long as it doesn't get published, we don't have to provide the content?". Well that does not work with the default implementation.Here is where this small library comes into play:
```csharp
public class MyModel
{
[Required]
public string Title { get; set; }[RequiredIf(nameof(IsPublished), true)]
public string Content { get; set; }[Required]
public bool IsPublished { get; set; }
}
```Now `Title` will always be required. But as long as `IsPublished` is false `Content` can be null or empty.
## Get Started
To install either go the [nuget](https://www.nuget.org/packages/LinkDotNet.ValidationExtensions) or execute the following command:
```
dotnet add LinkDotNet.ValidationExtensions
```## Example
```csharp
using LinkDotNet.ValidationExtensions;public class BlogArticle
{
[Required]
public string Title { get; set; }[RequiredIf(nameof(IsPublished), true)]
public string ArticleContent { get; set; }[RequiredIfNot(nameof(ArticleContent), null)]
public string ReplacementContent { get; set; }
[Required]
public bool? NoticeByEmail { get; set; }[RequiredDynamic(nameof(ValidateRequired_NoticeByEmail), "Notice by email is activated")]
public string? EmailAddress { get; set; }
[DynamicRange(typeof(decimal), minimum: 9.99, maximumPropertyName: nameof(MaximumPrice))]
public decimal? MinimumPrice { get; set; }[DynamicRange(typeof(decimal), minimumPropertyName: nameof(MinimumPrice), maximum: 199.99)]
public decimal? MaximumPrice { get; set; }[DynamicRange(minimum: 0.1d, maximumPropertyName: nameof(MaximumWeight))]
public double? MinimumWeight { get; set; }[DynamicRange(minimumPropertyName: nameof(MinimumWeight), maximum: 500d)]
public double? MaximumWeight { get; set; }private static bool ValidateRequired_NoticeByEmail(BlogArticle value)
{
if (!value.NoticeByEmail.HasValue)
{
return false;
}if (!value.NoticeByEmail.Value)
{
return false;
}if (string.IsNullOrWhiteSpace(value.EmailAddress))
{
return true;
}
else
{
return false;
}
}
}
```## Currently implemented additional attributes:
* `RequiredIf`
* `MinLengthIf` / `MaxLengthIf`
* `RangeIf`
* `MinIf` / `MaxIf`
* `Min` / `Max`
* `Dynamic`
* `DynamicRange`
* `DynamicRange` [C# 11 Generic Attributes](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-11.0/generic-attributes)
* `FutureDateValidation` / `PastDateValidation`