YAML Validation in java failing

1.7k Views Asked by At

I am getting following error when trying to validate json data using YAML schema. I am using networknt library as given here

com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException: mapping values are not allowed here in 'reader', line 1, column 58:

 ... -schema.org/draft-04/schema#type: objectproperties:  id:    type ... 
                                     ^

at [Source: (StringReader); line: 1, column: 58] at com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException.from(MarkedYAMLException.java:27) at com.fasterxml.jackson.dataformat.yaml.YAMLParser.nextToken(YAMLParser.java:359) at com.fasterxml.jackson.core.JsonParser.nextFieldName(JsonParser.java:861) at com.fasterxml.jackson.databind.deser.std.BaseNodeDeserializer.deserializeObject(JsonNodeDeserializer.java:250) at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:68) at com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:15) at com.fasterxml.jackson.databind.ObjectMapper._readTreeAndClose(ObjectMapper.java:4270) at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2720) at com.nike.nifi.validatejson.App.getJsonNodeFromStringContent(App.java:53) at com.nike.nifi.validatejson.App.validate(App.java:36) at com.nike.nifi.validatejson.App.main(App.java:25)

Code:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
        
    public boolean validate() {
        boolean isValid = false;
        try {
            String inputJsonString = getFileData("C:\\Projects\\data\\input.json");
            JsonNode node = getJsonNodeFromStringContent(inputJsonString);

            String schema = getFileData("C:\\Projects\\data\\yamlfrmjsonschema.yml");
            JsonNode schemaNode = getJsonNodeFromStringContent(schema);
            JsonSchema validator = getJsonSchemaFromJsonNodeAutomaticVersion(schemaNode);

            Set<ValidationMessage> errors = validator.validate(node);
            
            if (errors.isEmpty()) {
                isValid = !isValid;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return isValid;
    }
    
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

    private JsonNode getJsonNodeFromStringContent(String content) throws IOException {
        return mapper.readTree(content);
    }

    private JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) {       
        JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7)).objectMapper(mapper).build(); 
        return factory.getSchema(jsonNode);
    }

Input.json

    {
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "image":
        {
            "url": "images/0001.jpg",
            "width": 200,
            "height": 200
        },
    "thumbnail":
        {
            "url": "images/thumbnails/0001.jpg",
            "width": 32,
            "height": 32
        }
}

YAML

---
"$schema": http://json-schema.org/draft-04/schema#
type: object
properties:
  id:
    type: string
  type:
    type: string
  name:
    type: string
  image:
    type: object
    properties:
      url:
        type: string
      width:
        type: integer
      height:
        type: integer
    required:
    - url
    - width
    - height
  thumbnail:
    type: object
    properties:
      url:
        type: string
      width:
        type: integer
      height:
        type: integer
    required:
    - url
    - width
    - height
required:
- id
- type
- name
- image
- thumbnail
1

There are 1 best solutions below

4
Crude Code On

It seems to be an issue with you schema yaml.

At least it is my wild guess. It seems that your schema declaration is wrong. Asdf standard has an example in which the schema value is quoted, not the property itself. So try to change the first line of your schema to this:

---
$schema: "http://json-schema.org/draft-04/schema#"

Edit: And you may have to remove or change the first type: object -> - type: object