簡體   English   中英

json 模式驗證使用帶有 tv4 的 patternProprties

[英]json schema validation using patternProprties with tv4

我有一個 json 像:

{"post": {"someKey": {"anotherKey":"anotherValue"}}}

並且第一個鍵是有效的 http 方法,並且可以是在運行時發布、獲取等所有有效的 http 方法。

這是我的架構

var schema = {
    "type": "object",
    "patternProperties": {
        "^[a-z]+$": {
            'properties': {
              "type": "object",
              'properties': {
                'someKey':{
                    'type': 'object',
                    'properties': {
                      'anotherKey': {'type': 'string'},
                    }
                }
            }
        }
      }
   }
}

var valid = { "post": {"mkey":"myvalue"}}; //This is getting passed but I know that is wrong
var invalid = { "1": {"mkey":"myvalue"}}; //This is passed but actually it should fail

console.log(tv4.validateMultiple(invalid, schema));

有人可以幫忙嗎?

我想通了:

{
    'type': 'object',
    'patternProperties': {
        '^(POST|GET|DELETE|HEAD|PATCH|HEAD|PUT)$': {
            'additionalProperties': false,
            'type': 'object',
            'required': ['someKey'],
            'properties': {
                'someKey': {
                    'type': 'string'
                }
            }
        }
    }
}

在這里,需要注意的事情: 1.'additionalProperties': false 對 patternProperties 很重要 2. 根據 JSON Schema 規范,你不能有不區分大小寫的匹配,因此所有都是大寫的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM