简体   繁体   English

YAML java 中的验证失败

[英]YAML Validation in java failing

I am getting following error when trying to validate json data using YAML schema.尝试使用 YAML 架构验证 json 数据时出现以下错误。 I am using networknt library as given here我正在使用此处给出的 networknt 库

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

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

at [Source: (StringReader);在 [来源:(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.Z93F725A07423FE1C889F448B33 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.Z93F725A07423FE1C889F448B33 D21F46Z: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) D21F46Z: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) 在 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输入.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 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

It seems to be an issue with you schema yaml.您的架构 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. Asdf 标准有一个示例,其中引用了架构值,而不是属性本身。 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编辑:您可能必须删除或更改第一种type: object -> - type: object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM