@hyron/validator
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

Build status Gitter npm

@hyron/validator is a powerful library for validate input of function for more security

Features

  • Validate by comment
  • Support validate value
  • Support validate structure
  • Support for Hyron Framework
  • High performance

Usage

Step 1 : Install

By NPM :

npm i @hyron/validator

By YARN :

yarn add @hyron/validator

Step 2 : Validate data

To check input data with complex condition, using getConditionChecker(..) function

const validator = require("@hyron/validator");

var checker = validator.getConditionChecker({
    type : "number",
    lt : 10,
    gt : 0
});

checker(4); // allow
checker("hi"); // has error
checker(12); // has error

To check input structure of input. using getStructureChecker(..) function. For more info, checkout structure parser engine

const validator = require("@hyron/validator");

var checker = validator.getStructureChecker(`
{key1(string)}
`);

checker({key1:null}); // allow
checker({key1: 34}); // has error
checker({key1:"hi"}); // allow

validator also support to used comment for validator to validate function arguments easier. View Validate by Comment for more info

function demo(arg1){
    /**
     * @check arg1 {type:string, size:20} - check if arg1 is string with max length is 20
     */
    ...
}

API Reference

function registerValidator( func, eventName ? ) : (args) => void

Used to register validate for a function by comment. Valid options include :

  • @check : Check if the input parameter meets the condition
  • @ignore : remove values from input if matched
  • @accept : filter values from input if matched
  • @valid : Only accept input parameters when it matches correctly

To know more about how to write validate condition, please visit guide

params

  • func ( function ) : target function will be used to parser validate condition
  • eventName ( string ) : a name represent for this validator. Default is func.name

return

  • validator ( function (args)=>void ) : a function that could be used to check function input data

function getValidator( eventName ) : (args) => void

Used to get a registered validator that have been registered before to check input data

params

  • eventName ( string ) : a name represent for this validator. Default is func.name

return

  • (args)=>void : a function that could be used to check function input data

function getConditionChecker( conditionMap ) : checker

get checker by condition to validate input data

params

  • argsName ( string ) : a name represent for input data

return

  • (input)=>boolean : a function that could be used to check function input data

function getStructureChecker ( struct, onChecked? ) : (input)=>void

get checker by condition to validate input structure of data used structure parser engine

params

  • struct ( string ) : a structure that defined for input data
  • onChecked ( function ) : a function that will be called for each time data was check

return

  • (input)=>void : a function that could be used to check input structure

Readme

Keywords

Package Sidebar

Install

npm i @hyron/validator

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

65.1 kB

Total Files

18

Last publish

Collaborators

  • spatocode
  • thangphung