cb-valid
v1.0.3
Published
Validator very easy to use, and implement! (email, url, password, .etc)
Downloads
3
Readme
cb-valid
It is very easy to implement in your project. Allows the use of non-decorator based validation. Use validator.js internally to perform the validation.
Installation
npm install cb-valid --save
Note: Please use at least npm@6 when using cb-valid. From npm@6 the dependency tree is flattened, which is required by
cb-valid
to function properly.
Usage
// Valid for typescript
import CbValid from 'cb-valid'
// Valid for javascript vanilla
const CbValid = requrie("cb-valid").default
// We declare a test string to validate
// We clearly know that the variable testValue does not have a valid email!
// why will you show us the messages entered!
const testValue = "yonicalsin"
// Here you will add the validation you need
const exps = [
// We will validate if testValue is an email
{
// The value (isEmail) is to validate an email
name: "isEmail",
// Message in case the email is invalid!
message: "Email is invalid !"
},
{
// The value (minLength) is to validate at least 5 characters
name: "minLength",
// This attribute is necessary to validate the minimum number of characters
data: {
min: 5
},
// Message in case you do not meet the required conditions
message: "Email needs at least 4 characters!"
},
{
// The value (maxLength) is to validate a maximum of 10 characters
name: "maxLength",
// This attribute is necessary to validate the maximum number of characters
data: {
max: 9
},
// Message in case you do not meet the required conditions
message: "Email needs a maximum of 10 characters!"
}
]
const res = CbValid(testValue, exps)
/**
* @Return
* */
/*
{
status: false,
messages: [
"Email is invalid !",
"Email needs a maximum of 10 characters!"
]
}
*/
Passing options
Some functions require a data
parameter
export interface Data {
min: number
max: number
}
Validation errors
The cb-valid
method returns an array of ValidationError
objects. Each ValidationError
is:
{
status: Boolean; // Returns true if everything is correct, in case something is not correct it will return false
messages?: String[]; // Contains all nested validation errors of the property
}
Manual validation
There are several method exist in the Validator that allows to perform non-decorator based validation:
import { Validator } from "cb-valid";
// Validation methods
const validator = new Validator();
Length(value, {
min: @number,
max: @number
}) //
minLength(value, {
min: @number
})
maxLength(value, {
max: @number
})
isEmail(value) // Checks if the string is an email.
isURL(value) // Checks if the string is an url.
isFQDN(value) // Checks if the string is a fully qualified domain name (e.g. domain.com).
isEmpty(value) // Checks if given value is empty (=== '', === null, === undefined).
isNotEmpty(value) // Checks if given value is not empty (!== '', !== null, !== undefined).
isAscii(value) // Checks if the string contains ASCII chars only.
isBase64(value) // Checks if a string is base64 encoded.
isCreditCard(value) // Checks if the string is a credit card.
isCurrency(value) // Checks if the string is a valid currency amount.
isDecimal(value) // Checks if the string is a valid decimal value.
isFullWidth(value) // Checks if the string contains any full-width chars.
isHexadecimal(value) // Checks if the string is a hexadecimal number.
isHexColor(value) // Checks if the string is a hexadecimal color.
isISIN(value) // Checks if the string is an ISIN (stock/security identifier).
isISO31661Alpha2(value) // Check if the string is a valid ISO 3166-1 alpha-2
isISO31661Alpha3(value) // Check if the string is a valid ISO 3166-1 alpha-3
isISO8601(value) // Checks if the string is a valid ISO 8601 date. Use the option strict = true for additional checks for a valid date, e.g. invalidates dates like 2019-02-29.
isISSN(value) // Checks if the string is a ISSN.
isJSON(value) // Checks if the string is valid JSON (note: uses JSON.parse).
isJWT(value) // Checks if the string is valid JWT.
isLatLong(value) // Checks if the string is a valid latitude-longitude coordinate in the format lat,long
isLowercase(value) // Checks if the string is lowercase.
isMACAddress(value) // Checks if the string is a MAC Address.
isMongoId(value) // Checks if the string is a valid hex-encoded representation of a MongoDB ObjectId.
isMultibyte(value) // Checks if the string contains one or more multibyte chars.
isPort(value) // Check if the string is a valid port number.
isSurrogatePair(value) // Checks if the string contains any surrogate pairs chars.
isUppercase(value) // Checks if the string is uppercase.
isVariableWidth(value) // Checks if the string contains variable-width chars.
Backers
Stay in touch
- Author Yoni Calsin
- Twitter Yoni Calsin
Release notes
See information about breaking changes and release notes.
cb-valid v0.0.1
License
Nest is MIT licensed.