当前位置: 首页>>代码示例>>Java>>正文


Java JsonSchema.validate方法代码示例

本文整理汇总了Java中com.github.fge.jsonschema.main.JsonSchema.validate方法的典型用法代码示例。如果您正苦于以下问题:Java JsonSchema.validate方法的具体用法?Java JsonSchema.validate怎么用?Java JsonSchema.validate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.github.fge.jsonschema.main.JsonSchema的用法示例。


在下文中一共展示了JsonSchema.validate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: validate

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
@SneakyThrows
private boolean validate(Map<String, Object> data, String jsonSchema) {

    String stringData = objectMapper.writeValueAsString(data);
    log.debug("Validation data. map: {}, jsonData: {}", data, stringData);

    JsonNode schemaNode = JsonLoader.fromString(jsonSchema);

    JsonNode dataNode = JsonLoader.fromString(stringData);
    JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonSchema schema = factory.getJsonSchema(schemaNode);
    val report = schema.validate(dataNode);

    boolean isSuccess = report.isSuccess();
    if (!isSuccess) {
        log.info("Validation data report: {}", report.toString().replaceAll("\n", " | "));
    }
    return isSuccess;
}
 
开发者ID:xm-online,项目名称:xm-ms-entity,代码行数:20,代码来源:JsonDataValidator.java

示例2: checkSchema

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
private void checkSchema(File output)
{
    try
    {
        JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
        URL schemaPath = getClass().getResource("/swagger-schema.json");
        JsonSchema schema = factory.getJsonSchema(schemaPath.toString());
        JsonNode json = JsonLoader.fromFile(output);

        ProcessingReport report = schema.validate(json);
        System.out.println(report);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
 
开发者ID:mauriciotogneri,项目名称:stewie,代码行数:18,代码来源:Stewie.java

示例3: isValid

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
public static boolean isValid(String json, String schema) throws Exception {
    JsonNode schemaNode = JsonLoader.fromString(schema);       
    JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    JsonSchema jsonSchema = factory.getJsonSchema(schemaNode); 
    JsonNode jsonNode = JsonLoader.fromString(json);
    ProcessingReport report = jsonSchema.validate(jsonNode);
    logger.debug("report: {}", report);
    return report.isSuccess();
}
 
开发者ID:intuit,项目名称:karate,代码行数:10,代码来源:SchemaUtils.java

示例4: isValid

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
public boolean isValid(JsonNode jsonNode) {
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    log.verboseOutput("Checking against schema. Available at 'https://raw.githubusercontent.com/pivio/pivio-client/master/src/main/resources/pivio-schema.json'.", configuration.isVerbose());
    try {
        String schemaContent = readFromJARFile("/pivio-schema.json");
        JsonNode schemaNode = new ObjectMapper().readTree(schemaContent);
        final JsonSchema jsonSchema = factory.getJsonSchema(schemaNode);
        ProcessingReport processingReport = jsonSchema.validate(jsonNode);
        for (ProcessingMessage processingMessage : processingReport) {
            String pointer = processingMessage.asJson().get("instance").get("pointer").asText();
            log.output(pointer + " : " + processingMessage.getMessage());
        }
        return processingReport.isSuccess();
    } catch (ProcessingException | IOException e ) {
        log.output("Error processing Json. "+e.getMessage());
        return false;
    }
}
 
开发者ID:pivio,项目名称:pivio-client,代码行数:19,代码来源:SchemaValidator.java

示例5: validateSwaggerSpec

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
/**
 * Validates the input Swagger JsonNode against Swagger Specification schema.
 *
 * @throws OpenApiConversionException
 */
private static void validateSwaggerSpec(JsonNode swaggerJsonNode)
    throws OpenApiConversionException {
  ProcessingReport report = null;
  try {
    URL url = Resources.getResource(SCHEMA_RESOURCE_PATH);
    String swaggerSchema = Resources.toString(url, StandardCharsets.UTF_8);
    JsonNode schemaNode = Yaml.mapper().readTree(swaggerSchema);
    JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode);
    report = schema.validate(swaggerJsonNode);
  } catch (Exception ex) {
    throw new OpenApiConversionException("Unable to parse the content. " + ex.getMessage(), ex);
  }
  if (!report.isSuccess()) {
    String message = "";
    Iterator itr = report.iterator();
    if (itr.hasNext()) {
      message += ((ProcessingMessage) itr.next()).toString();
    }
    while(itr.hasNext())
    {
      message += "," + ((ProcessingMessage) itr.next()).toString();
    }
    throw new OpenApiConversionException(
        String.format("Invalid OpenAPI file. Please fix the schema errors:\n%s", message));
  }
}
 
开发者ID:googleapis,项目名称:api-compiler,代码行数:32,代码来源:MultiOpenApiParser.java

示例6: _validate

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
protected ProcessingReport _validate(JsonNode json) throws IOException {
    final JsonNode belSchema = JsonLoader.fromResource("/bel-json-graph.schema.json");
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    final JsonSchema schema;
    try {
        schema = factory.getJsonSchema(belSchema);
        return schema.validate(json, true);
    } catch (ProcessingException e) {
        throw new IOException(e);
    }
}
 
开发者ID:jsongraph,项目名称:jgf-app,代码行数:12,代码来源:GraphReaderImpl.java

示例7: jsonSchemaValidation

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
/**
 * Validates input node against given schema. Returns NULL if no errors
 * reported, else returns string representing violations.
 *
 * @param schema the json schema (see #loadSchema)
 * @param node the json node to validate
 * @return null if there are no errors, else string with all errors and
 * warnings
 * @throws ProcessingException
 */
public static String jsonSchemaValidation(JsonSchema schema, JsonNode node) throws ProcessingException, JsonProcessingException {
    ProcessingReport report = schema.validate(node);
    Iterator<ProcessingMessage> i = report.iterator();
    StringBuilder buff = new StringBuilder();
    while (!report.isSuccess() && i != null && i.hasNext()) {
        ProcessingMessage pm = i.next();

        // attempting to pretty print the json
        ObjectMapper mapper = new ObjectMapper();
        String prettyPrintJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pm.asJson());

        buff.append(prettyPrintJson).append("\n\n");
    }

    return report.isSuccess() ? null : buff.toString();
}
 
