Ссылка на локальный относительный файл в схеме JSON?

Я создаю JSON для веб-службы REST. Однако схема разрослась до более чем 1300 строк, и я хочу разделить ее на несколько файлов. Я провожу модульное тестирование своей схемы в сравнении с папкой образцов ответов JSON, используя json-schema-validator. версия 2.1.8.

Я знаю, что у вас есть типы импорта, определенные в вашем текущем файле, например { "$ref": "#/definitions/MyBool" }, однако, если бы я хотел переместить определение MyBool в другой файл, например Common.schema.json, как бы я тогда ссылался на него?

Я пробовал { "$ref": "Common.schema.json/definitions/MyBoolean" }, { "$ref": "./Common.schema.json/defintion/MyBoolean" } и { "$ref": "file://./Common.schema.json/definitions/MyBoolean" }, но ни один из них не работает.

Ответ на вопрос «Поддерживает ли проверка схемы JSON в утилитах common-js ссылки?" кажется, что он должен работать, но я не могу' Кажется, я правильно понимаю синтаксис.

Я загружаю схему с помощью:

JsonNode mySchema = JsonLoader.fromReader(   new InputStreamReader(  JsonSchemaTest.class.getResourceAsStream( "/json/schema/MySchema.schema.json" )  )   );

а затем подтвердить его с помощью:

JsonSchemaFactory.byDefault().getValidator().validate(   schema,   new InputStreamReader(  getClass().getResourceAsStream( "/json/sample/MyJsonSample.json" )  )   ).isSuccess();

FWIW MyBool выглядит так:

"MyBool": {
    "type": "object",
    "properties": {
        "value" :{
            "type": "string",
            "enum": [ "true", "false", "file not found" ]
        }
    },
    "required": ["value"],
    "additionalProperties": false
}

Когда я не пытаюсь извлечь MyBool, схема правильно проверяет все мои образцы JSON.


Исключение, которое я получаю:

com.github.fge.jsonschema.core.exceptions.ProcessingException: fatal: unable to dereference URI "file:/Common.schema.json/definitions/MyBool#"
    level: "fatal"
    uri: "file:/Common.schema.json/definitions/MyBool#"
    exceptionMessage: "\\Common.schema.json\\definitions\\MyBool (The system cannot find the path specified)"

    at com.github.fge.jsonschema.core.load.URIManager.getContent(URIManager.java:108)
    at com.github.fge.jsonschema.core.load.SchemaLoader$1.load(SchemaLoader.java:108)
    at com.github.fge.jsonschema.core.load.SchemaLoader$1.load(SchemaLoader.java:103)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
    at com.github.fge.jsonschema.core.load.SchemaLoader.get(SchemaLoader.java:165)
    at com.github.fge.jsonschema.core.load.RefResolver.rawProcess(RefResolver.java:113)
    at com.github.fge.jsonschema.core.load.RefResolver.rawProcess(RefResolver.java:50)
    at com.github.fge.jsonschema.core.processing.RawProcessor.process(RawProcessor.java:76)
    at com.github.fge.jsonschema.core.processing.RawProcessor.process(RawProcessor.java:40)
    at com.github.fge.jsonschema.core.processing.ProcessorChain$ProcessorMerger.process(ProcessorChain.java:188)
    at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:78)
    at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:127)
    at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:119)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
    at com.github.fge.jsonschema.core.processing.CachingProcessor.process(CachingProcessor.java:108)
    at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:106)
    at com.github.fge.jsonschema.processors.validation.ValidationChain.process(ValidationChain.java:55)
    at com.github.fge.jsonschema.core.processing.ProcessorMap$Mapper.process(ProcessorMap.java:165)
    at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:78)
    at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:127)
    at com.github.fge.jsonschema.core.processing.CachingProcessor$1.load(CachingProcessor.java:119)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3524)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2317)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2280)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2195)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3938)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4821)
    at com.github.fge.jsonschema.core.processing.CachingProcessor.process(CachingProcessor.java:108)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:83)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.processObject(ValidationProcessor.java:179)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:121)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.processArray(ValidationProcessor.java:149)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:119)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.processObject(ValidationProcessor.java:179)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:121)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:48)
    at com.github.fge.jsonschema.keyword.validator.draftv4.OneOfValidator.validate(OneOfValidator.java:67)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:100)
    at com.github.fge.jsonschema.processors.validation.ValidationProcessor.process(ValidationProcessor.java:48)
    at com.github.fge.jsonschema.core.processing.ProcessingResult.of(ProcessingResult.java:78)
    at com.github.fge.jsonschema.main.JsonValidator.validate(JsonValidator.java:103)
    at com.github.fge.jsonschema.main.JsonValidator.validate(JsonValidator.java:123)
    at com.initech.ws.json.BaseJsonSchemaTest.jsonFileShouldValidate(BaseJsonSchemaTest.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runners.Suite.runChild(Suite.java:127)
    at org.junit.runners.Suite.runChild(Suite.java:26)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

person Sled    schedule 01.04.2014    source источник
comment
Привет! Можете ли вы показать код, который вы используете для загрузки вашей схемы? Кроме того, какую версию вы используете?   -  person fge    schedule 03.04.2014
comment
@fge Я обновил вопрос, указав эти детали, но у меня еще не было возможности попробовать ваше предложение.   -  person Sled    schedule 03.04.2014
comment
Означают ли относительные пути, что вы можете использовать такие вещи, как ..?   -  person CMCDragonkai    schedule 19.03.2019
comment
@CMCDragonkai да   -  person Sled    schedule 19.03.2019


Ответы (3)


Что вам нужно, так это возможность ссылаться на ваши файлы, используя абсолютный URI.

Предположим, вы используете стабильную версию (например, 2.0.2), загрузите свою схему с помощью этот метод.

Учитывая, где находится ваш файл, используйте:

final JsonSchema schema 
    = factory.getJsonSchema("resource:/json/schema/MySchema.schema.json");

Это означает, что разрешение URI в этой схеме будет производиться относительно этого (загрузочного) URI; поэтому, ссылаясь на свой MyBoolean.json, вы будете делать:

{
    "$ref": "MyBoolean.json#/pointer/into/file"
}

Если он расположен, например, по адресу /json/schema/subschemas, вы напишете:

{
    "$ref": "subschemas/MyBoolean.json#/pointer/into/file"
}

Родители тоже работают и т.д.


Обратите внимание, что, как указано в README, 2.1.x является версией для разработчиков! В настоящее время я переделываю API...

person fge    schedule 03.04.2014

Следующее решение сработало для меня. Путь относится к месту, где я запускаю цели mvn.

"items": {
                "type": "object",
                "$ref": "file:src/xyz/abc/lmn/DeviceRecord.json"
         }

Здесь file: — это путь, по которому вы выполняете цель maven.

person apurbojha    schedule 07.12.2017
comment
но тогда, когда вы собираетесь развернуть свою схему на веб-сервере, не вызовет ли протокол file: проблем? - person Sled; 07.12.2017

Старый вопрос, но я смог заставить это работать, выполнив

given().when().get("...")
                .then()
                .statusCode(200)
                .body(matchesJsonSchemaInClasspath("schemas/the-array-schema.json"));

Затем в the-array-schema.json (в каталоге /resources/schemas) у меня есть

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "$ref": "the-object-schema.json"
  }
}

где the-object-schema.json также находится в каталоге /resources/schemas.

person Michael McKenna    schedule 10.12.2020