pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/KoryNunn/discriminate

ink crossorigen="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-b69241e157469407.css" /> GitHub - KoryNunn/discriminate: A data validator with an idiomatic API · GitHub
Skip to content

KoryNunn/discriminate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

discriminate

A data validator with an idiomatic API

Example:

Creating Discriminators:

var discriminate = require('discriminate');
var { Required, Custom, Maybe, And, Or } = discriminate;

const Email = discriminate(discriminate.And(
    String,
    Custom(([path, name], value, callback) =>
        value.match(/^.+@[^.].*?\..*[^.]$/) ? callbacl(null, value) : callback(`${path[1]} must be an email`)
    )
));

const User = discriminate({
    firstName: String,
    surname: String,
    email: Maybe(Email),
    nickname: Maybe(String, 'Buddy'),
    age: Number
});

function greetUser(maybeNotAValidUser){
    User(maybeNotAValidUser, function(error, user){
        if(error){
            /*
                error will look like:
                {
                    message: ...
                    errors: [
                        { path: 'path.in.schema', message: ... }
                    ]
                }
            */
            return;
        }

        // user will be valid at this point.
    });
}

Available base types (BaseType):

value type constructors are valid, eg:

String, Number, Boolean

Extra types provided by discriminate:

Maybe(Type, default<optional>)

Ensure a value is either Type or null/undefined

And(Types...)

Ensures a value is every one of Types.

Or(Types...)

Ensures a value is any one of Types.

Custom(fn<value>)

Ensures fn(value) does not throw.

The value returned from fn will be the result of the type instansiation.

Required(Type)

Prints a nicer error if a value is null or undefined, eg: 'age is required.'

Any()

Allow any value

List(Type, minLength<optional>, maxLength<optional>)

Ensures a value is an array where each item matches Type.

Cast(BaseType)

Casts a value to a BaseType.

About

A data validator with an idiomatic API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy