You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSON Schema 2020-12的prefixItems仅验证数组首元素问题及原理咨询

问题:prefixItems 如何评估JSON实例?

我使用以下JSON Schema验证JSON数据时,发现当appData数组包含多个元素时,仅第一个索引的元素会被验证,其余元素未被验证。想知道prefixItems是如何评估JSON实例的?

用到的JSON Schema

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
        "appName": {
            "type": "string"
        },
        "appData": {
            "type": "array",
            "prefixItems": [
                {
                    "type": "object",
                    "properties": {
                        "appReference": {
                            "type": "string"
                        },
                        "tableName": {
                            "type": "string"
                        },
                        "beginDate": {
                            "type": "string",
                            "pattern": "^[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}$",
                            "description": "Use regex to validate this string as a date"
                        },
                        "isTrueBeginDate": {
                            "type": "string"
                        },
                        "paid": {
                            "type": "string"
                        },
                        "amount": {
                            "type": "null"
                        }
                    },
                    "required": [
                        "appReference",
                        "tableName",
                        "beginDate",
                        "paid",
                        "amount"
                    ]
                }
            ]
        }
    },
    "required": [
        "appName",
        "appData"
    ]
}

使用的Maven依赖

<dependency>
    <groupId>com.networknt</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>1.4.0</version>
</dependency>

解答

prefixItems的核心行为

在JSON Schema Draft 2020-12规范中,prefixItems的作用是为数组的前N个元素分别指定独立的验证规则,其中N等于prefixItems数组的长度。

你当前的Schema中prefixItems只包含1个规则对象,所以只会验证数组的第1个元素(索引0),数组中剩下的元素默认不受任何验证约束,自然不会被校验。

如何验证所有数组元素

根据需求不同,有两种常见处理方式:

  1. 所有元素遵循同一规则:使用items关键字替代prefixItems,items会对数组中的每一个元素应用相同的验证规则。
    修改后的appData示例:

    "appData": {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "appReference": { "type": "string" },
                "tableName": { "type": "string" },
                "beginDate": {
                    "type": "string",
                    "pattern": "^[0-9]{1,2}\\/[0-9]{1,2}\\/[0-9]{4}$",
                    "description": "Use regex to validate this string as a date"
                },
                "isTrueBeginDate": { "type": "string" },
                "paid": { "type": "string" },
                "amount": { "type": "null" }
            },
            "required": [
                "appReference",
                "tableName",
                "beginDate",
                "paid",
                "amount"
            ]
        }
    }
    
  2. 前N个元素用独立规则,后续元素用统一规则:同时使用prefixItems和items——prefixItems指定前N个元素的专属规则,items指定剩余所有元素的通用规则。
    示例(验证第一个元素,且后续元素也遵循同一规则):

    "appData": {
        "type": "array",
        "prefixItems": [
            { /* 第一个元素的专属规则,可与后续规则不同 */ }
        ],
        "items": { /* 后续所有元素的通用规则 */ }
    }
    

你使用的com.networknt/json-schema-validator 1.4.0完全支持JSON Schema Draft 2020-12规范,当前的验证行为是符合标准的。


内容的提问来源于stack exchange,提问作者javaseeker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 19:06:20