org.openapi4j.schema.validator.SkemaBackedJsonValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-schema-validator Show documentation
Show all versions of openapi-schema-validator Show documentation
openapi4j schema data validator
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;
}
}