Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hapi/validate

Package Overview
Dependencies
Maintainers
7
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hapi/validate

Object schema validation

  • 1.1.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3M
decreased by-5.54%
Maintainers
7
Weekly downloads
 
Created

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

FAQs

Package last updated on 18 Oct 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc