1

we are using JsonSchema to document our Rest APIs and I need to be sure that every string, number, array has restrictions on their maximum size applied to them i.e.

  • all strings have a maxLength & pattern set
  • all integers/numbers have a maximum set
  • all arrays have a maxItems set

This will then allow us to run javax validation on the POJOs generated from the JsonSchema (we use jsonschema2pojo with JSR303 annotations).

I'd rather not manually eyeball every schema passed my way so wondering if there was any automated tool to check every element for these items? If not I may be writing one :-)

Many thanks

4

1 に答える 1

1

有効な JSON スキーマをdraft-04 meta-schemaに対して検証できるのと同じ方法で、独自のメタスキーマを構築できます。

サンプルを取得すると、一般的な有効な JSON スキーマに次の制約を追加できます。

{
    "oneOf" : [{
            "type" : "string",
            "required" : ["pattern", "maxLength"]
        }, {
            "type" : "array",
            "required" : ["maxItems"]
        }, {
            "type" : {
                "enum" : ["number", "integer"]
            },
            "required" : ["maximum"]
        }, {
            "type" : {
                "enum" : ["object", "boolean", "null"]
            }
        }

    ]
}

独自のメタ検証の後、Java クラスを安全に生成できます。

于 2015-12-09T16:04:52.140 に答える