json-schema-validator - Json 模式验证器 - 验证未按预期工作
问题描述
我尝试设置验证模式,以便如果请求正文中的公共字段设置为“公共”,那么我验证字段标题、描述和标签是必需的,否则请求有效,并且此字段是不需要。我的 json 架构如下所示:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Update post",
"type": "object",
"properties": {
"public": {
"type": "string"
},
"title": {
"type": "string",
"minLength": 3
},
"description": {
"type": "string",
"minLength": 3
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"minItems": 1
},
"attachmentUrl": {
"type": "string"
}
},
"anyOf": [{
"not": {
"properties": {
"public": { "enum": ["public"] } },
"required": ["public"]
}
},
{
"required": ["title", "description", "tags", "attachmentUrl"]
}],
"additionalProperties": true
}
但是,这不起作用,因为我希望它起作用。如果我发送这样的请求正文:
{
"PK":"USER#auth0|223232006ede0b3f",
"SK":"POST#8eab1dd5-4179-4f89-afc3-29af9e2732c1",
"postId":"8eab1dd5-4179-4f89-afc3-29af9e2732c1",
"userId":"auth0|5f8e2de69b8822006ede0b3f",
"title":"Another post",
"createdAt":"2020-10-20T23:54:37.172Z",
"itemType":"POST",
"public":"private",
"publicPostId":"private#8eab1dd5-4179-4f89-afc3-29af9e2732c1",
"tags":[],
"attachmentUrl":"",
"description":"some "
}
然后请求失败,我收到消息:
消息:“无效的请求正文”
但是,如果我发送这样的请求:
PK: "USER#auth0|1232132e69b8822006ede0b3f"
SK: "POST#8eab1dd5-4179-4f89-afc3-29af9e2732c1"
attachmentUrl: ""
createdAt: "2020-10-20T23:54:37.172Z"
description: "some "
itemType: "POST"
postId: "8eab1dd5-4179-4f89-afc3-29af9e2732c1"
public: "public"
publicPostId: "private#8eab1dd5-4179-4f89-afc3-29af9e2732c1"
tags: ["adsad"]
title: "Another post"
userId: "auth0|5f8e2de69b8822006ede0b3f"
如果public设置为“public”,且其他字段不为空,则请求有效。如何修复此架构,使其仅在字段设置为“公共”并且字段描述、标签或标题之一为空时才有效。我玩过 json 模式验证器,我做了这个验证,它适用于我的用例:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Update post",
"type": "object",
"properties": {
"title": {
"type": "string",
},
"tags": {
"type": "array"
},
"attachmentUrl": {
"type": "string",
},
"public": {
"enum": ["public", "private"]
}
},
"if": {
"properties": { "public": { "const": "public" } }
},
"then": {
"properties": {
"public": {
"type": "string"
},
"title": {
"type": "string",
"minLength": 3
},
"description": {
"type": "string",
"minLength": 3
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"minItems": 1
},
"attachmentUrl": {
"type": "string"
}
}
}
}
但是,如果我尝试使用无服务器框架部署它,我会收到错误消息:
发生错误:ApiGatewayMethodPostsPostidVarPatchApplicationJsonModel - 指定的模型无效:验证结果:警告:[数组架构应定义属性“项目”。此架构可能不支持某些功能,包括 SDK 生成],错误:[指定的模型架构无效。不支持的关键字:["if","then"]](服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException;请求 ID:9571a7f1-83a9-45d9-bb33-ce9cd85f1d91;代理:null)。
我怎样才能使这项工作?
解决方案
推荐阅读
- flutter - 如何在颤动中删除特定路线?
- r - 无法在 R 版本 4.0.0 上安装软件包
- c# - 从当前位置/旋转开始相机的动画师?
- android - 在 Android 中运行 .sql 文件
- javascript - 跳过曲目时播放暂停按钮未重置
- java - 使用 firebase 当前 uid 而不是随机的保存我的文档文件
- list - 从现有 json 列表中提取唯一值以创建唯一的列表视图构建器
- python - “in”运算符没有按预期工作?(python)
- indexeddb - IndexedDB 不适用于 Microsoft Teams 的 Android 应用程序
- javascript - 在Angular 7 SSR中从服务器端渲染弹出内容