验证JSON有效载荷对招摇文件 - JSON-架构验证

问题描述 投票:0回答:1

我试图验证对包含服务协议招摇文件中的JSON有效载荷。我使用JSON-架构验证(2.1.7)库来实现这一目标,但目前它没有验证对指定的图案或最小/最大长度。

Java代码:

public void validateJsonData(final String jsonData) throws IOException, ProcessingException {

    ClassLoader classLoader = getClass().getClassLoader();
    File jsonSchemaFile = new File (classLoader.getResource("coachingStatusUpdate.json").getFile());

    String jsonSchema = new String(Files.readAllBytes(jsonSchemaFile.toPath()));

    final JsonNode dataNode = JsonLoader.fromString(jsonData);
    final JsonNode schemaNode = JsonLoader.fromString(jsonSchema);

    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonValidator jsonValidator = factory.getValidator();

    ProcessingReport report = jsonValidator.validate(schemaNode, dataNode);
    System.out.println(report);
    if (!report.toString().contains("success")) {
        throw new ProcessingException (
                report.toString());
    }
}

消息我送过

{
  "a": "b",
  "c": "d",
  "e": -1,
  "f": "2018-10-30",
  "g": "string" }

招摇的定义:

    {
  "swagger": "2.0",
  "info": {
    "version": "1.0.0",
    "title": "Test",
    "termsOfService": "http://www.test.co.za",
    "license": {
      "name": "Test"
    }
  },
  "host": "localhost:9001",
  "basePath": "/test/",
  "tags": [
    {
      "name": "controller",
      "description": "Submission"
    }
  ],
  "paths": {
    "/a": {
      "put": {
        "tags": [
          "controller"
        ],
        "summary": "a",
        "operationId": "aPUT",
        "consumes": [
          "application/json;charset=UTF-8"
        ],
        "produces": [
          "application/json;charset=UTF-8"
        ],
        "parameters": [
          {
            "in": "body",
            "name": "aRequest",
            "description": "aRequest",
            "required": true,
            "schema": {
              "$ref": "#/definitions/aRequest"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Received",
            "schema": {
              "$ref": "#/definitions/a"
            }
          },
          "400": {
            "description": "Bad Request"
          },
          "401": {
            "description": "Unauthorized"
          },
          "408": {
            "description": "Request Timeout"
          },
          "500": {
            "description": "Generic Error"
          },
          "502": {
            "description": "Bad Gateway"
          },
          "503": {
            "description": "Service Unavailable"
          }
        }
      }
    }
  },
  "definitions": {
    "aRequest": {
      "type": "object",
      "required": [
        "a",
        "b",
        "c",
        "d"
      ],
      "properties": {
        "a": {
          "type": "string",
          "description": "Status",
          "enum": [
            "a",
            "b",
            "c",
            "d",
            "e",
            "f",
            "g",
            "h"
          ]
        },
        "aReason": {
          "type": "string",
          "description": "Reason",
          "enum": [
            "a",
            "b",
            "c",
            "d",
            "e",
            "f",
            "g",
            "h",
            "i",
            "j",
            "k",
            "l",
            "m",
            "n"
          ]
        },
        "correlationID": {
          "type": "integer",
          "format": "int32",
          "description": "",
          "minimum": 1,
          "maximum": 9999999
        },
        "effectiveDate": {
          "type": "string",
          "format": "date",
          "description": ""
        },
        "f": {
          "type": "string",
          "description": "",
          "minLength": 1,
          "maxLength": 100
        }
      }
    },
    "ResponseEntity": {
      "type": "object",
      "properties": {
        "body": {
          "type": "object"
        },
        "statusCode": {
          "type": "string",
          "enum": [
            "100",
            "101",
            "102",
            "103",
            "200",
            "201",
            "202",
            "203",
            "204",
            "205",
            "206",
            "207",
            "208",
            "226",
            "300",
            "301",
            "302",
            "303",
            "304",
            "305",
            "307",
            "308",
            "400",
            "401",
            "402",
            "403",
            "404",
            "405",
            "406",
            "407",
            "408",
            "409",
            "410",
            "411",
            "412",
            "413",
            "414",
            "415",
            "416",
            "417",
            "418",
            "419",
            "420",
            "421",
            "422",
            "423",
            "424",
            "426",
            "428",
            "429",
            "431",
            "451",
            "500",
            "501",
            "502",
            "503",
            "504",
            "505",
            "506",
            "507",
            "508",
            "509",
            "510",
            "511"
          ]
        },
        "statusCodeValue": {
          "type": "integer",
          "format": "int32"
        }
      }
    }
  }
}

正如你可以看到我送通过的correlationID -1,应验证失败,但此刻的真实回归的成功:

com.github.fge.jsonschema.report.ListProcessingReport: success
java json swagger jsonschema json-schema-validator
1个回答
2
投票

json-schema-validator似乎只有纯JSON模式工作。 OpenAPI的规范使用JSON模式的扩展子集,所以模式格式是不同的。您需要能够对的OpenAPI /扬鞭的定义,如Atlassian的swagger-request-validator专门验证库。

© www.soinside.com 2019 - 2024. All rights reserved.