Skip to content

Instantly share code, notes, and snippets.

@pianovwork
Created March 10, 2016 23:13
Show Gist options
  • Save pianovwork/4c0f8da54b85619ff91d to your computer and use it in GitHub Desktop.
Save pianovwork/4c0f8da54b85619ff91d to your computer and use it in GitHub Desktop.
json-schema fge/json-schema-validator: minAge
import java.io.IOException;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jackson.NodeType;
import com.github.fge.jsonschema.cfg.ValidationConfiguration;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.keyword.syntax.checkers.helpers.TypeOnlySyntaxChecker;
import com.github.fge.jsonschema.core.processing.Processor;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.keyword.digest.helpers.SimpleDigester;
import com.github.fge.jsonschema.keyword.validator.AbstractKeywordValidator;
import com.github.fge.jsonschema.library.DraftV4Library;
import com.github.fge.jsonschema.library.Keyword;
import com.github.fge.jsonschema.library.Library;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.processors.data.FullData;
import com.github.fge.msgsimple.bundle.MessageBundle;
public class MinAgeValidationExample {
public static void main(final String... args) throws IOException, ProcessingException {
final JsonNode customSchema = JsonLoader.fromString("{\"properties\":{\"birthday\":{\"type\":\"object\",\"minAge\":14}}}");
final JsonNode json = JsonLoader.fromString("{\"birthday\":{\"year\":\"2015\",\"month\":\"3\",\"day\":\"15\"}}");
final Keyword minAge = Keyword.newBuilder("minAge")
.withSyntaxChecker(new TypeOnlySyntaxChecker("minAge", NodeType.INTEGER))
.withDigester(new SimpleDigester("minAge", NodeType.OBJECT))
.withValidatorClass(MinAgeKeywordValidator.class).freeze();
final Library library = DraftV4Library.get().thaw()
.addKeyword(minAge)
.freeze();
final ValidationConfiguration cfg = ValidationConfiguration.newBuilder()
.setDefaultLibrary("http://my.site/myschema#", library)
.freeze();
final JsonSchemaFactory factory = JsonSchemaFactory.newBuilder()
.setValidationConfiguration(cfg).freeze();
final JsonSchema schema = factory.getJsonSchema(customSchema);
ProcessingReport report;
report = schema.validate(json);
System.out.println(report);
}
public static final class MinAgeKeywordValidator extends AbstractKeywordValidator {
private final int minAge;
public MinAgeKeywordValidator(final JsonNode digest) {
super("minAge");
minAge = digest.get("minAge").intValue();
}
@Override
public String toString() {
return keyword + ": " + minAge;
}
@Override
public void validate(Processor<FullData, FullData> processor, ProcessingReport report, MessageBundle bundle, FullData data) throws ProcessingException {
System.out.println();
final JsonNode instance = data.getInstance().getNode();
final int year = instance.get("year").asInt();
final int month = instance.get("month").asInt();
final int date = instance.get("day").asInt();
LocalDate birthday = new LocalDate(year, month, date);
Period period = new Period(birthday, LocalDate.now(DateTimeZone.UTC));
int years = period.getYears();
if (years < minAge) {
report.error(
newMsg(data, bundle, "err.common.min.age")
.putArgument(keyword, minAge)
);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment