/**
   * Validate the instance
   *
   * <p>Unlike all other validators:
   *
   * <ul>
   *   <li>this is the only one which will, if required, go over the net to grab new schemas;
   *   <li>this is the only one which can spawn a {@link ValidationContext} with a different root
   *       schema (if the ref represents an absolute {@link URI});
   *   <li>this is the only validator implementation which can spawn errors (ie, {@link
   *       ValidationReport#isError()} returns {@code true}) and not only failures.
   * </ul>
   *
   * @return the report from the spawned validator
   */
  @Override
  public ValidationReport validate(final ValidationContext context, final JsonNode instance)
      throws JsonValidationFailureException {
    final ValidationReport report = context.createReport();

    final String ref = context.getSchema().get(keyword).getTextValue();

    final URI uri, baseURI;
    final JsonPointer pointer;

    try {
      uri = new URI(ref);
      baseURI = new URI(uri.getScheme(), uri.getSchemeSpecificPart(), null);
      pointer = new JsonPointer(uri.getRawFragment());
    } catch (URISyntaxException e) {
      throw new RuntimeException(
          "PROBLEM: invalid URI found (" + ref + "), syntax validation should have caught that", e);
    }

    final ValidationContext ctx;
    try {
      ctx = context.fromURI(baseURI);
    } catch (IOException e) {
      report.error(
          String.format(
              "cannot download schema at ref %s: %s: " + "%s",
              ref, e.getClass().getName(), e.getMessage()));
      return report;
    } catch (IllegalArgumentException e) {
      report.error(String.format("cannot use ref %s: %s", ref, e.getMessage()));
      return report;
    }

    return ctx.getValidator(pointer, instance).validate(ctx, instance);
  }
Beispiel #2
0
  /**
   * Validates JSON node against a JSON schema.
   *
   * @param json {@link JsonNode} to validate.
   * @param schema {@link JsonNode} schema.
   * @return {@link Validator}.
   */
  public static Validator validateJson(JsonNode json, JsonNode schema) {
    Validator res = new Validator();

    JsonValidator validator;
    try {
      validator = new JsonValidator(schema);

      ValidationReport report;

      report = validator.validate(json);

      int i = 0;
      for (String s : report.getMessages()) {
        res.addError("json_error_" + i, s);
        i++;
      }
    } catch (JsonValidationFailureException e) {
      res.addError("jsonerror", "Unable to validate json");
      log.info(String.format("JsonUtils.validateJson(): Unable to validate json"));
    }
    return res;
  }
  @Override
  public void validate(final ValidationReport report, final JsonNode instance) {
    final JsonNode oldParent = report.getSchema();

    report.setSchema(parent);

    for (final KeywordValidator validator : validators)
      validator.validateInstance(report, instance);

    if (!(instance.isContainerNode() || report.isSuccess())) {
      report.setSchema(oldParent);
      return;
    }

    final JsonPointer ptr = report.getPath();

    JsonPointer current;

    if (instance.isArray()) {
      JsonNode subSchema;
      int i = 0;
      for (final JsonNode element : instance) {
        current = ptr.append(i);
        report.setPath(current);
        subSchema = arrayPath(i);
        JsonSchema.fromNode(parent, subSchema).validate(report, element);
        i++;
      }
      report.setSchema(oldParent);
      report.setPath(ptr);
      return;
    }

    // Can only be an object now
    final Map<String, JsonNode> map = CollectionUtils.toMap(instance.fields());

    String key;
    JsonNode value;

    for (final Map.Entry<String, JsonNode> entry : map.entrySet()) {
      key = entry.getKey();
      value = entry.getValue();
      current = ptr.append(key);
      report.setPath(current);
      for (final JsonNode subSchema : objectPath(key))
        JsonSchema.fromNode(parent, subSchema).validate(report, value);
    }

    report.setSchema(oldParent);
    report.setPath(ptr);
  }