Skip to content

ValidaZione provides several different approaches to validate your application's incoming data.

License

Notifications You must be signed in to change notification settings

joc-luis/ValidaZione

Repository files navigation

Status ☠️

The library is in testing mode, is not secure for production mode.

Install

dotnet add package ValidaZione

Use

Import

using ValidaZione;

Create an object

Validazione validazione = new Validazione(Language.En);
Validazione validazione = new Validazione(Language.Es);

Add like scoped

builder.Services.AddScoped<IValidazion>(v => new Validazione(Language.Fr));

// In the controller
private readonly IValidazione _validazion;

public PersonController(IValidazione validazione)
{
    _validazione = validazione;
}


[HttpPost]
public async Task<ActionResult> Store(Person person){

    _validazione.Field("name", person.Name).Between(3, 20);
    _validazione.PassOrException();
}

Validate string

validazione.Field("Test", "testing").Min(3).Max(10).Alpha();

Validate numbers

validazione.Field("Test", 4).Between(1, 10);
validazione.Field("Test2", 5.07).Between(1, 10);

Validate lists or arrays

validazione.Field("Test", new int[] {1, 2, 3}).Between(1, 10);
validazione.Field("Test2", new List<Person>(){ new Person()}).Distinct();

Validate dates

validazione.Field("Test", DateTime.Now).Before(DateTime.Now.AddDays(1));

Validate booleans

validazione.Field("Test", true).Accepted();
validazione.Field("Test", false).Declined();

Pass validation

if(validazione.Pass()){
  //Save data
}

Launch a exception

try
{
 
    validazione.PassOrException();
    
}
catch(ValidazioneException e){

    Console.WriteLine(e.Message);
    
    foreach(var field in e.Fields){
        field.Errors.ForEach(error => {
            Console.WriteLine(error);
        });
    }
}

Get errors by field

var fields = validazione.ErrorsByField();

Get all errors

var errors = validazione.Errors();

Create your custom error messages

Use the interface

public class MyCustomLang : ILang {

    public string FieldName { get; set; }
    
    public string Accepted(){
  
        return "Acepta las condiciones que evidentemente no leíste";
    }
 }

Set in the constructor

Validazione validazione = new Validazione(new MyCustomLang())

License

MIT

External resources

Icon by Paul J.

Validation messages

About

ValidaZione provides several different approaches to validate your application's incoming data.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages