Node.js Express中使用joi进行表单验证

使用joi npm包可以很方便的在Node.js Express项目中实现表单验证,以下例子可供参考:

创建登录表单验证:

javascript 复制代码
const joi = require('joi')

const title = joi.string().min(1).max(45).required()//最少1位,最多18位,必选
const text = joi.string().max(999).required()//密码为1-18位任意字符
const category = joi.string().min(2).max(2).required()//长度为2位任意字符
const date = joi.string().min(19).max(19).required()//密码为1-18位任意字符

//创建文章表单验证
exports.createArticle_schema = {
    body:{
        title,
        text,
        category,
        date,
    },
}

使用表单验证:

javascript 复制代码
const expressJoi = require('@escook/express-joi')
const {createArticle_schema} = require('../schema/article')


router.post('/article', expressJoi(createArticle_schema), article_handler.createArticle)
相关推荐
烟雨空灵2 天前
25 天做了个独立开发者作品橱窗,收录 340+ 件作品,代码全开源
前端·node.js
用户216076340832 天前
1200 个任务里,97 个还开着,0 个我能做
node.js
不要试图纠正别人2 天前
1055 个文件,共享 0 字节:pnpm 硬链接的静默降级
前端·node.js
吴名氏.3 天前
从零手写一个 AI Agent 框架(TypeScript 完整实现):Function Calling 循环、流式输出与错误恢复
人工智能·typescript·node.js·大模型·ai agent·functioncalling·llm 应用开发
关键词安全4 天前
PHP / Node.js / Go / Rust 全部中招(CVE-2026-109618、109619、111497、111943)完整排查与修复清单
golang·rust·node.js
zhensherlock5 天前
Protocol Launcher 系列:CodeHub 移动端 GitHub 协作利器
前端·javascript·typescript·node.js·自动化·github·js
jaysee-sjc5 天前
【黑马点评】Day02:优惠券秒杀与分布式锁
java·分布式·nginx·spring·node.js·tomcat·maven
ZhengEnCi6 天前
Express 企业级项目结构怎么组织:五层骨架、单向依赖和一份能跑的边界检查脚本
javascript·node.js·express
Dumbness6 天前
搜 platform 却给出 Form 表单:离线文档检索把短关键词当成了子串
javascript·node.js
用户216076340837 天前
一个 2 MB 的 .zst 文件,解压出来只有十几 KB
node.js