首页 > 解决方案 > JSON Schema 验证非空正文

问题描述

我正在使用 JSON Schema 验证 API Gateway 请求,我还没有意识到它允许空请求,所以我想知道是否有一种方法可以通过模式验证我实际收到的东西。

这是我拥有的架构示例:

{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "PutPetRequest",
"description": "Accepts a request to create or update a Pet.",
"type": "object",
"required": ["pet"],
"additionalProperties": false,
"properties": {
    "pet": {
        "description": "Pet object type",
        "type": "object",
        "required": [
            "id"
        ],
        "additionalProperties": false,
        "properties": {
            "id": {
                "type": "string",
                "format": "uuid"
            }
        }
    }
}

}

一个空对象{}将无法通过验证,但是 (空的,什么都没有)通过,我已经尝试过minPropertiesminItems但我看到了相同的行为,这不可能吗?

我正在使用https://www.jsonschemavalidator.net/进行测试

标签: jsonschemaaws-api-gatewayjsonschemaserverless

解决方案


我遇到了类似的情况,将 minLength 设置为 1 至少需要 1 个字符。我知道这不是一个完美的解决方案,但这可以用作解决方法。

scope: {
        title: 'my property',
        description: 'description about my property',
        type: JsonSchemaType.STRING,
        minLength: 1,
      },

推荐阅读