critical()critical(final Class<? extends ValidationException> clazz)handlerInvalidField(final HandlerInvalidField<P> handlerInvalidField)-
Using full object
ruleFor(entity -> entity) .must(stringEquals(Entity::getField1, Entity::getField2)) .when(not(stringEmptyOrNull(Entity::getField1)).and(not(stringEmptyOrNull(Entity::getField2)))) .handlerInvalidField(new HandlerInvalidField<Entity>() { @Override public Collection<Error> handle(final Entity instance) { final Erro field1 = Error.create("field1", "field1 must be equal field2", "404", instance.getField1()); final Erro field2 = Error.create("field2", "field2 must be equal field1", "404", instance.getField2()); return Arrays.asList(field1, field2); } });
The result when was error produce two custom errors.
-
Using property
ruleFor(Entity::getCollection) .must(hasSize(1)) .when(not(empty())) .handlerInvalidField(new HandlerInvalidField<Collection<String>>() { @Override public Collection<Error> handle(final Collection<String> instance) { return Collections.singletonList(Error.create("collection", "collection must be size 1", "404", instance.getCollection())); } });
The result when was error produce one custom error.
-
Using property and access main object
ruleFor(Entity::getCollection) .must(hasSize(1)) .when(not(empty())) .handlerInvalidField(new HandlerInvalidField<Collection<String>>() { @Override public Collection<Error> handle(final Object instance, final Collection<String> value) { final Entity entity = Entity.class.cast(instance); return Collections.singletonList(Error.create("entity", "entity property collection must be size 1", "404", entity)); } });
The result when was error produce one custom error.
must(final Predicate<P> predicate)when(final Predicate<P> when)whenever(final Predicate<P> predicate)withAttempedValue(final Function<T, Object> attemptedValue)withAttempedValue(final Object attemptedValue)withCode(final Function<T, String> code)withCode(final String code)withFieldName(final Function<T, String> fieldName)withFieldName(final String fieldName)withMessage(final Function<T, String> message)withMessage(final String message)withValidator(final Validator<P> validator)