npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

validex

v1.1.6

Published

The fastest and simple validator

Downloads

4

Readme

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

| Validate | Description | | :--------------------- | :----------------------------------------------------------------------------------------------------- | | required | value must be required. (boolean) | | type | string,number,array,object,bool check the value type. (string) | | min | check the string's minimum length. (integer) | | max | check the string's maximum length. (integer) | | email | value must be an email. (boolean) | | url | value must be an url. (boolean) | | equal | value must be equal. (string) | | notEqualWith | check the value equal with another field. (string) another field name | | lowercase | all the characters must be lowercase. (boolean) | | uppercase | all the characters must be uppercase. (boolean) | | capitalize | required the first characters uppercase. (boolean) | | hex | check the value is hex or not. (boolean) | | maxNumberRange | check the maximum number range. (integer) | | minNumberRange | check the minimum number range. (integer) | | maxWords | check the maximum words length. (integer) | | minWords | check the minimum words length. (integer) | | notAllowedChars | check the value contained the some characters or not. (string) example: "abcd.#@" | | notAllowedCharacters | a-zA-Z characters are not allowed. (boolean) | | notAlloweNumber | 0-9 numbers are not allowed. (boolean) | | notAllowedSpecialChars | !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/ these characters are not allowed, (boolean) | | notAllowedWords | check the value contained the some words or not. (string) example: "Hello World, Propgrammer, any" | | compare | validate the value by your self. (function) | | regex | compare with regular expression. (Regex) | | strongPassword | 8 or more characters with a mix of letters, numbers & symbols. (boolean) | | mediumPassword | 6 or more characters with a mix of letters, numbers & symbols. (boolean) | | oneOf | check the value is included. (array) | | oneOfType | check the value multiple types. (array) | | shape | check an object field with multiple properties. (object) | | excat | check an object field with multiple properties. (object) | | date | check the value is date or new Date Object. (boolean) | | nameAlias | just replace the field name. (string) |

Example

import validex from  'validex'

const data = {
	user_name: 'Jhon Doe',
	user_email: '[email protected]',
	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

    1. Property type
    1. 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

| Name | Description | | :------------ | :------------------------------------------------------------------------------------------------------------------------- | | set | Set the data and schema individually. set(fieldName, value, shema) Schema will be an Object | | hasError | check the error is exists or not @return boolean. you can also check the field error with this hasError('user_name') | | getError | get all the errors with an object. If you pass the field name then you can get just the field error message | | removeError | if you want you can remove the field error. removeError('user_name', 'min') it will remove just user_name min type error | | validate | validating 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: '[email protected]'
}, {
	name: {
			/// validex props
	}
}),

exact({
	name: 'any',
	email: '[email protected]'
}, {
	name: {
			/// validex props
	}
}),

Some utilities functions

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