/** * Validate the size of an Array. * * @param array the array to validate. * @param isNullable if true than null is a valid value for the input string regardless of min and max. * @param min The Array must have least this many elements in it. * @param max The Array can have at most this many elements in it. * @param multiple The multiple constraint on the Size. If less than zero it is * ignored. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @param <T> can be anytype. * @return {@link List} of {@link RaveError}s which list the validation violations. Null otherwise. */ @Nullable protected static <T> List<RaveError> isSizeOk( @Nullable T[] array, boolean isNullable, long min, long max, long multiple, ValidationContext validationContext) { List<RaveError> raveErrors = checkNullable(array, isNullable, validationContext); if (array == null) { return raveErrors; } raveErrors = testMultipleParameter(multiple, array.length, validationContext, "", raveErrors); if (array.length <= max && array.length >= min) { return raveErrors; } String msg = "With size" + array.length + " is not within " + "bounds min:" + min + " and max:" + max; return appendError(validationContext, msg, raveErrors); }
/** * Utility to check if the strings in a array of strings match acceptable values. If the input array is null then * nothing is done. * * @param isNullable if the value is allowed to be null or not. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @param values a array of strings to validate. * @param acceptableValues list acceptable values for the input array. * @return returns an error if string is not present in list. Otherwise returns an empty list. */ protected static List<RaveError> checkStringDef( boolean isNullable, ValidationContext validationContext, @Nullable String[] values, String... acceptableValues) { List<RaveError> errors = checkNullable(values, isNullable, validationContext); if (values == null) { return errors; } for (String s : values) { errors = mergeErrors(errors, checkStringDef(false, validationContext, s, acceptableValues)); } return errors == null ? Collections.<RaveError>emptyList() : errors; }
boolean isNullable, ValidationContext validationContext) { List<RaveError> errors = checkNullable((Object) array, isNullable, validationContext); if (array == null) { return errors; return errors; } catch (RaveException e) { errors = appendErrors(e, errors);
/** * Checks to see if the object is null. * * @param collection the collection to validate. * @param isNullable is the object is allowed to be null. * @param validationContext the context of the item in the class being validated. This is used in case of an error. * @return a list of of {@link RaveError}s if the object is not allowed to be null. Returns null otherwise. */ protected static List<RaveError> checkNullable( @Nullable Collection<?> collection, boolean isNullable, ValidationContext validationContext) { List<RaveError> errors = checkNullable((Object) collection, isNullable, validationContext); return collection == null ? errors : checkIterable(collection, errors); }
@Test public void checkMapCollection_whenMapIsNotNull_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(HashMap.class); List<RaveError> errors = BaseValidator.checkNullable(new HashMap<>(), false, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkString_whenCollectionEmptyOrNull_shouldNotProduceErrors() { Collection<String> strings = new ArrayList<>(); BaseValidator.ValidationContext context = BaseValidator.getValidationContext(Object.class); List<RaveError> errors = BaseValidator.checkStringDef(false, context, strings); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, (Collection<String>) null); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, (Collection<String>) null, "Valid"); assertThat(errors).isEmpty(); }
/** * Utility to check if a string is present in a list of strings. If the string is null then do nothing. * * @param isNullable if the value is allowed to be null or not. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @param value string to look for. * @param acceptableValues list of strings to look in. * @return returns an error if string is not present in list. Otherwise returns an empty list. */ protected static List<RaveError> checkStringDef( boolean isNullable, ValidationContext validationContext, @Nullable String value, String... acceptableValues) { List<RaveError> errors = checkNullable(value, isNullable, validationContext); if (value == null) { return errors; } for (String string : acceptableValues) { if (value.equals(string)) { return Collections.<RaveError>emptyList(); } } return createStringDefError(value, acceptableValues, validationContext); }
@Test public void checkLongDef_whenValueIsCorrect_shouldProduceNoErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); assertThat(BaseValidator.checkLongDef(context, 10L, false, 10L).isEmpty()); }
@Test public void checkIntDef_whenValueIsCorrect_shouldProduceNoErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); assertThat(BaseValidator.checkIntDef(context, 10, false, 10).isEmpty()); }
@Test public void checkIntRange_whenValuesInFloatRange_shouldReturnEmptyList() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); assertThat(BaseValidator.checkFloatRange(context, 10d, -1.0d, 10d)).isEmpty(); assertThat(BaseValidator.checkFloatRange(context, 10d, -1.0d, 11d)).isEmpty(); assertThat(BaseValidator.checkFloatRange(context, -1d, -1d, 11d)).isEmpty(); assertThat(BaseValidator.checkFloatRange(context, -1d, Double.NEGATIVE_INFINITY, 11d)).isEmpty(); assertThat(BaseValidator.checkFloatRange(context, -1d, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)) .isEmpty(); assertThat(BaseValidator.checkFloatRange(context, -1d, -10d, Double.POSITIVE_INFINITY)).isEmpty(); assertThat(BaseValidator.checkFloatRange(context, Double.POSITIVE_INFINITY, -10d, Double.POSITIVE_INFINITY)) .isEmpty(); assertThat(BaseValidator.checkFloatRange(context, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)).isEmpty(); }
@Test public void checkIntRange_whenValuesInRange_shouldReturnEmptyList() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); assertThat(BaseValidator.checkIntRange(context, 10, -1, 10).isEmpty()).isTrue(); assertThat(BaseValidator.checkIntRange(context, 10, -1, 11).isEmpty()).isTrue(); assertThat(BaseValidator.checkIntRange(context, -1, -1, 11).isEmpty()).isTrue(); }
/** * Validate the elements in a {@link Iterable}. * * @param iterable the items to check * @param errors the {@link List} of errros seen so far. * @param <T> The type of each object in the {@link Iterable}. * @return any new errors. */ private static <T> List<RaveError> checkIterable(Iterable<T> iterable, @Nullable List<RaveError> errors) { Rave rave = Rave.getInstance(); for (T type : iterable) { if (type == null) { continue; } try { rave.validate(type); } catch (UnsupportedObjectException e) { return (errors == null) ? Collections.<RaveError>emptyList() : errors; } catch (RaveException e) { errors = appendErrors(e, errors); } } return (errors == null) ? Collections.<RaveError>emptyList() : errors; }
/** * Checks to see if the size parameter of an some element is a multiple of the in input multiple value. * * @param multiple the multiple value to check the size against. * @param size the size value to check against. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @param elementType the type of the element being checked. * @param raveErrors the list of {@link RaveError}s. * @return a list of {@link RaveError}. */ private static List<RaveError> testMultipleParameter( long multiple, int size, ValidationContext validationContext, String elementType, List<RaveError> raveErrors) { if (multiple >= 0 && size % multiple != 0) { String msg = elementType + " is not a multiple of " + multiple + ", size is " + size; raveErrors = appendError(validationContext, msg, raveErrors); } return raveErrors; }
/** * Check the value of an input int to see if it matches the acceptable values. This is the check that is run for * the IntDef annotation. Note the flag value for this is currently unused but * we may add support later. * * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @param value The long value to check. * @param flag the is the flag value from IntDef it is currently unsupported. * @param acceptableValues the values that the value parameter can take on. * @return an error if value is not present in list of acceptable values. Otherwise returns an empty list. */ protected static List<RaveError> checkIntDef( ValidationContext validationContext, int value, boolean flag, int... acceptableValues) { for (int acceptable : acceptableValues) { if (value == acceptable) { return Collections.<RaveError>emptyList(); } } return createIntDefError(value, acceptableValues, validationContext); }
@Test public void checkNullableMap_whenMapIsNotNullAndIsNullable_shouldNotProduceErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(HashMap.class); List<RaveError> errors = BaseValidator.checkNullable(new HashMap<>(), true, context); assertThat(errors).isNotNull(); assertThat(errors).isEmpty(); }
@Test public void checkString_whenArrayIsNullOrEmpty_shouldNotProduceErrors() { String[] strings = new String[0]; BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); List<RaveError> errors = BaseValidator.checkStringDef(false, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, strings, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, (String[]) null, "Valid1", "Valid2", "Valid3"); assertThat(errors).isEmpty(); errors = BaseValidator.checkStringDef(true, context, (String[]) null); assertThat(errors).isEmpty(); }
/** * Checks to see if the map is null. * * @param map the map to validate. * @param isNullable is the object is allowed to be null. * @param validationContext The context of the item in the class being validated. This is used in case of an error. * @return a list of of {@link RaveError}s if the object is not allowed to be null. Returns null otherwise. This * method will also RAVE validate the keys and values of the map if they are RAVE enabled. */ protected static <K, V> List<RaveError> checkNullable( @Nullable Map<K, V> map, boolean isNullable, ValidationContext validationContext) { List<RaveError> errors = checkNullable((Object) map, isNullable, validationContext); if (map == null) { return errors; } errors = checkIterable(map.keySet(), errors); return checkIterable(map.values(), errors); }
@Test public void checkLongDef_whenValueMatchesSet_shouldProduceNoErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); assertThat(BaseValidator.checkLongDef(context, 10L, false, 10L, 11L, 12L, 13L).isEmpty()); }
@Test public void checkIntDef_whenValueMatchesSet_shouldProduceNoErrors() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); assertThat(BaseValidator.checkIntDef(context, 10, false, 10, 11, 12, 13).isEmpty()); }
@Test public void checkIntRange_whenValuesNotInFloatRange_shouldReturnError() { BaseValidator.ValidationContext context = BaseValidator.getValidationContext(String.class); assertThat(BaseValidator.checkFloatRange(context, -10d, -1d, 10d)).isNotEmpty(); assertThat(BaseValidator.checkFloatRange(context, 12d, -1d, 11d)).isNotEmpty(); List<RaveError> errors = BaseValidator.checkFloatRange(context, 12d, -1d, 11d); assertThat(errors).hasSize(1); assertThat(errors.get(0).getErrorMsg()).contains(RaveErrorStrings.FLOAT_RANGE_ERROR); }