0.0.1 • Published 3 years ago
js-validation-helper v0.0.1
JavaScript Validator
a validation helper to easily validate your data
Installation
with npm
npm install @baabouj/validatorwith yarn
yarn add @baabouj/validatorwith pnpm
pnpm install @baabouj/validatorUsage
first let’s import the validator from the package :
using ES modules
import validator from "@baabouj/validator";using CommonJs
const validator = require("@baabouj/validator");Now that you imported it, the validator provides you with 2 function :
- validate
used to validate fields within an object
const validated = validator.validate(
  {
    name: "John Doe",
    age: 20,
    email: "john.doe@gmail.com",
    password: "secret",
  },
  {
    name: "required", // with single rule
    age: "required|number|gt:18", // with multiple rules
    email: ["required", "email"], // with an array of rules
    password: [
      ["required", "Password is required"],
      ["min:6", "Password must be at least 6 characters long"],
    ], // with custom error messages
  }
);on case of validation didn’t succussed, the validate function returns an object containing each unvalidated field as a key and an array of error messages for that field as a value.
on validation succussed, the validate function returns null.
- check
used to validate a single field
const checked = validator.check(23, "number|gte:18");
// or
const checked = validator.check(23, ["number", "gte:18"]);
// or provide your custom error message
const checked = validator.check(23, [
  "number",
  ["gte:18", "You must be at least 18 years old"],
]);on case of validation didn’t succussed, the check function returns an array of error messages.
on success, the check function returns null.
Rules
- requiredchecks if the field is not empty
- emptychecks if the field is empty
- emailchecks if the field is a valid email address
- phonechecks if the field is a valid phone number
- urlchecks if the field is a valid url
- length:{length}checks if the field’s length is equal to the given length
- min:{length}checks if the field’s length is greater than the given length
- max:{length}checks if the field’s length is less than the given length
- numberchecks if the field is a number
- booleanchecks if the field is a boolean
- stringchecks if the field is a string
- arraychecks if the field is an array
- objectchecks if the field is an object
- functionchecks if the field is a function
- nullchecks if the field is null
- undefinedchecks if the field is undefined
- in:{item},{item} (...)checks if the field is in the given list
- nin:{item},{item} (...)checks if the field is not in the given list
- eq:{value}checks if the field is equal to the given value
- neq:{value}checks if the field is not equal to the given value
- gt:{value}checks if the field is greater than the given value
- gte:{value}checks if the field is greater than or equal to the given value
- lt:{value}checks if the field is less than the given value
- lte:{value}checks if the field is less than or equal to the given value
- between:{min},{max}checks if the field is between the given values
- contains:{value}checks if the field contains the given value
- sw:{value}checks if the field starts with the given value
- ew:{value}checks if the field ends with the given value
- regex:{regex}checks if the field matches the given regex pattern
- not:{rule}checks if the field does not match the given rule
0.0.1
3 years ago