A Savory class validator for Deno.

Made for Deno CI codecov

Usage

Create a Class using TypeScript Decorators and run validateObject.

If you need more built-in validators, please open an issue or create merge request

import {
    createValidator,
    IsEmail, // this is a decorator (PascalCase)
    isEmail, // this is a check function, called behavior (camelCase)
    IsString,
    LengthLowerOrEqual,
    validateObject,
} from 'https://deno.land/x/validatte/mod.ts';

// Custom validator with custom error message containing argument. <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>o</mi><mi>n</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>a</mi><mi>i</mi><mi>n</mi><mi>t</mi><mn>1</mn><mi>w</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>b</mi><mi>e</mi><mi>r</mi><mi>e</mi><mi>p</mi><mi>l</mi><mi>a</mi><mi>c</mi><mi>e</mi><mi>d</mi><mi>b</mi><mi>y</mi><mi>f</mi><mi>i</mi><mi>r</mi><mi>s</mi><mi>t</mi><mi>a</mi><mi>r</mi><mi>g</mi><mi>u</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi mathvariant="normal">.</mi></mrow><annotation encoding="application/x-tex">constraint1 will be replaced by first argument. </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">n</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">ain</span><span class="mord mathnormal">t</span><span class="mord">1</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mord mathnormal">b</span><span class="mord mathnormal">ere</span><span class="mord mathnormal" style="margin-right:0.01968em;">pl</span><span class="mord mathnormal">a</span><span class="mord mathnormal">ce</span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal">rs</span><span class="mord mathnormal">t</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">gu</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord">.</span></span></span></span>constraint2 by second etc...
const IsWhiteListedEmailDomain = createValidator(
    (whiteListedDomain: string) => {
        return (prop) => {
            if (isEmail(prop)) {
                return false;
            }
            return prop.indexOf(`@${whiteListedDomain}`) !== -1;
        };
    },
    'Email shoud be from $constraint1 domain',
);

class User {
    @IsString()
    @IsEmail()
    @IsWhiteListedEmailDomain('mydomain.com')
    email!: string;
}

// ---------- //

// Valid
const user = new User();
user.email = 'myemail@mydomain.com';
console.log(validateObject(user, User));
// User { email: 'myemail@mydomain.com' }

// ---------- //

// Invalid
const failingUser = new User();
failingUser.email = 'wrongemail@email.com';
console.log(validateObject(failingUser, User));

/*
[
    {
        property: 'email',
        errorMessage: `Email shoud be from mydomain.com domain`,
        constraints: ['mydomain.com'],
    },
];
*/

Come with us on this awesome journey

We always welcome contributors, feel free to submit a new feature or report a bug on our Github Repository and join our discord

License

MIT