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.2.0/mod.ts";
// or you can use rules one by one
import {
    endsWith,
    greaterThan,
    greaterThanOrEqual,
    isAlpha,
    isAlphanumeric,
    isArray,
    isAscii,
    isEmail,
    isIn,
    isIp,
    isNumber,
    isPortNumber,
    isRequired,
    isString,
    isUrl,
    lessThan,
    lessThanOrEqual,
    max,
    min,
    notIn,
    sizeOf,
    startsWith
} from "https://deno.land/x/validify@v0.2.0/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: {
        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: {
        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: {
        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()

// Use pure methods
if (isEmail('info@codethereal.com')) { }
if (isAlphanumeric('abc213', 'en-US')) { }
if (isPortNumber(65)) { }

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})