Search in sources :

Example 1 with TimestampValidator

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;
}
Also used : TimestampValidator(com.thinkbiganalytics.policy.validation.TimestampValidator) InvalidFormatException(com.thinkbiganalytics.spark.util.InvalidFormatException) Date(java.sql.Date)

Example 2 with TimestampValidator

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());
}
Also used : FieldValidationRule(com.thinkbiganalytics.policy.rest.model.FieldValidationRule) TimestampValidator(com.thinkbiganalytics.policy.validation.TimestampValidator) Test(org.junit.Test)

Example 3 with TimestampValidator

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));
}
Also used : TimestampValidator(com.thinkbiganalytics.policy.validation.TimestampValidator) Test(org.junit.Test)

Aggregations

TimestampValidator (com.thinkbiganalytics.policy.validation.TimestampValidator)3 Test (org.junit.Test)2 FieldValidationRule (com.thinkbiganalytics.policy.rest.model.FieldValidationRule)1 InvalidFormatException (com.thinkbiganalytics.spark.util.InvalidFormatException)1 Date (java.sql.Date)1