Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@types/hapi__joi
Advanced tools
TypeScript definitions for @hapi/joi
@types/hapi__joi is a TypeScript type definition package for the Joi library, which is used for data validation. It provides type definitions to ensure type safety and better development experience when using Joi in TypeScript projects.
Basic Validation
This feature allows you to define a schema for basic string validation, specifying minimum and maximum length, and whether the field is required.
const Joi = require('@hapi/joi');
const schema = Joi.string().min(3).max(30).required();
const result = schema.validate('example');
console.log(result);
Object Validation
This feature allows you to define a schema for validating objects with various properties, including strings, numbers, and custom patterns.
const Joi = require('@hapi/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 result = schema.validate({ username: 'abc', birth_year: 1994 });
console.log(result);
Array Validation
This feature allows you to define a schema for validating arrays, specifying the allowed values for the array items.
const Joi = require('@hapi/joi');
const schema = Joi.array().items(Joi.string().valid('a', 'b', 'c'));
const result = schema.validate(['a', 'b']);
console.log(result);
Custom Validation
This feature allows you to define custom validation logic using a custom function.
const Joi = require('@hapi/joi');
const schema = Joi.string().custom((value, helpers) => {
if (value !== 'custom') {
return helpers.error('any.invalid');
}
return value;
});
const result = schema.validate('custom');
console.log(result);
Yup is a JavaScript schema builder for value parsing and validation. It is similar to Joi but offers a more modern API and better integration with React applications. It also has TypeScript support, making it a good alternative to @types/hapi__joi.
AJV (Another JSON Schema Validator) is a JSON schema validator that is very fast and supports JSON Schema draft-07. It is more focused on JSON schema validation compared to Joi, which is more general-purpose. AJV also has TypeScript support.
Zod is a TypeScript-first schema declaration and validation library. It is designed to be simple and easy to use, with a focus on TypeScript integration. Zod provides a more TypeScript-centric approach compared to Joi.
npm install --save @types/hapi__joi
This package contains type definitions for @hapi/joi (https://github.com/hapijs/joi).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/hapi__joi.
These definitions were written by Bart van der Schoor, Laurence Dougal Myers, Christopher Glantschnig, David Broder-Rodgers, Gael Magnan de Bornier, Rytis Alekna, Pavel Ivanov, Youngrok Kim, Dan Kraus, Anjun Wang, Rafael Kallis, Conan Lai, Peter Thorson, Will Garcia, Simon Schick, Alejandro Fernandez Haro, Silas Rech, Anand Chowdhary, Miro Yovchev, David Recuenco, Frederic Reisenhauer, Stefan-Gabriel Muscalu, and Steven Barnett.
FAQs
TypeScript definitions for @hapi/joi
We found that @types/hapi__joi demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.