开发者ID:lightblue-platform,项目名称:lightblue-core,代码行数:27,代码来源:JsonUtils.java

示例8: validateExtensionTest

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
@Test
public void validateExtensionTest() throws Exception {

    ObjectMapper mapper = Json.mapper();

    String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json";
    JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema);


    Extension extension = new Extension.Builder()
        .extensionId("my-extension")
        .name("Name")
        .description("Description")
        .uses(OptionalInt.empty())
        .version("1.0.0")
        .addAction(new ExtensionAction.Builder()
            .id("action-1")
            .name("action-1-name")
            .description("Action 1 Description")
            .actionType("extension")
            .pattern(Action.Pattern.From)
            .descriptor(new ExtensionDescriptor.Builder()
                .entrypoint("direct:hello")
                .kind(ExtensionAction.Kind.ENDPOINT)
                .build())
            .build())
        .build();

    JsonNode node = mapper.valueToTree(extension);
    ProcessingReport report = schema.validate(node);

    assertFalse(report.toString(), report.iterator().hasNext());

}
 
开发者ID:syndesisio,项目名称:syndesis,代码行数:35,代码来源:ExtensionSchemaValidationTest.java

示例9: validate

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
public static boolean validate(String data, ObjectMapper objectMapper, BenderSchema benderSchema)
    throws ConfigurationException {
  ProcessingReport report;
  try {
    /*
     * Create object
     */
    JsonNode node = objectMapper.readTree(data);

    /*
     * Create JSON schema
     */
    JsonNode jsonSchema = benderSchema.getSchema();

    /*
     * Validate
     */
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    final JsonSchema schema = factory.getJsonSchema(jsonSchema);
    report = schema.validate(node);
  } catch (IOException | ProcessingException ioe) {
    throw new ConfigurationException("unable to validate config", ioe);
  }

  if (report.isSuccess()) {
    return true;
  } else {
    throw new ConfigurationException("invalid config file",
        report.iterator().next().asException());
  }
}
 
开发者ID:Nextdoor,项目名称:bender,代码行数:32,代码来源:BenderConfig.java

示例10: validateJson

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
public static void validateJson(JsonSchema jsonSchemaNode, JsonNode jsonNode)
throws ProcessingException 
{
    ProcessingReport report = jsonSchemaNode.validate(jsonNode);
    if (!report.isSuccess()) {
        for (ProcessingMessage processingMessage : report) {
            throw new ProcessingException(processingMessage);
        }
    }
}
 
开发者ID:deib-polimi,项目名称:diceH2020-space4clouds_shared,代码行数:11,代码来源:ValidationUtils.java

