validify

validify is a Validation Class that helps you to validate your incoming data

Examples

Initialize

import {validator} from "https://deno.land/x/validify@v0.1.1/mod.ts";
// or you can use rules one by one
import {
    isRequired,
    isString,
    isNumber,
    isArray,
    isEmail,
    isUrl,
    isIp,
    min,
    max,
    lessThan,
    lessThanOrEqual,
    greaterThan,
    greaterThanOrEqual,
    isIn,
    notIn,
    sizeOf,
    startsWith,
    endsWith,
    isAlpha,
    isAlphanumeric
} from "https://deno.land/x/validify@v0.1.1/mod.ts";

Custom error messages

You can use sample custom error messages from en.json file

const customErrorMessages = JSON.parse(Deno.readTextFileSync("./en.json"))
validator.setErrorMessages(customErrorMessages)

If you don’t pass custom error messages object the errors will look like this by default:

There is an error in field: {field}

Use

// Data to validate
const input = {
    email: 'info@codethereal.com',
    url: 'codethereal.com',
    address: [
        {
            name: 'Address line 1',
            location: 'Turkey, Istanbul'
        },
        {
            name: 'Address line 2',
            location: 'Turkey, Istanbul'
        }
    ],
    detail: {
        job: 'Software Developer',
        salary: 10.000,
        currency: 'USD'
    },
    status: 1,
    numbers: [1,2,3,4,5,6]
}

// Rules to apply
const rules = {
    email: 'string|required|email|min:3|max:40',
    url: 'string|required|url|sw:http,https|ew:com,net',
    address: {
        name: 'string|required',
        location: 'string|required'
    },
    detail: {
        job: 'string',
        salary: 'number',
        currency: 'string|nin:ETH,BTC|in:USD,EUR,TRY'
    },
    status: 'number|required|in:0,1',
    numbers: 'required|array|max:5'
}

// Errored fields name to show prettier messages to user
const fields = {
    email: 'Email',
    address: {
        name: 'Address Name',
        location: 'Address Location'
    },
    detail: {
        job: 'Job',
        salary: 'Salary',
        currency: 'Currency'
    },
}

// Run `validate` with arguments and chain more data if you want with 
// `single` function and then get the errors at last with `getErrors`
const errors = validator
    .validate(input, rules, fields)
    .single('192.2222222', 'ip', 'IP')
    .getErrors()

// After `getErrors` errors will be empty and ready for another validation
const errors2 = validator
    .single(15, 'number|gt:5|lt:10', 'Some Number')
    .single('ab', 'alpha:en-US', 'Some String')
    .getErrors()

// Check for any errors
if ([...errors, ...errors2].length > 0) { }

// Use pure methods
if (isEmail('info@codethereal.com')) { }
if (isAlphanumeric('abc213')) { }

Add Custom Rules

// You can also pass array of objects to add multiple rules
validator.addRule({
    name: 'equal',
    message: 'The {field} field must be equal to {param}',
    filter: (value: unknown, param: string) => {
        return value === param
    }
})

validator.single('codetherea', 'equal:codethereal', 'Custom Field')

If you don’t want errors to be empty for next validations after calling getErrors

.getErrors({empty: false})

In the example data we have more than one address but even if all of them fails it returns you single error. If you want to get the same errors for each address object

.getErrors({unique: false})