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

joi-plus

v1.4.0

Published

Joi with extra rules for string and array.

Downloads

568

Readme

Joi-Plus

npm package npm license install size dependencies status

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 &amp; | &gt; | &lt; | &quot; | &#36; | &#47; | &#92; | &#96; 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-2 or ISO alpha-3 country 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-plus

Initialization

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:

  • username

    • a required string
    • at least 8 characters long but no more than 20
    • must contain only alphabetic characters
  • email

    • a required string
    • a valid email address string
  • password

    • a 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_password

    • a required string
    • must match password
    • will be removed after validation
  • base32_encoded

    • a required string
    • a valid base32 string
  • country

    • a required string
    • must be a valid ISO 'alpha-2' country code
  • contact_number

    • a required string
    • at least 2 characters long but no more than 20
    • must contain only numeric characters
  • salary

    • a 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: '&lt;unescape&gt;',
	sanitize: 'Hello,<script>evil()</script> I am Good.'
});

console.log(value);
/*
{
	escape: '&lt;escape&gt;',
	unescape: '<unescape>',
	sanitize: 'Hello, I am Good.'
}
*/