Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/samhammerag/samhammer.validation


https://github.com/samhammerag/samhammer.validation

net-core-project-lib

Last synced: about 1 month ago
JSON representation

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