0

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
Sajin Surendran
  • 248
  • 6
  • 17

1 Answers1

0

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

Crude Code
  • 137
  • 1
  • 9
  • Still there is error com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException: while parsing a block mapping in 'reader', line 1, column 1: ---$schema: "http://json-schema. ... ^ expected , but found '' in 'reader', line 1, column 54: ... son-schema.org/draft-04/schema#"type: objectproperties: id: ... – Sajin Surendran Dec 28 '20 at 08:57
  • when changing yaml schema - type: object com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException: sequence entries are not allowed here in 'reader', line 1, column 54: ... son-schema.org/draft-04/schema#"- type: objectproperties: id: ... – Sajin Surendran Dec 28 '20 at 09:08
  • Then you may need to remove the `$schema` line entirely. As it seems to be an issue with this line. Or you have encountered a bug in the networknt implementation. They use the schema definition in the json schema examples, but are missing an explicit example for the yaml schema usecase. You may open an issue there and ask to improve the documentation and clarify – Crude Code Dec 28 '20 at 09:16
  • I tried removing schema from yaml to get following error com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException: mapping values are not allowed here in 'reader', line 1, column 26: ---type: objectproperties: id: type: string type: ... where the error is now at objectproperties: id – Sajin Surendran Dec 28 '20 at 14:25