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: Wed, 23 Oct 2024 03:36:41 GMT
- Dependencies: none
Credits
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.