@Override public Object convert(ResponseBody value) throws IOException { Object convert = delegateConverter.convert(value); try { Rave.getInstance().validate(convert); } catch (RaveException e) { // This response didn't pass RAVE validation, throw an exception. throw new RuntimeException(e); } return convert; } }
/** * Register this {@link BaseValidator} with the {@link Rave} registry. */ protected final void registerSelf() { Rave.getInstance().registerValidator(this, supportedClasses); }
/** * This class kicks the validate call back up through the RAVE api. This is because this particular validator may * not know how to handle the input class. Using the Rave.validate api allows Rave to correctly route the object * to the appropriate validator. * * @param clazz the class to validate this object as. * @param obj the object to validate * @return A list of {@link RaveError}s. */ @Nullable protected final List<RaveError> reEvaluateAsSuperType( Class<?> clazz, Object obj) { try { Rave.getInstance().validateAs(obj, clazz); } catch (RaveException e) { return e.errors; } return null; }
@Test public void validateIgnoreUnsupported_whenTypeIsNotSupported_shouldNotFailWithRaveUnsupportedException() throws RaveException { Rave.getInstance().validateIgnoreUnsupported(new Object()); }
@Override public boolean removeEldestEntry(Map.Entry<Class<?>, Void> eldest) { boolean returnValue = size() > cacheSize; if (returnValue) { getInstance().removeEntry(eldest.getKey()); } return returnValue; } };
/** * Validate an object. If the object is not supported, nothing will happen. Otherwise the object will be routed to * the correct sub-validator which knows how to validate it. * * @param object the object to be validated. * @throws InvalidModelException if validation fails. */ public void validateIgnoreUnsupported(Object object) throws InvalidModelException { try { validate(object); } catch (InvalidModelException e) { throw e; } catch (RaveException e) { } } /**
/** * Since various objects may have subclass/interfaces that also require validation this mechanism allows for * targeted validation of different inherited subtypes on the same object. * * @param obj the object to validate * @param clazz the class type to validate it as. * validation. * @throws RaveException thrown if the validation process fails. */ void validateAs(Object obj, Class<?> clazz) throws RaveException { if (!clazz.isInstance(obj)) { throw new IllegalArgumentException("Trying to validate " + obj.getClass().getCanonicalName() + " as " + clazz.getCanonicalName()); } BaseValidator base; synchronized (this) { base = classValidatorMap.get(clazz); if (base == null) { base = getValidatorInstance(clazz); } if (base == null) { throw new UnsupportedObjectException(Collections.singletonList( new RaveError(obj.getClass(), "", RaveErrorStrings.CLASS_NOT_SUPPORTED_ERROR))); } } base.validateAs(obj, clazz); }
@Test public void validate_whenNonSupportedClass_shouldNotThrowException() throws RaveException { Rave.getInstance().validateIgnoreUnsupported(Collections.emptyList()); }
/** * Process any {@link Class} that is not annotated with the {@link Validated} annotation. * * @param clazz the {@link Class} to process. */ void processNonAnnotatedClasses(Class<?> clazz) { Validated validated = clazz.getAnnotation(Validated.class); if (validated != null) { throw new IllegalArgumentException( clazz.getCanonicalName() + " is annotated with " + Validated.class.getCanonicalName()); } if (!traverseClassHierarchy(clazz, clazz)) { unsupportedClassesCache.put(clazz, null); } addSupportedClass(clazz); getInstance().registerValidatorWithClass(this, clazz); }
/** * Validate an object. * * @param object the object to be validated. * @throws RaveException if validation fails. */ public void validate(Object object) throws RaveException { Class<?> clazz = object.getClass(); Validated validated = clazz.getAnnotation(Validated.class); BaseValidator validator; synchronized (this) { if (validated == null && !unannotatedModelValidator.hasSeen(clazz)) { unannotatedModelValidator.processNonAnnotatedClasses(clazz); } validator = classValidatorMap.get(clazz); if (validator == null) { validator = getValidatorInstance(clazz); } if (validator == null) { throw new UnsupportedObjectException(Collections.singletonList( new RaveError(clazz, "", RaveErrorStrings.CLASS_NOT_SUPPORTED_ERROR))); } } validator.validate(object); }
in = new ObjectInputStream(bis); Owner deserializedOwner = (Owner) in.readObject(); Rave.getInstance().validate(deserializedOwner); return deserializedOwner; } catch (ClassNotFoundException | IOException e) {
@Test(expected = IllegalArgumentException.class) public void validateAs_whenClazzTypeDoesntMatchObjectType_shouldFailWithException() throws RaveException { Rave.getInstance().validateAs(new Object(), String.class); }
@Test public void unAnnotateValidatorHandlerValidateAs_whenUnValidateUnsupportedClass_shouldNotThrowException() throws RaveException { Rave.getInstance().validateIgnoreUnsupported(new NonAnnotated.UnAnnotatedEvenWithInheritance()); }
@Test(expected = IllegalStateException.class) public void registerValidator_whenTwoValidatorsOnSameModel_shouldFailwithIllegalStateException() { TestValidator testValidator = new TestValidator(); Rave.getInstance().registerValidator(testValidator, testValidator.getSupportedClasses()); Rave.getInstance().registerValidator(testValidator, testValidator.getSupportedClasses()); }
/** * 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; }
@Test(expected = UnsupportedObjectException.class) public void validateAs_whenTypeIsNotSupported_shouldFailWithRaveUnsupportedException() throws RaveException { Rave.getInstance().validateAs(new Object(), Object.class); }
return errors; Rave rave = Rave.getInstance(); for (T type : array) { if (type == null) { rave.validate(type); } catch (UnsupportedObjectException e) { return errors;
Rave.getInstance().validate(obj); } catch (UnsupportedObjectException e) { return Collections.<RaveError>emptyList();
@Test(expected = UnsupportedObjectException.class) public void validate_whenNonSupportedClass_shouldThrowException() throws RaveException { Rave.getInstance().validate(Collections.emptyList()); }
@Test(expected = UnsupportedObjectException.class) public void validate_whenTypeIsNotSupported_shouldFailWithRaveUnsupportedException() throws RaveException { Rave.getInstance().validate(new Object()); }