com.github.fge.jsonschema.main.JsonSchema.validate()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(11.8k)|赞(0)|评价(0)|浏览(125)

本文整理了Java中com.github.fge.jsonschema.main.JsonSchema.validate()方法的一些代码示例,展示了JsonSchema.validate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonSchema.validate()方法的具体详情如下:
包路径:com.github.fge.jsonschema.main.JsonSchema
类名称:JsonSchema
方法名:validate

JsonSchema.validate介绍

[英]Validate an instance and return a processing report

This calls #validate(JsonNode,boolean) with false as a second argument.
[中]验证实例并返回处理报告
这将使用false作为第二个参数调用#validate(JsonNode,boolean)。

代码示例

代码示例来源:origin: Vedenin/useful-java-links

public static void main(final String... args)
      throws IOException, ProcessingException {
    final JsonNode fstabSchema = Utils.loadResource("/fstab.json");
    final JsonNode good = Utils.loadResource("/fstab-good.json");
    final JsonNode bad = Utils.loadResource("/fstab-bad.json");
    final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");

    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

    final JsonSchema schema = factory.getJsonSchema(fstabSchema);

    ProcessingReport report;

    report = schema.validate(good);
    System.out.println(report);

    report = schema.validate(bad);
    System.out.println(report);

    report = schema.validate(bad2);
    System.out.println(report);
  }
}

代码示例来源:origin: rest-assured/rest-assured

@Override
protected boolean matchesSafely(String content) {
  try {
    JsonNode contentAsJsonNode = JsonLoader.fromString(content);
    JsonSchemaFactory jsonSchemaFactory = instanceSettings.jsonSchemaFactory();
    Schema loadedSchema = loadSchema(schema, instanceSettings);
    final JsonSchema jsonSchema;
    if (loadedSchema.hasType(JsonNode.class)) {
      jsonSchema = jsonSchemaFactory.getJsonSchema(JsonNode.class.cast(loadedSchema.schema));
    } else if (loadedSchema.hasType(String.class)) {
      jsonSchema = jsonSchemaFactory.getJsonSchema(String.class.cast(loadedSchema.schema));
    } else {
      throw new RuntimeException("Internal error when loading schema from factory. Type was " + loadedSchema.schema.getClass().getName());
    }
    if (instanceSettings.shouldUseCheckedValidation()) {
      report = jsonSchema.validate(contentAsJsonNode);
    } else {
      report = jsonSchema.validateUnchecked(contentAsJsonNode);
    }
    return report.isSuccess();
  } catch (Exception e) {
    throw new JsonSchemaValidationException(e);
  }
}

代码示例来源:origin: loklak/loklak_server

