Json模式验证器-验证无法按预期进行

时间:2020-10-21 00:11:12

标签: json-schema-validator

我试图设置验证架构,以便如果请求正文中的public字段设置为“ public”,那么我验证是否需要字段标题,描述和标签,否则请求有效,并且此字段不是必需的。我的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”,并且其他字段不为空,则该请求有效。如何修复此架构,以便仅在将字段设置为“ 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-无效 指定的模型:验证结果:警告:[数组架构应 定义了属性“ items”。某些功能,包括SDK 生成,此架构可能不支持],错误:[无效 指定的模型架构。不支持的关键字:[“ if”,“ then”]] (服务:AmazonApiGateway;状态代码:400;错误代码: BadRequestException;请求ID:9571a7f1-83a9-45d9-bb33-ce9cd85f1d91; 代理:空)。

我该如何进行这项工作?

0 个答案:

没有答案