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

@honojs/validator

Package Overview
Dependencies
Maintainers
3
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@honojs/validator - npm Package Compare versions

Comparing version 0.0.5 to 0.1.0

5

dist/index.d.ts

@@ -7,2 +7,3 @@ import type { Handler, Context } from 'hono';

declare type Rules = Rule | Rule[];
declare type Done = (resultSet: ResultSet, context: Context) => Response | undefined;
declare type RuleSet = {

@@ -13,2 +14,3 @@ body?: Record<string, Rules>;

query?: Record<string, Rules>;
done?: Done;
};

@@ -31,3 +33,2 @@ declare type ResultSet = {

declare const validatorMiddleware: (validatorFunction: (validator: Validator, message: (value: string) => Message) => RuleSet) => Handler;
declare const validationResult: (c: Context) => ResultSet;
export { validatorMiddleware as validation, validationResult };
export { validatorMiddleware as validation };

15

dist/index.js

@@ -6,3 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.validationResult = exports.validation = void 0;
exports.validation = void 0;
const jsonpath_plus_1 = require("jsonpath-plus");

@@ -117,13 +117,14 @@ const validator_1 = __importDefault(require("./validator"));

}
c.set('validationResult', result);
await next();
if (v.done) {
const res = v.done(result, c);
if (res) {
return res;
}
}
if (result.hasError) {
return c.text(result.messages.join('\n'), 400);
}
await next();
};
};
exports.validation = validatorMiddleware;
const validationResult = (c) => {
return c.get('validationResult');
};
exports.validationResult = validationResult;
{
"name": "@honojs/validator",
"version": "0.0.5",
"version": "0.1.0",
"description": "Validator Middleware for Hono",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -13,2 +13,3 @@ # Validator Middleware for Hono

```
npm install hono
npm install @honojs/validator

@@ -20,3 +21,8 @@ ```

```ts
import { serve } from 'https://deno.land/std/http/server.ts'
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { validation } from 'https://deno.land/x/hono_validator/mod.ts'
//...
serve(app.fetch)
```

@@ -132,23 +138,21 @@

You can handle the errors more flexibly using `validatorResult` method.
You can handle the errors more flexibly using `done` method.
```ts
import { validation, validationResult } from '@honojs/validator'
//...
app.get('/search', (c) => {
const result = validationResult(c)
if (result.hasError) {
return c.json(
{
messages: result.messages,
},
404
)
app.get(
'/custom-error',
validation((v) => ({
body: {
userId: v.required,
},
done: (result, c) => {
if (result.hasError) {
return c.json({ messages: result.messages }, 403)
}
},
})),
(c) => {
return c.json({ messages: ['SUCCESS'] })
}
return c.json({
messages: ['success!'],
})
})
)
```

@@ -155,0 +159,0 @@

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