public ProcessingReport validate(String jsonText) throws IOException {

    ProcessingReport report;
    JsonNode toValidate = JsonLoader.fromString(jsonText);
    try {
      report = this.schema.validate(toValidate);
    } catch (ProcessingException e) {
      throw new IOException("Error validating json text : " +  e.getMessage());
    }
    return report;
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

public static void main(final String... args)
    throws IOException, ProcessingException
  {
    final JsonNode good = Utils.loadResource("/fstab-good.json");
    final JsonNode bad = Utils.loadResource("/fstab-bad.json");
    final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");

    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

    final JsonSchema schema = factory.getJsonSchema(SCHEMA_URI);

    ProcessingReport report;

    report = schema.validate(good);
    System.out.println(report);

    report = schema.validate(bad);
    System.out.println(report);

    report = schema.validate(bad2);
    System.out.println(report);
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

public static void main(final String... args)
    throws IOException, ProcessingException
  {
    final JsonNode fstabSchema = Utils.loadResource("/fstab.json");
    final JsonNode good = Utils.loadResource("/fstab-good.json");
    final JsonNode bad = Utils.loadResource("/fstab-bad.json");
    final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");

    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

    final JsonSchema schema = factory.getJsonSchema(fstabSchema);

    ProcessingReport report;

    report = schema.validate(good);
    System.out.println(report);

    report = schema.validate(bad);
    System.out.println(report);

    report = schema.validate(bad2);
    System.out.println(report);
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

@Override
  public RetCode validateInstance(final JsonSchema schema,
    final String fileName, final JsonNode node)
    throws IOException, ProcessingException
  {
    final ListProcessingReport report
      = (ListProcessingReport) schema.validate(node, true);
    final boolean success = report.isSuccess();
    System.out.println("--- BEGIN " + fileName + "---");
    System.out.println("validation: " + (success ? "SUCCESS"
      : "FAILURE"));
    if (!success)
      System.out.println(JacksonUtils.prettyPrint(report
        .asJson()));
    System.out.println("--- END " + fileName + "---");
    return success ? ALL_OK : VALIDATION_FAILURE;
  }
},

代码示例来源:origin: java-json-tools/json-schema-validator

public static void main(final String... args)
    throws IOException, ProcessingException
  {
    final JsonNode fstabSchema = Utils.loadResource("/fstab-draftv3.json");
    final JsonNode good = Utils.loadResource("/fstab-good.json");
    final JsonNode bad = Utils.loadResource("/fstab-bad.json");
    final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");

    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();

    final JsonSchema schema = factory.getJsonSchema(fstabSchema);

    ProcessingReport report;

    report = schema.validate(good);
    System.out.println(report);

    report = schema.validate(bad);
    System.out.println(report);

    report = schema.validate(bad2);
    System.out.println(report);
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

public static void main(final String... args)
    throws IOException, ProcessingException
  {
    final JsonNode fstabSchema = Utils.loadResource("/fstab-inline.json");
    final JsonNode good = Utils.loadResource("/fstab-good.json");
    final JsonNode bad = Utils.loadResource("/fstab-bad.json");
    final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");

    final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
      .dereferencing(Dereferencing.INLINE).freeze();
    final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
      .setLoadingConfiguration(cfg).freeze();

    final JsonSchema schema = factory.getJsonSchema(fstabSchema);

    ProcessingReport report;

    report = schema.validate(good);
    System.out.println(report);

    report = schema.validate(bad);
    System.out.println(report);

    report = schema.validate(bad2);
    System.out.println(report);
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

public static void main(final String... args)
  throws IOException, ProcessingException
{
  final JsonNode good = Utils.loadResource("/fstab-good.json");
  final JsonNode bad = Utils.loadResource("/fstab-bad.json");
  final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
  final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
    .addScheme("foobar", CustomDownloader.getInstance()).freeze();
  final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
    .setLoadingConfiguration(cfg).freeze();
  final JsonSchema schema
    = factory.getJsonSchema("foobar:/fstab.json#");
  ProcessingReport report;
  report = schema.validate(good);
  System.out.println(report);
  report = schema.validate(bad);
  System.out.println(report);
  report = schema.validate(bad2);
  System.out.println(report);
}

代码示例来源:origin: java-json-tools/json-schema-validator

private static void doValidate(final Map<String, JsonNode> schemas,
    final int i)
    throws ProcessingException
  {
    String name;
    JsonNode value;
    ProcessingReport report;

    for (final Map.Entry<String, JsonNode> entry: schemas.entrySet()) {
      name = entry.getKey();
      value = entry.getValue();
      report = SCHEMA.validate(value);
      if (!report.isSuccess()) {
        System.err.println("ERROR: schema " + name + " did not "
          + "validate (iteration " + i + ')');
        System.exit(1);
      }
    }
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

public static void main(final String... args)
    throws IOException, ProcessingException
  {
    final JsonNode good = Utils.loadResource("/fstab-good.json");
    final JsonNode bad = Utils.loadResource("/fstab-bad.json");
    final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");

    final URITranslatorConfiguration translatorCfg
      = URITranslatorConfiguration.newBuilder()
      .setNamespace(NAMESPACE).freeze();
    final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
      .setURITranslatorConfiguration(translatorCfg).freeze();

    final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
      .setLoadingConfiguration(cfg).freeze();

    final JsonSchema schema = factory.getJsonSchema("fstab.json");

    ProcessingReport report;

    report = schema.validate(good);
    System.out.println(report);

    report = schema.validate(bad);
    System.out.println(report);

    report = schema.validate(bad2);
    System.out.println(report);
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

public static void main(final String... args)
    throws IOException, ProcessingException
  {
    final JsonNode good = Utils.loadResource("/fstab-good.json");
    final JsonNode bad = Utils.loadResource("/fstab-bad.json");
    final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");

    final URITranslatorConfiguration translatorCfg
      = URITranslatorConfiguration.newBuilder()
      .addSchemaRedirect(FROM, TO).freeze();
    final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
      .setURITranslatorConfiguration(translatorCfg).freeze();

    final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
      .setLoadingConfiguration(cfg).freeze();

    final JsonSchema schema = factory.getJsonSchema(FROM);

    ProcessingReport report;

    report = schema.validate(good);
    System.out.println(report);

    report = schema.validate(bad);
    System.out.println(report);

    report = schema.validate(bad2);
    System.out.println(report);
  }
}

代码示例来源:origin: java-json-tools/json-schema-validator

report = schema.validate(good);
System.out.println(report);
report = schema.validate(bad);
System.out.println(report);
report = schema.validate(bad2);
System.out.println(report);

代码示例来源:origin: java-json-tools/json-schema-validator

report = schema.validate(good);
System.out.println(report);
report = schema.validate(bad);
System.out.println(report);

代码示例来源:origin: java-json-tools/json-schema-validator

report = schema.validate(good);
System.out.println(report);
report = schema.validate(bad);
System.out.println(report);

代码示例来源:origin: stackoverflow.com

public boolean validate(String jsonData, String jsonSchema, boolean useV4, boolean useId) throws Exception {
  // create the Json nodes for schema and data
  JsonNode schemaNode = JsonLoader.fromString(jsonSchema); // throws JsonProcessingException if error
  JsonNode data = JsonLoader.fromString(jsonData);         // same here

  JsonSchemaFactory factory = JsonSchemaFactories.withOptions(useV4, useId);
  // load the schema and validate
  JsonSchema schema = factory.fromSchema(schemaNode);
  ValidationReport report = schema.validate(data);

  return report.isSuccess();
}

代码示例来源:origin: stackoverflow.com

JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); 
  JsonSchema schema = factory.getJsonSchema(schemaNode);
  report = schema.validate(data);
} catch (JsonParseException jpex) {
  System.out.println("Error. Something went wrong trying to parse json data: #<#<"+jsonData+

代码示例来源:origin: com.github.bjansen/swagger-schema-validator

/**
 * Same as {@link #validate(String, String)} but with a custom JSON deserializer.
 */
public ProcessingReport validate(String jsonPayload, String definitionPointer, ObjectMapper jsonMapper) throws ProcessingException, IOException {
  JsonNode jsonNode = jsonMapper.readTree(jsonPayload);
  if (jsonNode == null) {
    throw new IOException("The JSON payload could not be parsed correctly");
  }
  return getSchema(definitionPointer).validate(jsonNode);
}

代码示例来源:origin: olacabs/fabric

private boolean isValid(String dsl) throws ProcessingException, IOException {
  JsonNode jsonNode = JsonLoader.fromString(dsl);
  ProcessingReport report;
  report = schema.validate(jsonNode);
  if (!report.isSuccess()) {
    throw new RuntimeException("Invalid Dsl :\\n" + report.toString());
  }
  return report.isSuccess();
}

代码示例来源:origin: com.github.cafdataprocessing/corepolicy-common

static public void validateJson(JsonNode json, JsonNode schemaDefinition) throws Exception {
    final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
    final JsonNode policyJson = json;
    final JsonNode policyTypeJson = schemaDefinition;

    final com.github.fge.jsonschema.main.JsonSchema schema =
        factory.getJsonSchema(policyTypeJson);

    ProcessingReport report = schema.validate(policyJson);
    if(!report.isSuccess()){
      throw new Exception(report.toString());
    }
  }
}

相关文章