Skip to content

signarit/validate.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

validate.js

A simple, Laravelian, way to validate JavaScript forms.

Table of Contents

  1. Installation
  2. Introduction
  3. Nested Objects
  4. Wildcard Character
  5. Messages
  6. Rules

Installation

npm i @mikkjal/validate.js

Introduction

const form = {
    email: 'test@example.com',
    password: 'Haken 123',
    password_confirmation: '',
};

const rules = {
    email: 'required|email',
    password: 'required|min:8|confirmed',
};

const $v = validate(form, rules);
{
    "valid": false,
    "email": {
        "required": true,
        "email": true
    },
    "password": {
        "required": true,
        "min": true,
        "confirmed": false
    }
}

Nested objects

const form = {
    name: 'mikkjal',
    job: {
        title: 'Full-stack Developer',
        email: 'mikkjal@example.com',
    },
};

let rules = {
    name: 'required',
    job: {
        title: 'required|ends_with:Developer',
        email: 'required|email',
    },
    email: 'required|email',
};

// or

rules = {
    name: 'required',
    'job.title': 'required|ends_with:Developer',
    'job.email': 'required|email',
    email: 'required|email',
};

const $v = validate(form, rules);
{
    "valid": false,
    "name": {
        "required": true
    },
    "job": {
        "title": {
            "required": true,
            "ends_with": true
        },
        "email": {
            "required": true,
            "email": true
        }
    },
    "email": {
        "required": false,
        "email": false
    }
}

Wildcard Character

const form = {
    name: 'mikkjal',
    personal_email: '',
    job: {
        title: 'Full-stack Developer',
        email: 'mikkjal@example.com',
    },
};

const $v = validate(form, {
    '*email': 'email',
    'job.*': 'required',
});
{
    "valid": false,
    "personal_email": {
        "email": false
    },
    "job": {
        "email": {
            "required": true,
            "email": true
        },
        "title": {
            "required": true
        }
    }
}

Messages

const form = {
    email: 'test@example.com',
    password: 'Test123',
    password_confirmation: '',
    work: {
        email: '',
    },
};

const rules = {
    email: 'required',
    password: 'required|min:8|confirmed',
    '*email': 'email',
};

const messages = {
    'email.required': 'Email address is required',
    '*email.email': 'Not a valid email address',
    'password.min': 'The password needs to be at least 8 characters',
    'password.confirmed': 'The passwords do not match',
};

const $v = validate(form, rules, messages);
{
    "valid": false,
    "email": {
        "required": true,
        "email": true
    },
    "password": {
        "required": true,
        "min": false,
        "messages": ["The password needs to be at least 8 characters", "The passwords do not match"],
        "confirmed": false
    }
}

Rules

after

const rules = {
    dob: 'after:2000-01-01',
};

ascii

const rules = {
    text: 'ascii',
};

before

const rules = {
    dob: 'before:2000-01-01',
};

between

const rules = {
    age: 'between:18,122',
};

confirmed

const rules = {
    password: 'confirmed',
};

date

const rules = {
    dob: 'date',
};

different

const rules = {
    password: 'different:username',
};

email

const rules = {
    email: 'email',
};

ends_with

const rules = {
    text: 'ends:world!',
};

hex

const rules = {
    color: 'hex',
};

in

const rules = {
    name: 'in:hello,world',
};

ip

const rules = {
    ip: 'ip',
};

mac_address

const rules = {
    mac_address: 'mac_address',
};

max

const rules = {
    age: 'max:122',
    password: 'max:16',
};

min

const rules = {
    age: 'min:18',
    password: 'min:8',
};

not_in

const rules = {
    name: 'not_in:mikkjal,signar',
};

not_regex

const rules = {
    not_a_number: 'not_regex:/^[0-9]$/',
};

numeric

const rules = {
    age: 'numeric',
};

regex

const rules = {
    number: 'regex:/^[0-9]{6}$/',
};

required

const rules = {
    name: 'required',
};

required_with

const rules = {
    password: 'required_with:name,email',
};

required_with_all

const rules = {
    password: 'required_with_all:name,email',
};

required_without

const rules = {
    password: 'required_without:name,email',
};

required_without_all

const rules = {
    password: 'required_without_all:name,email',
};

same

const rules = {
    password: 'same:password_confirmation',
};

starts_with

const rules = {
    text: 'starts_with:Hello',
};

url

const rules = {
    url: 'url',
};

uuid

const rules = {
    uuid: 'uuid',
};

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published