как проверить, что $ refs в json схеме разрешены правильно - PullRequest
0 голосов
/ 17 апреля 2020

Я пытаюсь написать набор тестов, который проверяет всю мою схему и проверяет, что ссылка $ref в каждом файле JSON разрешена правильно. Например: рассмотрим, является ли это действительной ссылкой в ​​JSON

"title": "Response",
"description": "Schema descriptions of common properties of a response",
"type": [ "object" ],
"properties": {
  "_meta": {
     "$ref": "../Meta.json"
   },
  "operations": {
     "type": "string"

  },
  "bs":{
    "$ref": "file/bs.json"
  }  

}

. Я бы хотел проверить, правильны ли ссылки ../Meta.json и file/bs.json и являются ли ссылки неправильными (например, .file/bills.json) тогда тест должен вызвать ErrorError.

def test_schema_ref(self): """ Test whether or not the ref is valid """ schema_files = self._get_schema_files() try: for schema_file in schema_files: base_uri = 'file://'+os.path.join(self._schema_dir, os.path.dirname(schema_file)) referer = os.path.basename(schema_file) fpath = os.path.join(self._schema_dir, schema_file) resolver = RefResolver(base_uri, referer) if os.path.isfile(fpath): with open(fpath, "r") as schema_fd: schema = json.load(schema_fd) node = TestRefResolution._do_resolve(schema)

def _do_resolve(node):
  if isinstance(node, dict):
    for k, v in node.items():
       if k == 'properties' and isinstance(node[k], dict):
          for key, value in node[k].items():
            print ("key", key, value)

Как мне проверить, разрешаются ли ссылки, и есть ли способ рекурсивно проверить ссылки в properties и разрешить их?

...