Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@types/joi

Package Overview
Dependencies
Maintainers
1
Versions
69
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@types/joi

Stub TypeScript definitions entry for joi, which provides its own types definitions

  • 17.2.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
98K
decreased by-44.84%
Maintainers
1
Weekly downloads
 
Created

What is @types/joi?

@types/joi provides TypeScript type definitions for the Joi validation library, which is used to validate JavaScript objects and data structures.

What are @types/joi's main functionalities?

Basic Validation

This feature allows you to define a schema and validate an object against it. The schema can include various rules such as string patterns, number ranges, and required fields.

const Joi = require('joi');

const schema = Joi.object({
  username: Joi.string().alphanum().min(3).max(30).required(),
  password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')),
  birth_year: Joi.number().integer().min(1900).max(2013)
});

const { error, value } = schema.validate({ username: 'abc', birth_year: 1994 });

Custom Error Messages

This feature allows you to customize error messages for specific validation rules, making it easier to provide user-friendly feedback.

const schema = Joi.object({
  username: Joi.string().min(3).message('Username must be at least 3 characters long')
});

const { error, value } = schema.validate({ username: 'ab' });
if (error) {
  console.log(error.details[0].message); // 'Username must be at least 3 characters long'
}

Nested Object Validation

This feature allows you to validate nested objects, ensuring that all required fields within the nested structure are present and valid.

const schema = Joi.object({
  user: Joi.object({
    username: Joi.string().required(),
    password: Joi.string().required()
  }).required()
});

const { error, value } = schema.validate({ user: { username: 'abc', password: '123' } });

Array Validation

This feature allows you to validate arrays, including the types of items within the array and constraints on the array itself, such as minimum length.

const schema = Joi.object({
  tags: Joi.array().items(Joi.string().min(2)).min(1).required()
});

const { error, value } = schema.validate({ tags: ['node', 'express'] });

Other packages similar to @types/joi

FAQs

Package last updated on 14 Jan 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc