validify

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

Examples

Initialize

import {validation, ErrorMessages, Fields, Rules} from "https://deno.land/x/validify@v0.0.6/mod.ts";

Custom error messages

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

const customErrorMessages: ErrorMessages = JSON.parse(Deno.readTextFileSync("./en.json"))
validation.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: Rules = {
    email: 'string|required|email|min:3|max:40',
    url: 'string|required|url',
    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: 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 = validation
    .validate(input, rules, fields)
    .single('name surname', 'min:5|required', 'The Name')
    .getErrors()
/*
[
  "The url field must contain a valid URL.",
  "The numbers field cannot exceed 5 in length.",
  "The test field must be at least 5 in length."
]
*/

// After `getErrors` errors will be empty and ready for another validation
const errors2 = validation
    .single('info@codethereal', 'required|email', 'E-Mail')
    .single('192.2222222', 'ip', 'IP')
    .single(15, 'number|gt:5|lt:10', 'Some Number')
    .single('foobarbaz', 'sw:foo,bar|ew:bar,baz', 'Some String')
    .getErrors()
/*
[
  "The E-Mail field must contain a valid email address.",
  "The IP field must contain a valid IP.",
  "The Some Number field must contain a number less than 10."
]
*/

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

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