首页 > 解决方案 > JustinRainbow/JsonSchema 中的外部引用

问题描述

我一直在挖掘使用外部引用的方法。我发现所有引用都应该在$ref.

我尝试了不同的方法来实现这一点,最后没有任何例外:

$schemaStorage = new SchemaStorage();

$schemaStorage->addSchema('file://foo.json', json_decode(file_get_contents(realpath(__DIR__."/foo.json")));
$schemaStorage->addSchema('file://bar.json', json_decode(file_get_contents(realpath(__DIR__."/bar.json")));

$validator = return new Validator(new Factory($schemaStorage));

$validator->validate($jsonData, json_decode(file_get_contents(realpath(__DIR__."/main.json"))));

然后在我的架构中,我尝试在定义的架构 ID 上引用:

{
  "title": "foobar schema",
  "type": "object",
  "properties": {
    "foo": {
       "$ref": "file://foo.json"
    },
    "bar": {
       "$ref": "file://bar.json"
    }
  },
  "required": ["foo", "bar"]
}

但是,即使正确预加载了所有引用,也不会验证所有引用。只有主模式用于验证。也许有人知道正确的方法是什么?

标签: phpjsonschema

解决方案


推荐阅读