use of com.thinkbiganalytics.policy.validation.TimestampValidator in project kylo by Teradata.
the class HCatDataType method isValueConvertibleToType.
public boolean isValueConvertibleToType(Object val, boolean enforcePrecision) {
try {
if (val instanceof String) {
String strVal = (String) val;
if (strVal != null && !isnumeric) {
if (convertibleType == Timestamp.class) {
return new TimestampValidator(true).validate(strVal);
} else if (convertibleType == Date.class) {
return DateValidator.instance().validate(strVal);
} else if (convertibleType == byte[].class) {
return true;
}
}
Comparable nativeValue = toNativeValue(strVal);
if (nativeValue != null) {
if (isnumeric) {
if (min != null && min.compareTo(nativeValue) > 0) {
return false;
}
if (max != null && max.compareTo(nativeValue) < 0) {
return false;
}
if (digits != null && !(!enforcePrecision || validatePrecision(nativeValue))) {
return false;
}
} else if (isstring && strVal.length() > maxlength) {
return false;
}
}
} else {
return val == null || val.getClass() == convertibleType || val instanceof Number && Number.class.isAssignableFrom(convertibleType);
}
} catch (InvalidFormatException | ClassCastException | IllegalArgumentException e) {
return false;
}
return true;
}
use of com.thinkbiganalytics.policy.validation.TimestampValidator in project kylo by Teradata.
the class TestValidationTransformation method testTimestampValidator.
@Test
public void testTimestampValidator() {
TimestampValidator validator = new TimestampValidator(false);
FieldValidationRule uiModel = ValidatorAnnotationTransformer.instance().toUIModel(validator);
TimestampValidator convertedValidator = fromUI(uiModel, TimestampValidator.class);
Assert.assertEquals(validator.isAllowNull(), convertedValidator.isAllowNull());
}
use of com.thinkbiganalytics.policy.validation.TimestampValidator in project kylo by Teradata.
the class ValidatorTests method timestampTests.
@Test
public void timestampTests() {
TimestampValidator validator = new TimestampValidator(false);
Assert.assertTrue(validator.validate("2004-12-01 12:00:00"));
Assert.assertTrue(validator.validate("2004-12-01T12:00:00"));
Assert.assertTrue(validator.validate("2004-12-01T12:00:00.124Z"));
Assert.assertTrue(validator.validate("2009-06-18T18:50:57-06:00"));
Assert.assertTrue(validator.validate("2009-06-18T18:30:45Z"));
Assert.assertTrue(validator.validate("2009-06-18T18:39Z"));
Assert.assertTrue(validator.validate("2001-01-12"));
Assert.assertTrue(validator.validate("1984-10-16"));
Assert.assertFalse(validator.validate("2004-1-1 12:00:00"));
Assert.assertFalse(validator.validate("2004-12-0112:00:00"));
Assert.assertFalse(validator.validate("20041201120000"));
// Test NULL value
TimestampValidator nullValidator = new TimestampValidator(true);
Assert.assertTrue(nullValidator.validate(null));
}
Aggregations