joi-plus v1.4.0
Joi-Plus
joi - v17.4.0 Making the most powerful schema description language and data validator for JavaScript slightly more powerful.
Introduction
Joi.string().escape() * replace
&,>,<,",',\,/and`with HTML entities.Joi.string().unescape() * replace
&|>|<|"|$|/|\|`HTML entities with characters.Joi.string().sanitize(function) sanitize string using function that takes a string as a parameter. returns sanitize string
Joi.string().alpha() * Requires the string value to only contain alphabetic characters.
Joi.string().numeric() * Requires the string value to only contain numeric characters.
Joi.string().decimal(digit, decimal) * Requires the string value to be a valid decimal number.
Joi.string().base32() * Requires the value to be a valid base32 string.
Joi.string().countryCode(type) * Requires the value to be a valid ISO
alpha-2or ISOalpha-3country code.Joi.string().password(policy) Requires the string value to match policy. policy.min - password minimum length, default 8. policy.max - password maximum length, default 24. policy.lowercase - if true, password requires lowercase. policy.uppercase - if true, password requires uppercase. policy.number - if true, password requires number. policy.special - if true, password requires special character. policy.count If defined, password is required to pass the number of requirements. If undefined, password is required to pass all of requirements.
Joi.string().match(reference) Requires the string value to match the reference. Removed after validation.
Joi.string().contain(seed, index) Requires the string value to contain the seed. If index is defined, position of the seed in the string must match the index. * Set index to -1 to match from end of string.
Quick Start
Installation
$ npm i joi-plusInitialization
const Joi = require('joi-plus');Usage
const schema = Joi.object({
username: Joi.string()
.min(8)
.max(20)
.alpha()
.required(),
email: Joi.string()
.email()
.required(),
password: Joi.string()
.password({
min: 8,
max: 120,
lowercase: true,
uppercase: true,
number: true,
special: true,
count: 3
})
.required(),
repeat_password: Joi.string()
.match('password')
.required(),
base32_encoded: Joi.string()
.base32()
.required(),
country: Joi.string()
.countryCode('alpha-2')
.required(),
contact_number: Joi.string()
.min(2)
.max(20)
.numeric()
.required(),
salary: Joi.string()
.decimal(11,2)
.required()
});The above schema defines the following constraints:
usernamea required string at least 8 characters long but no more than 20 * must contain only alphabetic charactersemaila required string a valid email address stringpassworda required string at least 8 characters long but no more than 120 must contain at least 3 of the 4 requirements one lowercase character one uppercase character one numeric character one special character space ! " # $ % & ' ( ) * + , - . : ; < = > ? @ \ ^ _ ` { | } ~repeat_passworda required string must matchpassword* will be removed after validationbase32_encodeda required string a valid base32 stringcountrya required string must be a valid ISO 'alpha-2' country codecontact_numbera required string at least 2 characters long but no more than 20 * must contain only numeric characterssalarya required string must be a valid decimal number with up to 11 digits with 2 decimal places
Sanitize
Using Joi.string().sanitize() with sanitization libraries such as sanitize-html
const sanitizeHtml = require('sanitize-html');
const schema = Joi.object({
escape: Joi.string()
.escape(),
unescape: Joi.string()
.unescape(),
sanitize: Joi.string()
.sanitize(sanitizeHtml)
});
const { error, value } = schema.validate({
escape: '<escape>',
unescape: '<unescape>',
sanitize: 'Hello,<script>evil()</script> I am Good.'
});
console.log(value);
/*
{
escape: '<escape>',
unescape: '<unescape>',
sanitize: 'Hello, I am Good.'
}
*/4 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago