@honojs/validator
Advanced tools
Comparing version 0.0.5 to 0.1.0
@@ -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 }; |
@@ -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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17911
280
164