0.0.2 • Published 6 years ago

@uzelux/validator v0.0.2

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

#Validator

A simple Schema based Validator

Uzelux's note

This project is WIP, derived from one of my projects' submodule

Please kindly report any issue/bugs discovered when use

Usage

const { Validator, ValidatorTypes, ValidatorError } = require('@uzelux/validator');

const validator = new Validator({
  iAmString: {
    type: ValidatorTypes.STRING,
    length: 10,
  }
});

const iAmString = 'sure i am';

try {
  const result = validator.validate({iAmString});
  console.log(result); // true
} catch (e) {
  console.log(e.name); // ValidatorError;
  consoel.log(e.message) // Error message
}

Options Available

Currently the schema only provides single layer structure. With the parameter name as the key and the criteria as the body.

const validator = new Validator({
  parameterName: {
    type: ValidatorTypes.STRING, // see ValidatorTypes
    contains: 'must have', // value must contain the data specified 
    regex: /\w+/g, // match regular expression
    length: 10, // length after toString()
    within: ['possible1', 'possible2'], // value exist in array provided
    required: false // all parameters are treated as required by default, 
                    // i.e. must be provided in validate() 
  }
})

Types Supported

Currently the following types are provided under the class ValidatorTypes

NameTypeAlias
BOOLEANbooleanBOOL
NULLnull
UNDEFINEDundefined
NUMBERnumberFLOAT, DOUBLE
BIGINTbigint
STRINGstring
SYMBOLsymbol
OBJECTobject
FUNCTIONfunction
ARRAYarray
DATEdate
ERRORerror
INTEGERinteger
NANNaN

Errors

NameTypeThrown By
MissingParameterValidatorErrorAssert Require
InvalidTypeValidatorErrorAssert Type
DoesNotContainValidatorErrorAssert Contain
NotInRangeValidatorErrorAssert Within
RegexMismatchValidatorErrorAssert Regex
LengthMismatchValidatorErrorAssert Length