New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

validex

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validex

The fastest and simple validator

latest
npmnpm
Version
1.1.6
Version published
Weekly downloads
3
200%
Maintainers
1
Weekly downloads
 
Created
Source

validex

The fastest and simple validator.

NPM JavaScript Style Guide

Install

npm install --save validex

use it with react react-validex

Usage

import validex from 'validex'

const validator = validex(data: Object, options: Object)

if(validator.validate()){

}

Options

ValidateDescription
requiredvalue must be required. (boolean)
typestring,number,array,object,bool check the value type. (string)
mincheck the string's minimum length. (integer)
maxcheck the string's maximum length. (integer)
emailvalue must be an email. (boolean)
urlvalue must be an url. (boolean)
equalvalue must be equal. (string)
notEqualWithcheck the value equal with another field. (string) another field name
lowercaseall the characters must be lowercase. (boolean)
uppercaseall the characters must be uppercase. (boolean)
capitalizerequired the first characters uppercase. (boolean)
hexcheck the value is hex or not. (boolean)
maxNumberRangecheck the maximum number range. (integer)
minNumberRangecheck the minimum number range. (integer)
maxWordscheck the maximum words length. (integer)
minWordscheck the minimum words length. (integer)
notAllowedCharscheck the value contained the some characters or not. (string) example: "abcd.#@"
notAllowedCharactersa-zA-Z characters are not allowed. (boolean)
notAlloweNumber0-9 numbers are not allowed. (boolean)
notAllowedSpecialChars`!@#$%^&*()_+-=[]{};':"\
notAllowedWordscheck the value contained the some words or not. (string) example: "Hello World, Propgrammer, any"
comparevalidate the value by your self. (function)
regexcompare with regular expression. (Regex)
strongPassword8 or more characters with a mix of letters, numbers & symbols. (boolean)
mediumPassword6 or more characters with a mix of letters, numbers & symbols. (boolean)
oneOfcheck the value is included. (array)
oneOfTypecheck the value multiple types. (array)
shapecheck an object field with multiple properties. (object)
excatcheck an object field with multiple properties. (object)
datecheck the value is date or new Date Object. (boolean)
nameAliasjust replace the field name. (string)

Example

import validex from  'validex'

const data = {
	user_name: 'Jhon Doe',
	user_email: 'yourname@example.com',
	user_age: 20
}
const schema = {
	user_name: {
		nameAlias: "User Name",
		required: true,
		type: 'string',
		capitalize: true,
		notAllowedSpecialChars: true
	},
	user_email: {
		nameAlias: "Email",
		email: true,
		lowercase: true
	},
	user_age: {
		nameAlias: "Age",
		type: 'number',
		minNumberRange: 18,
		maxNumberRange: 30
	}
}
const validator =  validex(data, schema)

const isValidate = validator.validate()

if(isValidate){
	// .....
}

// Or You can the hasError function
if(!validator.hasError()){
	// .....
}


Schema format

In the schema property you can pass single option or you can pass an array. the array contained two indexes

    • Property type
    • Error message

Example


const schema = {
	user_name: {
		required: [true, new Error("$field must be required")], // or you can pass the arra
		type: ['string', new Error("$field must be type of string")],
		min: [10, new Error("$field minumum characters of $compare")], // $compare will be replaced with 10
		max: 20,
		notAllowedSpecialChars: true,
	}
}

Validator Methods

NameDescription
setSet the data and schema individually. set(fieldName, value, shema) Schema will be an Object
hasErrorcheck the error is exists or not @return boolean. you can also check the field error with this hasError('user_name')
getErrorget all the errors with an object. If you pass the field name then you can get just the field error message
removeErrorif you want you can remove the field error. removeError('user_name', 'min') it will remove just user_name min type error
validatevalidating the data.

Validator Callback

It will call when validate and removeError function call

validator.callback = (type, validator) => {
	if(type === 'removeError'){

	}
}

Compare

Custom validation with compare. @return type boolean.

{
	compare: (value, options) => {
		if(typeof value !== 'string'){
			return new Error("$field must be string")
		}
	}
}

Individually Import


import {
	isType,
	isEmail,
	isEqual,
	isUrl,
	isHex,
	isUpperCase,
	isLowerCase,
	isCapitalize,
	minWords,
	maxWords,
	minNumberRange,
	maxNumberRange,
	notAllowedChars,
	notAllowedCharacters,
	notAllowedSpecialChars,
	notAllowedWords,
	notAllowedNumber,
	regex,
	strongPassword,
	mediumPassword,
	oneOf,
	oneOfType,
	exact,
	shape
} from 'validex'

// every function has two arguments
// first is value second is compare value

isType(value, 'numbe|object|array|string|bool')
isEmail(value)
isEqual(value, compareValue)
isUrl(value)
isUpperCase(value)
isLowerCase(value)
isCapitalize(value)
isHex(value)
minWords(value, length)
maxWords(value, length)
minNumberRange(value, length)
maxNumberRange(value, length)
notAllowedChars(value, characters)
notAllowedCharacters(value)
notAllowedNumber(value)
notAllowedSpecialChars(value)
notAllowedWords(value, 'Hello World, Programmer')
regex(value, expression),
strongPassword(value),
mediumPassword(value),
oneOf(value, ['public', 'private']),
oneOfType(value, ['string', 'number']),
shape({
	name: 'any',
	email: 'any@example.com'
}, {
	name: {
			/// validex props
	}
}),

exact({
	name: 'any',
	email: 'any@example.com'
}, {
	name: {
			/// validex props
	}
}),

Some utilities functions

import {isObject, isArray, isNumber, isInteger, isString, isBool} from 'validex'

Keywords

validator

FAQs

Package last updated on 27 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts