Validation Methods

Abolish provides 4 validation Methods: validate, attempt, check, test. All with async support.

MethodDescription
validate, validateAsyncValidate objects. Go Lang error handling style. No Error thrown.
attempt, attemptAsyncValidate variable, Throw error when validation fails.
check, checkAsyncValidate variable but use Go Lang error handling style. No Error thrown.
test, testAsyncValidate variable, return boolean. true for pass and false for fail.

validate

The validate method is for validating objects. unlike other methods,
it takes an Object of {key: rules} structure as rules.

const data = {
    email: "[email protected]",
    password: undefined,
    referrer: "john"
};

const [err, validated] = Abolish.validate(data, {
    email: "required|typeof:string",
    password: "required|typeof:string"
});

console.log([err, validated]);
// WILL LOG
[
    // The error object
    {
        key: "password",
        type: "validator",
        validator: "required",
        message: "Password is required.",
        data: null
    },
    {} // validated will be empty when error.
];
// Assuming password is `password2020` not `undefined`
// WILL LOG
[
    // error is false
    false,
    // only validated keys
    { email: "[email protected]", password: "password2020" }
];

$include

From the example above, you will notice that only keys defined in rules are returned in validated. i.e. referrer which is present in the data object, is not included in the returned validated object.

To include fields in the validated object, $include is a super key that lets you define keys you want to be included in the validated object like so:

const data = {
    email: "[email protected]",
    password: "password",
    referrer: "john"
};

const [err, validated] = Abolish.validate(data, {
    email: "required|typeof:string",
    password: "required|typeof:string",
    $include: ["referrer"]
});

console.log([err, validated]);

// WILL LOG
[
    // error is false
    false,
    // `referrer` is included.
    {
        email: "[email protected]",
        password: "password",
        referrer: "john"
    }
];

Wildcard

The Wildcard rules * or $ can be used to define rules that will apply to all keys defined in a rules object.

Abolish.validate(object, {
    "*": "required|typeof:string",
    email: true,
    password: true
});

// will be converted to
Abolish.validate(object, {
    email: "required|typeof:string",
    password: "required|typeof:string"
});

// With extra validators
Abolish.validate(object, {
    "*": "required|typeof:string",
    email: "email",
    password: "minLength:6"
});

// will be converted to
Abolish.validate(object, {
    email: "required|typeof:string|email",
    password: "required|typeof:string|minLength:6"
});

validateAsync

The async method for #validate

attempt

The attempt method is for validating variables. it throws an error when validation fails and returns the validated value on success.

it takes the syntax of validate(variable, rules);

const bool = Abolish.attempt(0, "boolean"); // pass
// bool === false
const number = Abolish.attempt("1234", "number"); // pass
// number === 1234

const age = Abolish.attempt(12, "min:18"); // fail
// Throw Error: Variable is too small. (Min. 18)

attemptAsync

The async method for #attempt

check

Check is for validating variables but unlike attempt does not throw an error. It returns a Go Lang error typeof syntax like validate

const [error, age] = Abolish.check(12, "min:18");

// error === {
// key: 'variable',
// type: 'validator',
// validator: 'min',
// message: 'Variable is too small. (Min. 18)',
// data: null
// },

// age === undefined

const [error, age] = Abolish.check(28, "min:18");
// error === false
// age === 28

checkAsync

The async method for #check

test

The test method does not throw an exception or return an error. It returns a boolean value. If the validation fails, it returns false else true.

Abolish.test(12, "min:18"); // false
Abolish.test("My Name", "minLength:3|maxLength:20"); // true
Abolish.test([], "typeof:array"); // true

testAsync

The async method for #test

Last Updated:
Contributors: trapcodeio, TrapCode, trapcodeio