json-schema-validator icon indicating copy to clipboard operation
json-schema-validator copied to clipboard

YAML validation fails

Open sajinkk opened this issue 5 years ago • 2 comments

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)

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 data

{ "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 data

---
"$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

I tried to remove $schema from above YAML, still getting similar error on next line

sajinkk avatar Dec 28 '20 14:12 sajinkk

Could you please try https instead of http for the $schema URL?

stevehu avatar Dec 30 '20 15:12 stevehu

I am getting following error when I update to https.

com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.MarkedYAMLException: while parsing a block mapping in 'reader', line 1, column 1: ---$schema: "https://json-schema ... ^ expected , but found '' in 'reader', line 1, column 55:


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

As I mentioned in original post if I remove the "$schema": http://json-schema.org/draft-04/schema#, then I am getting 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:    ... 
                          ^

sajinkk avatar Dec 31 '20 04:12 sajinkk

This is a Jackson issue not a json-schema-validator issue.

fdutton avatar Jun 11 '23 20:06 fdutton