JSON schema validation with arbitrary keys

后端 未结 1 1280
伪装坚强ぢ
伪装坚强ぢ 2020-12-31 02:28

I am using validictory for validating the attached JSON data and schema. Working so far.

However the data dictionary can have arbitrary string keys (others than \'bp

相关标签:
1条回答
  • 2020-12-31 03:09

    It depends on exactly what you're trying to do.

    If you want the same specification, but for a range of properties, you can abstract out the definition:

    {
        "type": "object",
        "properties": {
            "bp": {"$ref": "#/definitions/categoryList"},
            "foo": {"$ref": "#/definitions/categoryList"},
            "bar": {"$ref": "#/definitions/categoryList"}
        },
        "definitions": {
            "categoryList": {...}
        }
    }
    

    If you want any properties to follow that schema, you can use additionalProperties:

    {
        "type": "object",
        "additionalProperties": {...}
    }
    

    Or a range of properties (matched by a pattern) - for instance, anything lower-case:

    {
        "type": "object",
        "patternProperties": {
            "^[a-z]+$": {...}
        }
    }
    

    If you want to limit the number of properties that can be defined, then you can use "maxProperties" (v4 of the standard only):

    {
        "type": "object",
        "additionalProperties": {...},
        "maxProperties": 1
    }
    

    P.S. - in v4 of the standard, "required" is an array. In fact, even in v3, "required" defaults to false, so your example doesn't need it at all

    0 讨论(0)
提交回复
热议问题