Socket
Socket
Sign inDemoInstall

@hapi/validate

Package Overview
Dependencies
2
Maintainers
7
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @hapi/validate

Object schema validation


Version published
Weekly downloads
1.6M
increased by9.38%
Maintainers
7
Install size
272 kB
Created
Weekly downloads
 

Package description

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

Readme

Source

@hapi/validate

A fork of the joi validation library for internal hapi core needs only. This fork is maintained to keep the hapi framework 100% free of external dependencies. It is not meant to be used outside of hapi core modules. Please use the official joi library.

FAQs

Last updated on 18 Oct 2020

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc