// null is handled as a special case
if (field == null) {
if (optional || type == Type.Null) {
return;
}
throw new YokeException(errorCode, "'" + path + "' cannot be NULL");
}
switch (type) {
case Any:
return;
// base json types
case JsonObject:
if (field instanceof JsonObject || field instanceof Map) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case JsonArray:
if (field instanceof JsonArray || field instanceof List) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case String:
if (field instanceof String) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Number:
if (field instanceof Number) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Boolean:
if (field instanceof Boolean) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
// specific types
case Integer:
if (field instanceof Integer) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Long:
if (field instanceof Long) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Double:
if (field instanceof Double) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
// json schema validations
case DateTime:
if (field instanceof CharSequence && DATETIME.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Date:
if (field instanceof CharSequence && DATE.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Time:
if (field instanceof CharSequence && TIME.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Email:
if (field instanceof CharSequence && EMAIL.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case IPAddress:
if (field instanceof CharSequence && IPADDRESS.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case IPV6Address:
if (field instanceof CharSequence && IPV6ADDRESS.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case URI:
if (field instanceof CharSequence && URI.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Hostname:
if (field instanceof CharSequence && HOSTNAME.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Alpha:
if (field instanceof CharSequence && ALPHA.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
case Alphanumeric:
if (field instanceof CharSequence && ALPHANUMERIC.matcher((CharSequence) field).matches()) {
return;
}
throw new YokeException(errorCode, "'" + path + "' is not " + type.name());
}
// unknown
throw new YokeException(errorCode, "Failed to validate");
}
};
}