xref: /aosp_15_r20/external/json-schema-validator/doc/yaml.md (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1One 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
3### Usage
4
5Add the dependency
6
7```xml
8<dependency>
9    <groupId>com.fasterxml.jackson.dataformat</groupId>
10    <artifactId>jackson-dataformat-yaml</artifactId>
11    <version>2.10.1</version>
12</dependency>
13```
14
15and create object mapper using yaml factory i.e `ObjectMapper objMapper =new ObjectMapper(new YAMLFactory());`
16
17#### Example
18```java
19JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build(); /* Using draft-07. You can choose anyother draft.*/
20JsonSchema schema = factory.getSchema(YamlOperations.class.getClassLoader().getResourceAsStream("your-schema.json"));
21
22JsonNode jsonNode = mapper.readTree(YamlOperations.class.getClassLoader().getResourceAsStream("your-file.yaml"));
23Set<ValidationMessage> validateMsg = schema.validate(jsonNode);
24```
25
26