JSON 模式 + 相对 JSON-指针:如何验证 "confirm password" 字段
JSON schema + relative JSON-pointers: how to verify "confirm password" field
这是我的 JSON Schema:
{
"required": [
"username",
"password",
"confirmPassword"
],
"properties": {
"username": {
"minLength": 3,
"type": "string"
},
"password": {
"minLength": 6,
"type": "string"
},
"confirmPassword": {
"const": {
"$data": "1/password"
},
"type": "string"
}
},
"type": "object"
}
这是我的数据:
{
"username": "abc",
"password": "asdfasdf",
"confirmPassword": "asdfasdf"
}
您可以将它们复制粘贴到 this online validator 中,看看会发生什么。
confirmPassword
字段验证失败,错误消息:
Value "asdfasdf" does not match const.
我认为我的 relative JSON pointer 有问题,但我不知道正确的语法是什么。
AFAICT,1/password
的意思是“上升一级,然后检查 password
属性”,但事实并非如此。什么是正确的语法?
我使用的具体实现是 AJV,它说 确实 support relative-JSON-pointers.
原来唯一的问题是我忘记将 $data
option 设置为 true
。例如
const ajv = new Ajv({
allErrors: true,
$data: true,
});
这是我的 JSON Schema:
{
"required": [
"username",
"password",
"confirmPassword"
],
"properties": {
"username": {
"minLength": 3,
"type": "string"
},
"password": {
"minLength": 6,
"type": "string"
},
"confirmPassword": {
"const": {
"$data": "1/password"
},
"type": "string"
}
},
"type": "object"
}
这是我的数据:
{
"username": "abc",
"password": "asdfasdf",
"confirmPassword": "asdfasdf"
}
您可以将它们复制粘贴到 this online validator 中,看看会发生什么。
confirmPassword
字段验证失败,错误消息:
Value "asdfasdf" does not match const.
我认为我的 relative JSON pointer 有问题,但我不知道正确的语法是什么。
AFAICT,1/password
的意思是“上升一级,然后检查 password
属性”,但事实并非如此。什么是正确的语法?
我使用的具体实现是 AJV,它说 确实 support relative-JSON-pointers.
原来唯一的问题是我忘记将 $data
option 设置为 true
。例如
const ajv = new Ajv({
allErrors: true,
$data: true,
});