
revalidator 18个JSON Schema验证关键字详解从required到uniqueItems的完整清单【免费下载链接】revalidatorA cross-browser / node.js validator powered by JSON Schema项目地址: https://gitcode.com/gh_mirrors/re/revalidatorrevalidator 是一款轻量级的跨浏览器 / Node.js 数据校验库使用 JSON Schema 验证关键字来声明规则一行validate()调用即可判断数据是否合法。本文为你整理 revalidator 支持的全部 18 个验证关键字——从最基础的required到数组专用的uniqueItems——每个关键字都配一句大白话解释和最小示例适合刚接触 JSON Schema 验证的新手快速上手。1. 快速上手3 分钟跑通 revalidator 数据校验revalidator 的设计非常简洁你给一份数据object 一份规则schema它返回是否合法和错在哪里。安装方式二选一npm 安装npm install revalidator源码安装git clone https://gitcode.com/gh_mirrors/re/revalidator最小示例完整逻辑见lib/revalidator.jsvar revalidator require(revalidator); var schema { properties: { username: { type: string, required: true, minLength: 3 }, age: { type: integer, minimum: 0, maximum: 120 } } }; var result revalidator.validate({ username: ab }, schema); // { valid: false, errors: [{ attribute: minLength, property: username, ... }] }返回值只有两个字段非常好记 ✅字段含义validtrue/false数据是否通过校验errors错误数组每项包含attribute哪个关键字没通过、property哪个字段、message人话提示 在浏览器中直接引入revalidator.js后校验函数会挂载到window.validate上无需构建工具。2. 18 个 JSON Schema 验证关键字速查表先给一张总表方便检索后面分组逐个细讲 #关键字适用类型一句话说明1type通用指定值的类型string / number / integer / array / object / boolean / null / any / date也支持数组表示多选一2required通用为true时值不能是undefined缺了必填字段就报错3enum通用白名单值必须出现在给定数组里4format通用内置格式检查email、url、ip-address、date、color、regex等5pattern字符串值必须匹配给定的正则表达式6minLength字符串字符串长度下限7maxLength字符串字符串长度上限8allowEmpty字符串设为false时值不能是空字符串9minimum数字值 ≥ 期望值含边界10maximum数字值 ≤ 期望值含边界11exclusiveMinimum数字值 期望值不含边界12exclusiveMaximum数字值 期望值不含边界13divisibleBy数字值必须能被期望值整除支持小数14minItems数组数组最少包含的元素个数15maxItems数组数组最多包含的元素个数16uniqueItems数组为true时数组内所有元素不能重复17conform通用自定义校验函数完全自由18dependencies通用字段依赖该字段存在时其他指定字段也必须存在3. 基础四关键字type、required、enum、format这是写 schema 时出场率最高的四个先拿下它们就够应付 80% 的场景。type声明类型支持string、number、integer、array、object、boolean、null、any、date还可以写成数组表示其中一种即可{ type: [boolean, string] } // 布尔或字符串都合法required必填校验{ type: string, required: true } // 缺失undefined时报 is requiredenum白名单{ type: string, enum: [month, year] } // 只能是 month 或 yearformat内置格式校验revalidator 在lib/revalidator.js中内置了十余种格式的正则规则常用的有email/url/ip-address/ipv6date2024-01-01/time/date-timecolor#ff0000、rgb(...)/host-name/utc-millisec/regex{ type: string, format: email } // 不符合邮箱格式即报错4. 字符串校验关键字minLength、maxLength、pattern、allowEmptyminLength/maxLength长度区间{ type: string, minLength: 6, maxLength: 16 } // 密码长度 6~16 位pattern正则约束{ type: string, pattern: /^[a-z]$/ } // 只允许小写字母allowEmpty拒绝空串注意它和required的分工required管字段不能缺allowEmpty管值不能是空字符串。{ type: string, required: true, allowEmpty: false } // 字段必须存在且非空5. 数字校验关键字minimum 家族 divisibleBy数字类关键字是区间控制四件套边界是否包含一目了然关键字含义边界minimum最小值✅ 包含maximum最大值✅ 包含exclusiveMinimum严格大于❌ 不包含exclusiveMaximum严格小于❌ 不包含{ type: number, minimum: 0, exclusiveMaximum: 100 } // [0, 100) 区间divisibleBy整除约束还支持小数{ type: number, divisibleBy: 5 } // 5 的倍数 { type: number, divisibleBy: 0.5 } // 0.2 合法0.009 不合法6. 数组校验关键字minItems、maxItems、uniqueItemsitems逐项校验附赠关键字items里写一个小 schema数组中每个元素都要通过它错误信息会自动带上下标如0、1.1方便定位{ type: array, items: { type: number } } // [1, a, 3] 会在下标 1 报错minItems/maxItems个数控制{ type: array, minItems: 1, maxItems: 10 } // 至少 1 个、至多 10 个uniqueItems去重检查{ type: array, uniqueItems: true } // [a, b] 合法[a, a] 报错revalidator 用JSON.stringify做元素指纹所以对象元素也能判重test/validator-test.js中有对应测试用例。7. 进阶关键字conform 自定义函数与 dependencies 字段依赖conform一切皆函数内置关键字不够用时直接给一个回调函数value是实际值object是整个对象{ type: number, conform: function (v, object) { return v % 3 1 } // 只接受 3 的倍数加 1 }dependencies字段联动表达A 字段存在时B 字段必须存在。支持三种写法单个字段名、字段名数组、或直接给一个子 schema{ properties: { town: { required: true, dependencies: country }, // 有 town 必须有 country country: { type: string, maxLength: 3 } } }8. 实用技巧自定义错误提示与常用选项自定义错误消息每个 schema 节点都可以配messages按关键字分别写或全局message{ type: string, format: url, messages: { type: 不是字符串类型, format: 期望是合法的 url } }内置提示模板支持%{expected}/%{actual}占位符所有默认文案集中在lib/revalidator.js的validate.messages中可按需覆盖。常用选项通过第三个参数传入validate选项默认作用validateFormatstrue是否启用format格式校验validateFormatsStrictfalse未识别的 format 是否直接报错additionalPropertiestrue是否允许 schema 之外的多余字段设为false即白名单模式cast关闭自动把42转成数字42、true转成布尔最后看一个真实场景example/webservice.js里用 revalidator 给一个 REST 服务做请求体校验——URL 用pattern约束、challenge 用minLength保底校验不通过直接返回 400 和错误列表这就是它在服务端最典型的用法。结语revalidator 的魅力在于小而全18 个关键字覆盖了类型、必填、白名单、字符串、数字、数组和字段依赖的绝大多数场景返回值结构简单错误信息自带字段定位。建议从速查表挑出你项目需要的关键字组合配合messages定制中文提示就能以极低的成本给 API 和表单加上可靠的数据校验防线 更多关键字细节可以对照项目内的 README.md 文档与test/validator-test.js测试用例逐条验证行为。【免费下载链接】revalidatorA cross-browser / node.js validator powered by JSON Schema项目地址: https://gitcode.com/gh_mirrors/re/revalidator创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考