Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samhammerag/samhammer.validation
https://github.com/samhammerag/samhammer.validation
net-core-project-lib
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/samhammerag/samhammer.validation
- Owner: SamhammerAG
- License: mit
- Created: 2020-01-15T16:36:35.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-10T07:06:25.000Z (7 months ago)
- Last Synced: 2024-11-14T13:46:48.247Z (about 2 months ago)
- Topics: net-core-project-lib
- Language: C#
- Homepage:
- Size: 43.9 KB
- Stars: 0
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Usage
#### How to add this to your project:
- reference this package to your project: https://www.nuget.org/packages/Samhammer.Validation/#### Validate a model ####
```csharp
var context = new Validation()
.Load("test")
.Add(SampleRule);var result = await context.ValidateAsync();
if (!result.Succeeded)
{
// TODO handle validation error
}public ValidationResult SampleRule(string input)
{
return new ValidationResult { Succeeded = input != null };
}
```#### Validate a model, loaded by func ####
```csharp
async Task LoadModel() => await Repository.GetById(id);var context = new Validation()
.Load(LoadModel)
.Add(SampleRule);
```#### Validate multiple models ####
```csharp
var context1 = new Validation()
.Load(input1)
.Add(SampleRule);var context2 = new Validation()
.Load(input2)
.Add(SampleRule2);var result = await Validation.ValidateAllAsync(context1, context2);
```#### Validate with custom result class ####
You can define your own result class with additional fields.
This can be used to add something like an errorCode or an errorMessage by your rules.```csharp
var context = new Validation()
.Load(input)
.Add(SampleRuleWithErrorCode);var result = await context.ValidateAsync();
public static CustomValidationResult SampleRuleWithErrorCode(string input)
{
return string.IsNullOrEmpty(input)
? new CustomValidationResult(ErrorCode.Error)
: new CustomValidationResult();
}
``````csharp
public class CustomValidationResult : ValidationResult
{
public ErrorCode ErrorCode { get; set; }public CustomValidationResult()
{
Succeeded = true;
}public CustomValidationResult(ErrorCode errorCode)
{
Succeeded = false;
ErrorCode = errorCode;
}
}public enum ErrorCode
{
Ok,
Error,
}
```## Contribute
#### How to publish package
- Create a tag and let the github action do the publishing for you