New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@blubox/validation

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@blubox/validation

Body validation middleware for Blubox

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Validation Middleware

A middleware for request body validation using schema validation libraries such as Zod or Yup.

Installation

npm install @your-namespace/validation-middleware

Usage

const { Controller } = require('blubox');
const { Schema as ZodSchema, ZodError } = require('zod');
const { Schema as YupSchema, ValidationError } = require('yup');
const validation = require('@blubox/validation');

// Define your Zod or Yup schema
const zodSchema = ZodSchema.object({
  name: ZodSchema.string(),
  age: ZodSchema.number(),
});

// Define middleware options
const validationOptions = {
  onError: error => {
    if (error instanceof ValidationError) {
      return JSON.stringify(error.errors);
    } else if (error instanceof ZodError) {
      return JSON.stringify(error.errors);
    } else {
      return error.message;
    }
  },
  statusCode: 400,
};

// Create the validation middleware
const validationMiddleware = validation(zodSchema, validationOptions);

const controller = new Controller()
controller.use(validationMiddleware)

Options

onError: Custom error handling function. Default is a function that formats errors into JSON strings. statusCode: HTTP status code to set on validation failure. Default is 400.

License

MIT

Keywords

validation

FAQs

Package last updated on 10 Dec 2023

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