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

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

本文整理了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

  1. public static void main(final String... args)
  2. throws IOException, ProcessingException {
  3. final JsonNode fstabSchema = Utils.loadResource("/fstab.json");
  4. final JsonNode good = Utils.loadResource("/fstab-good.json");
  5. final JsonNode bad = Utils.loadResource("/fstab-bad.json");
  6. final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
  7. final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
  8. final JsonSchema schema = factory.getJsonSchema(fstabSchema);
  9. ProcessingReport report;
  10. report = schema.validate(good);
  11. System.out.println(report);
  12. report = schema.validate(bad);
  13. System.out.println(report);
  14. report = schema.validate(bad2);
  15. System.out.println(report);
  16. }
  17. }

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

  1. @Override
  2. protected boolean matchesSafely(String content) {
  3. try {
  4. JsonNode contentAsJsonNode = JsonLoader.fromString(content);
  5. JsonSchemaFactory jsonSchemaFactory = instanceSettings.jsonSchemaFactory();
  6. Schema loadedSchema = loadSchema(schema, instanceSettings);
  7. final JsonSchema jsonSchema;
  8. if (loadedSchema.hasType(JsonNode.class)) {
  9. jsonSchema = jsonSchemaFactory.getJsonSchema(JsonNode.class.cast(loadedSchema.schema));
  10. } else if (loadedSchema.hasType(String.class)) {
  11. jsonSchema = jsonSchemaFactory.getJsonSchema(String.class.cast(loadedSchema.schema));
  12. } else {
  13. throw new RuntimeException("Internal error when loading schema from factory. Type was " + loadedSchema.schema.getClass().getName());
  14. }
  15. if (instanceSettings.shouldUseCheckedValidation()) {
  16. report = jsonSchema.validate(contentAsJsonNode);
  17. } else {
  18. report = jsonSchema.validateUnchecked(contentAsJsonNode);
  19. }
  20. return report.isSuccess();
  21. } catch (Exception e) {
  22. throw new JsonSchemaValidationException(e);
  23. }
  24. }

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

  1. public ProcessingReport validate(String jsonText) throws IOException {
  2. ProcessingReport report;
  3. JsonNode toValidate = JsonLoader.fromString(jsonText);
  4. try {
  5. report = this.schema.validate(toValidate);
  6. } catch (ProcessingException e) {
  7. throw new IOException("Error validating json text : " + e.getMessage());
  8. }
  9. return report;
  10. }
  11. }

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

  1. public static void main(final String... args)
  2. throws IOException, ProcessingException
  3. {
  4. final JsonNode good = Utils.loadResource("/fstab-good.json");
  5. final JsonNode bad = Utils.loadResource("/fstab-bad.json");
  6. final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
  7. final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
  8. final JsonSchema schema = factory.getJsonSchema(SCHEMA_URI);
  9. ProcessingReport report;
  10. report = schema.validate(good);
  11. System.out.println(report);
  12. report = schema.validate(bad);
  13. System.out.println(report);
  14. report = schema.validate(bad2);
  15. System.out.println(report);
  16. }
  17. }

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

  1. public static void main(final String... args)
  2. throws IOException, ProcessingException
  3. {
  4. final JsonNode fstabSchema = Utils.loadResource("/fstab.json");
  5. final JsonNode good = Utils.loadResource("/fstab-good.json");
  6. final JsonNode bad = Utils.loadResource("/fstab-bad.json");
  7. final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
  8. final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
  9. final JsonSchema schema = factory.getJsonSchema(fstabSchema);
  10. ProcessingReport report;
  11. report = schema.validate(good);
  12. System.out.println(report);
  13. report = schema.validate(bad);
  14. System.out.println(report);
  15. report = schema.validate(bad2);
  16. System.out.println(report);
  17. }
  18. }

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

  1. @Override
  2. public RetCode validateInstance(final JsonSchema schema,
  3. final String fileName, final JsonNode node)
  4. throws IOException, ProcessingException
  5. {
  6. final ListProcessingReport report
  7. = (ListProcessingReport) schema.validate(node, true);
  8. final boolean success = report.isSuccess();
  9. System.out.println("--- BEGIN " + fileName + "---");
  10. System.out.println("validation: " + (success ? "SUCCESS"
  11. : "FAILURE"));
  12. if (!success)
  13. System.out.println(JacksonUtils.prettyPrint(report
  14. .asJson()));
  15. System.out.println("--- END " + fileName + "---");
  16. return success ? ALL_OK : VALIDATION_FAILURE;
  17. }
  18. },

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

  1. public static void main(final String... args)
  2. throws IOException, ProcessingException
  3. {
  4. final JsonNode fstabSchema = Utils.loadResource("/fstab-draftv3.json");
  5. final JsonNode good = Utils.loadResource("/fstab-good.json");
  6. final JsonNode bad = Utils.loadResource("/fstab-bad.json");
  7. final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
  8. final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
  9. final JsonSchema schema = factory.getJsonSchema(fstabSchema);
  10. ProcessingReport report;
  11. report = schema.validate(good);
  12. System.out.println(report);
  13. report = schema.validate(bad);
  14. System.out.println(report);
  15. report = schema.validate(bad2);
  16. System.out.println(report);
  17. }
  18. }

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

  1. public static void main(final String... args)
  2. throws IOException, ProcessingException
  3. {
  4. final JsonNode fstabSchema = Utils.loadResource("/fstab-inline.json");
  5. final JsonNode good = Utils.loadResource("/fstab-good.json");
  6. final JsonNode bad = Utils.loadResource("/fstab-bad.json");
  7. final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
  8. final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
  9. .dereferencing(Dereferencing.INLINE).freeze();
  10. final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
  11. .setLoadingConfiguration(cfg).freeze();
  12. final JsonSchema schema = factory.getJsonSchema(fstabSchema);
  13. ProcessingReport report;
  14. report = schema.validate(good);
  15. System.out.println(report);
  16. report = schema.validate(bad);
  17. System.out.println(report);
  18. report = schema.validate(bad2);
  19. System.out.println(report);
  20. }
  21. }

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

  1. public static void main(final String... args)
  2. throws IOException, ProcessingException
  3. {
  4. final JsonNode good = Utils.loadResource("/fstab-good.json");
  5. final JsonNode bad = Utils.loadResource("/fstab-bad.json");
  6. final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
  7. final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
  8. .addScheme("foobar", CustomDownloader.getInstance()).freeze();
  9. final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
  10. .setLoadingConfiguration(cfg).freeze();
  11. final JsonSchema schema
  12. = factory.getJsonSchema("foobar:/fstab.json#");
  13. ProcessingReport report;
  14. report = schema.validate(good);
  15. System.out.println(report);
  16. report = schema.validate(bad);
  17. System.out.println(report);
  18. report = schema.validate(bad2);
  19. System.out.println(report);
  20. }

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

  1. private static void doValidate(final Map<String, JsonNode> schemas,
  2. final int i)
  3. throws ProcessingException
  4. {
  5. String name;
  6. JsonNode value;
  7. ProcessingReport report;
  8. for (final Map.Entry<String, JsonNode> entry: schemas.entrySet()) {
  9. name = entry.getKey();
  10. value = entry.getValue();
  11. report = SCHEMA.validate(value);
  12. if (!report.isSuccess()) {
  13. System.err.println("ERROR: schema " + name + " did not "
  14. + "validate (iteration " + i + ')');
  15. System.exit(1);
  16. }
  17. }
  18. }
  19. }

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

  1. public static void main(final String... args)
  2. throws IOException, ProcessingException
  3. {
  4. final JsonNode good = Utils.loadResource("/fstab-good.json");
  5. final JsonNode bad = Utils.loadResource("/fstab-bad.json");
  6. final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
  7. final URITranslatorConfiguration translatorCfg
  8. = URITranslatorConfiguration.newBuilder()
  9. .setNamespace(NAMESPACE).freeze();
  10. final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
  11. .setURITranslatorConfiguration(translatorCfg).freeze();
  12. final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
  13. .setLoadingConfiguration(cfg).freeze();
  14. final JsonSchema schema = factory.getJsonSchema("fstab.json");
  15. ProcessingReport report;
  16. report = schema.validate(good);
  17. System.out.println(report);
  18. report = schema.validate(bad);
  19. System.out.println(report);
  20. report = schema.validate(bad2);
  21. System.out.println(report);
  22. }
  23. }

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

  1. public static void main(final String... args)
  2. throws IOException, ProcessingException
  3. {
  4. final JsonNode good = Utils.loadResource("/fstab-good.json");
  5. final JsonNode bad = Utils.loadResource("/fstab-bad.json");
  6. final JsonNode bad2 = Utils.loadResource("/fstab-bad2.json");
  7. final URITranslatorConfiguration translatorCfg
  8. = URITranslatorConfiguration.newBuilder()
  9. .addSchemaRedirect(FROM, TO).freeze();
  10. final LoadingConfiguration cfg = LoadingConfiguration.newBuilder()
  11. .setURITranslatorConfiguration(translatorCfg).freeze();
  12. final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
  13. .setLoadingConfiguration(cfg).freeze();
  14. final JsonSchema schema = factory.getJsonSchema(FROM);
  15. ProcessingReport report;
  16. report = schema.validate(good);
  17. System.out.println(report);
  18. report = schema.validate(bad);
  19. System.out.println(report);
  20. report = schema.validate(bad2);
  21. System.out.println(report);
  22. }
  23. }

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

  1. report = schema.validate(good);
  2. System.out.println(report);
  3. report = schema.validate(bad);
  4. System.out.println(report);
  5. report = schema.validate(bad2);
  6. System.out.println(report);

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

  1. report = schema.validate(good);
  2. System.out.println(report);
  3. report = schema.validate(bad);
  4. System.out.println(report);

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

  1. report = schema.validate(good);
  2. System.out.println(report);
  3. report = schema.validate(bad);
  4. System.out.println(report);

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

  1. public boolean validate(String jsonData, String jsonSchema, boolean useV4, boolean useId) throws Exception {
  2. // create the Json nodes for schema and data
  3. JsonNode schemaNode = JsonLoader.fromString(jsonSchema); // throws JsonProcessingException if error
  4. JsonNode data = JsonLoader.fromString(jsonData); // same here
  5. JsonSchemaFactory factory = JsonSchemaFactories.withOptions(useV4, useId);
  6. // load the schema and validate
  7. JsonSchema schema = factory.fromSchema(schemaNode);
  8. ValidationReport report = schema.validate(data);
  9. return report.isSuccess();
  10. }

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

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

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

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

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

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

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

  1. static public void validateJson(JsonNode json, JsonNode schemaDefinition) throws Exception {
  2. final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
  3. final JsonNode policyJson = json;
  4. final JsonNode policyTypeJson = schemaDefinition;
  5. final com.github.fge.jsonschema.main.JsonSchema schema =
  6. factory.getJsonSchema(policyTypeJson);
  7. ProcessingReport report = schema.validate(policyJson);
  8. if(!report.isSuccess()){
  9. throw new Exception(report.toString());
  10. }
  11. }
  12. }

相关文章