async-validator 引入配置 创建表单验证模块 src/utils/validate.ts 代码语言:javascript 复制 importtype{Context}from'koa';importSchema,{Value,Rules}from'async-validator';/** * 请求表单验证 * @param ctx * @param rules * @returns */constvalidate=async<TextendsValue>(ctx:Context,rules:Rules,):Promi...
Overview 0 Active Pull Requests 0 Active Issues 0 Merged Pull Requests 0 Open Pull Requests 0 Closed Issues 0 New Issues There hasn’t been any commit activity on carlitux/koa-async-validator in the last week. Want to help out? Fork this repository ©...
每天一个npm包:koa2-validation koa2-validation这个库用于Controller前的参数校验。 简单介绍它的使用: consthttp=require('http');constKoa=require('koa');constbodyParser=require('koa-bodyparser');constrouter=require('koa-router')();constvalidate=require('koa2-validation');// 1. import the koa2-...
constKoa=require('koa');constkoaContextValidator=require('koa-context-validator');constapp=newKoa();// 定义验证规则const schema = { body: { username: Joi.string().required(), },};// 使用Koa-Context-Validator中间件app.use(koaContextValidator(schema));// 处理请求app.use(async (ctx) => ...
}awaitnext() } } //... app.use(async (ctx,next) => { try { awaitnext(); } catch (error) {//ctx.status =500; ctx.body = error; } }); app.use(validator(/* schema */)/* , ... */);
npm install koa-api-validatorBasic Usageconst Koa = require('koa') const {ApiValidator} = require('koa-api-validator') const commands = require('./api') // your api files (async () => { const app = new Koa() const api = new ApiValidator(commands) app.use(await api.build()) }...
node-input-validator,用户少,不开源 推荐koa-parameter是基于参数验证框架parameter, 给koa框架做的适配 使用样例 附上一个简单的例子 constbodyparser=require('koa-bodyparser');//注意官方例子这里是require('..'), 请把..替换为koa-parameterconstparameter=require('koa-parameter');constKoa=require('koa')...
(koaValidator({customValidators:{isArray:function(value){return_.isArray(value);},isAsyncTest:function(testparam){returnnewPromise(function(resolve,reject){setTimeout(function(){if(testparam==='42'){returnresolve();}reject();},200);});}},customSanitizers:{toTestSanitize:function(){return"...
middlewares/common'importvalidateParamsfrom'../middlewares/validateParams'import{testSchema}from'../validator/test'constrouter=newRouter({prefix:'/standard-test'})router.allowedMethods()router.get('/name',commonMiddleware,validateParams('get',testSchema),async(ctx,next)=>{const{name}=ctx.request....
虽然这是一个内部系统,除了前端提交做校验外,业务方还是希望接口层要有一些必要的校验。如果全部用 If-else 写,想想这还是一个比较大的工作量的,不过还好,有 class-validator 这个库的存在,加上装饰器的写法,还是比较简洁。比如下面这个登录表单的校验示例:import { MinLength, Length } from "class-...