如何限制 JSON 模式中对象键的最大长度

How to constraint maximum length of keys of an object in JSON schema

我想限制 属性 类型为 object 的键的最大长度。它的所有值都是在运行时生成的,我想通过在进一步处理之前验证文档来尽快发现不当行为。

你可以这样做:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "Object with 10 char max properties",
    "type": "object",
    "additionalProperties": false,
    "minProperties": 1,
    "patternProperties": 
    {
        "^[a-z]{0,10}$": 
        { 
            "description": "Some description", 
            "type": "string"
        }
    }
}