示例11: validateBodyIfPresent

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
private List<ValidationError> validateBodyIfPresent(CheckableRequest request, Operation operation) throws ProcessingException {
    Optional<BodyParameter> bodyParameter = getBodyParameter(operation);

    ImmutableList.Builder<ValidationError> bodyErrors = ImmutableList.builder();
    if (bodyParameter.isPresent()) {
        RefModel schemaRef = (RefModel) bodyParameter.get().getSchema();
        JsonSchema schema = schemas.get(schemaRef.getSimpleRef());

        JsonNode jsonNode = parseJson(request.getBody());

        ProcessingReport processingReport = schema.validate(jsonNode);
        for (ProcessingMessage processingMessage : processingReport) {
            if (processingMessage.getLogLevel().compareTo(LogLevel.INFO) > 0) {
                JsonNode detailsNode = processingMessage.asJson();
                String propertyPath = detailsNode.findValue("instance").findValue("pointer").textValue();
                bodyErrors.add(ValidationError.bodyError(propertyPath, processingMessage.getMessage()));
            }
        }
    }

    return bodyErrors.build();
}
 
开发者ID:energizedwork,项目名称:swagger-http-validator,代码行数:23,代码来源:SwaggerHttpValidator.java

示例12: validateSchema

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
/**
 * Checks if the specified json fulfills the specified schema.
 *
 * @param schemaName        The name of the schema.
 * @param contentObjectTree The json which is analysed.
 * @return TRUE if the json fulfills the specified schema, FALSE otherwise.
 * @throws ResourceException if an error occurred.
 */
private boolean validateSchema(String schemaName, JsonNode contentObjectTree) throws ResourceException {
    try {
        URITranslatorConfiguration translatorCfg = URITranslatorConfiguration.newBuilder().setNamespace(NAMESPACE).freeze();
        LoadingConfiguration cfg = LoadingConfiguration.newBuilder().setURITranslatorConfiguration(translatorCfg).freeze();
        JsonSchemaFactory factory = JsonSchemaFactory.newBuilder().setLoadingConfiguration(cfg).freeze();
        JsonSchema schema = factory.getJsonSchema(schemaName);
        ProcessingReport report = schema.validate(contentObjectTree);
        return report.isSuccess();
    } catch (ProcessingException e) {
        throw new ResourceException(e);
    }
}
 
开发者ID:qaware,项目名称:gradle-cloud-deployer,代码行数:21,代码来源:MarathonResourceFactory.java

示例13: testGetJsonValidation

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
@Test
public void testGetJsonValidation() throws Exception {
    MathTag tag = new MathTag(1, "a+b", WikiTextUtils.MathMarkUpType.LATEX);
    final String real = "[" + tag.toJson() + "]";
    final JsonNode jsonSchema = JsonLoader.fromResource("/formula_list_schema.json");
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    final JsonSchema schema = factory.getJsonSchema(jsonSchema);
    ProcessingReport report = schema.validate(JsonLoader.fromString(real));
    assertTrue(report.isSuccess());
}
 
开发者ID:ag-gipp,项目名称:mathosphere,代码行数:11,代码来源:MathTagTest.java

示例14: validateValueAgainstSchema

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
private void validateValueAgainstSchema(JsonNode node, JsonSchema schema)
        throws UnRAVLException {
    try {
        ProcessingReport report = schema.validate(node, true);
        boolean success = report.isSuccess();
        if (!success) {
            throw new UnRAVLAssertionException(report.toString());
        }
    } catch (ProcessingException e) {
        throw new UnRAVLException(e);
    }
}
 
开发者ID:sassoftware,项目名称:unravl,代码行数:13,代码来源:SchemaAssertion.java

示例15: runInvalidJsonTest

import com.github.fge.jsonschema.main.JsonSchema; //导入方法依赖的package包/类
/**
 * Verify the given document does not validate against the given schema.
 *
 * @param schemaResourceName
 * @param documentResourceName
 * @throws IOException
 * @throws ProcessingException
 */
public void runInvalidJsonTest(String schemaResourceName, String documentResourceName) throws IOException, ProcessingException {
    JsonSchema schema = JsonUtils.loadSchema(schemaResourceName);

    JsonNode instance = loadJsonNode(documentResourceName);

    ProcessingReport report = schema.validate(instance);
    Assert.assertFalse("Expected validation to fail!", report.isSuccess());
}
 
开发者ID:lightblue-platform,项目名称:lightblue-core,代码行数:17,代码来源:AbstractJsonSchemaTest.java


注:本文中的com.github.fge.jsonschema.main.JsonSchema.validate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。