All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openapi4j.schema.validator.SkemaBackedJsonValidator Maven / Gradle / Ivy

There is a newer version: 2.0.0-RC3
Show newest version
package org.openapi4j.schema.validator;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.erosb.jsonsKema.IJsonValue;
import com.github.erosb.jsonsKema.JsonParser;
import com.github.erosb.jsonsKema.Schema;
import com.github.erosb.jsonsKema.SchemaLoader;
import com.github.erosb.jsonsKema.ValidationFailure;
import com.github.erosb.jsonsKema.Validator;

import java.net.URI;

public class SkemaBackedJsonValidator implements JsonValidator {

  private final Schema schema;

  public SkemaBackedJsonValidator(JsonNode rawJson, URI documentSource) {
    String schemaJsonString = rawJson.toPrettyString();
    schema = new SchemaLoader(new JsonParser(schemaJsonString, documentSource).parse())
      .load();
  }

  @Override
  public boolean validate(JsonNode valueNode, URI documentSource, ValidationData validation) {
    String jsonString = valueNode.toPrettyString();
    IJsonValue jsonValue = new JsonParser(jsonString, documentSource).parse();
    ValidationFailure failure = Validator.forSchema(schema).validate(jsonValue);
    if (failure != null) {
      validation.add(failure);
      return false;
    }
    return true;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy