Code Monkey home page Code Monkey logo

many.validators's Introduction

Many.Validators

A set of very-easy-to-read immutable types to add seamless validations in your projects.

Focused on:

  • Less documentation, better code legibility.
  • No extra line of code, just change type by validators and that's all.
  • Minimum overhead compared to traditional copy-pasted-conditionals way or any other implementation.

๐Ÿ‘‰Download releases at https://www.nuget.org/packages/Many.Validators/

Content

  • [Roadmap][]
  • [Features][]
    • [Two-way conversion between validator and underlying types][โœ… Two-way conversion between validator and underlying types]
    • [Seamless integration][โœ… Seamless integration]
    • [InRange validators][โœ… InRange validators]
    • [Concatenation clauses][โœ… Concatenation clauses]
    • [Multiple explicit validations][โœ… Multiple explicit validations]
    • [NetStandard 2.1 and NET60 specific implementations][โœ… NetStandard 2.1 and NET60 specific implementations]

Roadmap

Validator Implemented Version
NotNull<> โœ… 1.0.0
NotNullOrEmpty< string> โœ… 1.0.0
NotNullOrEmptyArray<> โœ… 1.1.0
Positive<> โœ… 1.0.0
PositiveOrZero<> โœ… 1.0.0
Negative<> โœ… 1.0.0
NegativeOrZero<> โœ… 1.0.0
InRange<> โœ… 2.0.0
GreaterThan<> โŒ
LowerThan<> โŒ
NotDefault<> โŒ
Feature Implemented Version
Multiple explicit validation โœ… 3.0.0
Concatenation clause (All) โœ… 4.0.0
Custom exception type and/or message โŒ

Features

โœ… Two-way conversion between validator and underlying types

NotNull<string> notNullStringValidator = "whatever"; //Ok
string x = notNullStringValidator; //Ok

notNullStringValidator.Equals(x); //true!

โœ… Seamless integration

Use validators in your methods referring your underlying type:

bool DoSomething(NotNull<string> @param)
{
    //@param is observed and validated when is instantiated. 
    //If validation fails no line of code will be run in this method

    Console.WriteLine("Whatever");
    
    //Gets @param value with no conversion
    return !string.IsNullOrWhiteSpace(@param); 
}

And call your methods as usual with no additional conversions:

string p;

<Code where you can assign a value to p or not...>

var result = WhateverMethod(p);

<Continues if no exception was raised...>

โœ… InRange validators

InRange validators need a range ๐Ÿ˜…. To get that you only need to create your custom range class (inherited from abstract RangeBase class) and implement the abstract properties. Finally be sure to name your class with a self-descritive name.

Example:

namespace YourProject.Validators.Ranges.Int64
{
    internal class Neg100_1 : Range<Int64>
    {
        public override long Max => 1;

        public override long Min => -100;
    }
}

Then only use it:

public void DoSometing(InRange<Neg100_1, Int64> param1) 
{
    ...
}

Tip: Also you can follow a self-descriptive namespace naming strategy in order to get clearer shorter range names.

โœ… Concatenation clauses

You can concatenate 2 or 3 validators using clauses (they are placed in different namespaces depending on number of validators: Many.Validators.Clauses.S2 and S3):

using Many.Validators.Clauses.S2;

public void DoSometing(All<NotNull<int?>, Positive<int?>, int?> param1) 
{
    ...
}

or

using Many.Validators.Clauses.S3;

public void DoSometing(All<NotNull<int?>, Positive<int?>, InRange<Int_1_100, int?>, int?> param1) 
{
    ...
}

โœ… Multiple explicit validations

Also you can validate multiple values in a explicit way. If you have a class like this:

class YourObject
{
    public string Id {get;set;}
    public string Data {get;set;}
}

You can validate itself and properties' values in several ways:

public void DoSomething(NotNull<YourObject> param) //This line checks if param itself is not null
{
    var otherParamsToCheck = new string[]{param.Id, param.Data};

    //You can validate params and get an exception if any of them is not valid or...
    NotNull<string>.Validate(otherParamsToCheck);

    //...you can validate params and get the whole result with no exceptions

    //1.Get all errors
    var result = NotNull<string>.IsValid(otherParamsToCheck, out string[] errors);

    //2.Get and stop after first error
    result = NotNull<string>.IsValid(otherParamsToCheck, out string error);    
}

โœ… NetStandard 2.1 and NET60 specific implementations

  • New Half type support for NET5 and NET6 projects.

many.validators's People

Contributors

yeyopepe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.