Git Product home page Git Product logo

ensure's Introduction

ensure

Fluent guards to aid in ensuring the correctness of function/method arguments. Inspired by the lovely Ensure.That library for C#.

Example

import std.string : chop;
import std.stdio : write, writefln, stdin;

import ensure;

void greet( string name )
{
    // String cannot be null, zero-length, or consist entirely of whitespace characters.
    ensure!name.isNotNull.isNotEmpty.isNotWhitespace;

    writefln( "Hello, %s!", name );
}

void main()
{
    write( "What is your name? " );
    auto name = stdin.readln().chop();
    greet( name );
}

Get it

It's on dub: https://code.dlang.org/packages/ensure

Extending

ensure is easily extensible using UFCS and templated functions, in fact all built-in validators use this. Take the isNotNull validator as an example:

Arg!T isNotNull( T )( Arg!T arg ) if( isNullable!T )
{
    if( arg.value is null )
        arg.throwWith( "argument cannot be null" );

    return arg;
}

That's really all it takes! isNullable is a private template used to determine if T can even be null, so isNotNull won't compile if T is something that can't be null, like int.

Writing your own validator is similarly simple:

import std.traits : isIntegral;
import std.format : format;

import ensure;

// template function lets this be called on any valid type (as per the template guard)
// however it's also possible to use concrete types
// ex: Arg!int someValidator( Arg!int arg )
// {
//     ...
// }
Arg!N isTheAnswerToEverything( N )( Arg!N arg ) if( isIntegral!N ) // only for integer numbers
{
    // access the argument's value.
    // if needed, the name can also be accessed with arg.paramName
    if( arg.value != 42 )
        // throw a new EnsureException with a custom message when validation fails.
        arg.throwWith( "%s is not the answer to everything".format( arg.value ) );
    
    // return arg when we're done with it so successful calls can be chained as above in the greet example.
    return arg;
}

ensure's People

Contributors

sirtony avatar

Stargazers

red thing avatar Paul Crane avatar Beyar avatar

Watchers

James Cloos avatar  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.