xref: /aosp_15_r20/external/json-schema-validator/doc/yaml.md (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1*78c4dd6aSAndroid Build Coastguard WorkerOne of the features of this library is to validate the YAML file in addition to the JSON. In fact, the main use case for this library is to be part of the light-4j framework to validate the request/response at runtime against the OpenAPI specification file openapi.yaml. If you are not using light-4j, you need to load the YAML with https://github.com/FasterXML/jackson-dataformats-text first, and then everything is the same as JSON.
2*78c4dd6aSAndroid Build Coastguard Worker
3*78c4dd6aSAndroid Build Coastguard Worker### Usage
4*78c4dd6aSAndroid Build Coastguard Worker
5*78c4dd6aSAndroid Build Coastguard WorkerAdd the dependency
6*78c4dd6aSAndroid Build Coastguard Worker
7*78c4dd6aSAndroid Build Coastguard Worker```xml
8*78c4dd6aSAndroid Build Coastguard Worker<dependency>
9*78c4dd6aSAndroid Build Coastguard Worker    <groupId>com.fasterxml.jackson.dataformat</groupId>
10*78c4dd6aSAndroid Build Coastguard Worker    <artifactId>jackson-dataformat-yaml</artifactId>
11*78c4dd6aSAndroid Build Coastguard Worker    <version>2.10.1</version>
12*78c4dd6aSAndroid Build Coastguard Worker</dependency>
13*78c4dd6aSAndroid Build Coastguard Worker```
14*78c4dd6aSAndroid Build Coastguard Worker
15*78c4dd6aSAndroid Build Coastguard Workerand create object mapper using yaml factory i.e `ObjectMapper objMapper =new ObjectMapper(new YAMLFactory());`
16*78c4dd6aSAndroid Build Coastguard Worker
17*78c4dd6aSAndroid Build Coastguard Worker#### Example
18*78c4dd6aSAndroid Build Coastguard Worker```java
19*78c4dd6aSAndroid Build Coastguard WorkerJsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build(); /* Using draft-07. You can choose anyother draft.*/
20*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema schema = factory.getSchema(YamlOperations.class.getClassLoader().getResourceAsStream("your-schema.json"));
21*78c4dd6aSAndroid Build Coastguard Worker
22*78c4dd6aSAndroid Build Coastguard WorkerJsonNode jsonNode = mapper.readTree(YamlOperations.class.getClassLoader().getResourceAsStream("your-file.yaml"));
23*78c4dd6aSAndroid Build Coastguard WorkerSet<ValidationMessage> validateMsg = schema.validate(jsonNode);
24*78c4dd6aSAndroid Build Coastguard Worker```
25*78c4dd6aSAndroid Build Coastguard Worker
26