@acai/validator

A customizable validator for the Açaí framework

Usage no npm install needed!

<script type="module">
  import acaiValidator from 'https://cdn.skypack.dev/@acai/validator';
</script>

README

Açai Validator Module

Build Status Support

A customizable validator for the Açaí framework. You can easily add your own rules and extend the capabilities of our validator.

Usage

import { addRule } from "@acai/validator";

// first thing we need is to extend the base Validator class
class RegisterValidator extends Validator {
    protected getSchema () {
        return {
            email: [ "required", "email" ],
            password: [ "required", "confirmed" ],
        };
    }
}

// now we get the fields to be validated
const fields = {
    email: "not an email",
    password: "not confirmed password",
}

const validation = new RegisterValidator(fields);
const errors = validation.errors;

// this will be filled if the validation failed
if (errors) {
    console.warn(errors);
}

Custom rules

import { addRule } from "@acai/validator";

addRule("password", {
    /** Validation to check if value should pass */
    onValidate	? (value: unknown, key: string, fields: Record<string, unknown>, args?: string[]) => {
        return /* your validation here */
    }
    /** Message to be returned in case of validation error */
    onError		? (value: unknown, key: string, fields: Record<string, unknown>, args?: string[]) => {
        return `${key} is not a valid password`;
    }
});