持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第6天,点击查看活动详情
JSON Schema 内置format类型
- Json Schema String类型,format内置的校验类型。
内置格式类型 | 说明 | 格式 |
---|---|---|
'data-time' | 日期和时间 | 2018-11-13T20:20:39+00:00 |
time | 时间 | 20:20:39+00:00 |
date | 日期 | 2018-11-13 |
email | Internet 电子邮件地址 | - |
hostname | 主机 | - |
ipv4 | IPv4 | - |
ipv6 | IPv6 | - |
PS: 这里没一一列举所有的类型,具体的可以到 Understanding JSON Schema 了解。
Ajv format的应用和扩展
应用
从版本 7 开始,Ajv 不包括 JSON Schema 规范定义的格式 - 这些和其他几种格式由ajv-formats提供 (打开新窗口)插入。
const Ajv = require("ajv")
const addFormats = require("ajv-formats")
const ajv = new Ajv()
addFormats(ajv)
以下格式在ajv-formats中定义 (打开新窗口)使用“格式”关键字进行字符串验证:
- 日期:根据RFC3339的完整日期 (打开新窗口).
- time:带有可选时区的时间。
- 日期时间:来自同一来源的日期时间(时区是强制性的)。
- 持续时间:来自RFC3339的持续时间(打开新窗口)
- uri:完整的 URI。
- uri-reference : URI 引用,包括完整和相对 URI。
- uri-template : 符合RFC6570的 URI 模板(打开新窗口)
- url(已弃用):URL 记录 (打开新窗口).
- 电子邮件:电子邮件地址。
- hostname:根据RFC1034的主机名 (打开新窗口).
- ipv4:IP 地址 v4。
- ipv6:IP 地址 v6。
- regex:通过将字符串传递给 RegExp 构造函数来测试字符串是否是有效的正则表达式。
- uuid:根据RFC4122的通用唯一标识符 (打开新窗口).
- json-pointer:根据RFC6901的 JSON 指针 (打开新窗口).
- relative-json-pointer:根据此草案的相对 JSON 指针 (打开新窗口).
扩展
使用addFormat方法添加和替换任何格式:
ajv.addFormat("identifier", /^a-z$_[a-zA-Z$_0-9]*$/)
Ajv 还允许定义仅适用于数字的格式:
ajv.addFormat("byte", {
type: "number",
validate: (x) => x >= 0 && x <= 255 && x % 1 == 0,
})
自定义自己的格式导入:
const {default: Ajv, _} = require("ajv")
const ajv = new Ajv({code: {formats: _`require("./my_formats")`}})
addFormat添加格式要求
ajv.addFormat(name: string, format: Format): Ajv
type Format =
| true // to ignore this format (and pass validation)
| string // will be converted to RegExp
| RegExp
| (data: string) => boolean
| Object // format definition (see below and in types)
如果传递了Object属性,它还应有一下属性:
interface FormatDefinition { // actual type definition is more precise - see types.ts
validate: string | RegExp | (data: number | string) => boolean | Promise<boolean>
compare: (data1: string, data2: string): number // an optional function that accepts two strings
// and compares them according to the format meaning.
// This function is used with keywords `formatMaximum`/`formatMinimum`
// (defined in [ajv-keywords](https://github.com/ajv-validator/ajv-keywords) package).
// It should return `1` if the first value is bigger than the second value,
// `-1` if it is smaller and `0` if it is equal.
async?: true // if `validate` is an asynchronous function
type?: "string" | "number" // "string" is default. If data type is different, the validation will pass.
}
ajv-formats插件库源码
ajv-formats插件库,做内置类型的维护,和实现。通过addFormats方法,添加到ajv中,实现JSON Schema的校验。
定义的类型:
"date","time","date-time","iso-time","iso-date-time","duration","uri","uri-reference","uri-template","url","email","hostname","ipv4","ipv6","regex","uuid","json-pointer","json-pointer-uri-fragment","relative-json-291pxpointer","byte","int32","int64","float","double","password","binary",
类型实现:
在线运行
const Ajv = require("ajv")
const addFormats = require("ajv-formats")
const ajv = new Ajv()
addFormats(ajv)
const schema = {
type: "string",
format: 'email'
};
const validate = ajv.compile(schema)
const data = '123123@qq.com'
const valid = validate(data)
console.log(valid)
if (!valid) console.log(validate.errors)
结果
总结
- Ajv通过ajv-formats插件库维护format类型
- Ajv通过addFormat,添加自定义规则