What is @hapi/validate?
The @hapi/validate package is a powerful schema description language and data validator for JavaScript. It allows you to create schemas for validation, enabling you to validate JavaScript objects to ensure they match a predefined structure, types, and constraints. This is particularly useful for validating API inputs, configuration objects, or any data received or processed by an application.
What are @hapi/validate's main functionalities?
Object Validation
This feature allows you to validate JavaScript objects against a predefined schema. The example demonstrates creating a schema for a user object that requires a name, an age between 10 and 99, and a valid email. The object is then validated against this schema.
{"const schema = Joi.object({ name: Joi.string().required(), age: Joi.number().integer().min(10).max(99), email: Joi.string().email().required() }); const result = schema.validate({ name: 'John Doe', age: 29, email: 'john.doe@example.com' }); if (result.error) { console.error(result.error.details); } else { console.log('Validation passed:', result.value); }"}
Array Validation
This feature enables the validation of arrays, including specifying the type of items the array should contain and any additional constraints on those items. The example shows how to validate an array containing specific string values and numbers within a range.
{"const schema = Joi.array().items(Joi.string().valid('apple', 'banana'), Joi.number().min(1).max(5)); const result = schema.validate(['apple', 3]); if (result.error) { console.error(result.error.details); } else { console.log('Validation passed:', result.value); }"}
Custom Validation
This feature allows for custom validation logic to be defined. The example demonstrates creating a custom validator that checks if a string matches a specific expected value, showcasing the flexibility of @hapi/validate for complex validation scenarios.
{"const customValidator = Joi.string().custom((value, helpers) => { if (value !== 'expectedValue') { return helpers.error('any.invalid'); } return value; }, 'Custom validation description'); const result = customValidator.validate('unexpectedValue'); if (result.error) { console.error(result.error.message); } else { console.log('Validation passed:', result.value); }"}
Other packages similar to @hapi/validate
joi
Joi is a popular schema description language and data validator for JavaScript, similar to @hapi/validate. It was originally part of the hapi ecosystem but now exists as a standalone package. Joi offers a rich set of features for validating data structures and has a very similar API to @hapi/validate, making it a direct alternative.
yup
Yup is another JavaScript schema builder for value parsing and validation. It adopts a slightly different approach compared to @hapi/validate, focusing on a more concise and expressive syntax to define schemas. Yup integrates well with form libraries, making it a popular choice for front-end validation scenarios.
ajv
Ajv is a fast JSON schema validator. Unlike @hapi/validate, which uses a custom schema description language, Ajv validates data against JSON Schema standards. This makes Ajv a good choice for projects that require standard JSON Schema validation, offering high performance and compatibility with many JSON Schema versions.