What is @types/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.
What are @types/hapi__joi's main functionalities?
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);
Other packages similar to @types/hapi__joi
yup
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
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
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.
Installation
npm install --save @types/hapi__joi
Summary
This package contains type definitions for @hapi/joi (https://github.com/hapijs/joi).
Details
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/hapi__joi
Additional Details
- Last updated: Tue, 03 Sep 2019 21:47:43 GMT
- Dependencies: @types/hapi__joi
- Global values: none
Credits
These definitions were written by Bart van der Schoor https://github.com/Bartvds, Laurence Dougal Myers https://github.com/laurence-myers, Christopher Glantschnig https://github.com/cglantschnig, David Broder-Rodgers https://github.com/DavidBR-SW, Gael Magnan de Bornier https://github.com/GaelMagnan, Rytis Alekna https://github.com/ralekna, Pavel Ivanov https://github.com/schfkt, Youngrok Kim https://github.com/rokoroku, Dan Kraus https://github.com/dankraus, Anjun Wang https://github.com/wanganjun, Rafael Kallis https://github.com/rafaelkallis, Conan Lai https://github.com/aconanlai, Peter Thorson https://github.com/zaphoyd, Will Garcia https://github.com/thewillg, Simon Schick https://github.com/SimonSchick, Alejandro Fernandez Haro https://github.com/afharo, Silas Rech https://github.com/lenovouser, Anand Chowdhary https://github.com/AnandChowdhary, and Miro Yovchev https://github.com/myovchev.