0.1.2 • Published 6 years ago

hzob-validator v0.1.2

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Hzob Validator

This package is a simple and lightweight object validation in node.js, react.js, angular or any other javascript project that uses es6 modules.

Instalation

npm install hzob-validator --save

First steps

It's really simple to get started, just import the validator function along with some rules, and you are ready to go!

import { validator, required, minValue } from "hzob-validator";

const myData = {
  "name": "John Doe",
  "age": 16
};

  const rules = {
  "name": [required()],
  "age": [required(), minValue({value: 18, message: "You gotta be at least {value} years old!"})]
}

const result = validator(myData, rules);

console.log(result);
// result:
//   { "age": "You gotta be at least 18 years old!" }

Rules

Most rules accept an object as an argument with some custom configuration:

required

ParameterTypeDescription
messagestringThe displayed error message
customEmptyany, arraycustom value or an array of values that should be considered "empty"
onlyIffunctionA function that receives 2 parameters, the first one is the value of the equivalent key on the data object, the second is the whole data object, it should return a boolean value that confirms if the required rule is applied

strLength

ParameterTypeDescription
minValueintThe min length allowed
maxValueintThe max length allowed
minMessagestringThe displayed error message, it can contain {value} witch will be replaced by the min length expected
maxMessagestringThe displayed error message, it can contain {value} witch will be replaced by the max length expected

minValue

ParameterTypeDescription
valueintThe minimal value allowed
messagestringThe displayed error message, it can contain {value} witch will be replaced by the minimal value expected

maxValue

ParameterTypeDescription
valueintThe max value allowed
messagestringThe displayed error message, it can contain {value} witch will be replaced by the max value expected

email

ParameterTypeDescription
messagestringThe displayed error message