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

@hono/ajv-validator

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hono/ajv-validator

Validator middleware using Ajv

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

Ajv validator middleware for Hono

Validator middleware using Ajv for Hono applications. Define your schema with Ajv and validate incoming requests.

Usage

No Hook:

import { type JSONSchemaType } from 'ajv';
import { ajvValidator } from '@hono/ajv-validator';
 
const schema: JSONSchemaType<{ name: string; age: number }> = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number' },
  },
  required: ['name', 'age'],
  additionalProperties: false,
} as const;

const route = app.post('/user', ajvValidator('json', schema), (c) => {
  const user = c.req.valid('json');
  return c.json({ success: true, message: `${user.name} is ${user.age}` });
});

Hook:

import { type JSONSchemaType } from 'ajv';
import { ajvValidator } from '@hono/ajv-validator';

const schema: JSONSchemaType<{ name: string; age: number }> = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number' },
  },
  required: ['name', 'age'],
  additionalProperties: false,
};

app.post(
  '/user',
  ajvValidator('json', schema, (result, c) => {
    if (!result.success) {
      return c.text('Invalid!', 400);
    }
  })
  //...
);

Author

Illia Khvost https://github.com/ikhvost

License

MIT

FAQs

Package last updated on 15 Nov 2024

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