You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a validation issue that I think is not that uncommon, but yet I was not able to find any solutions for that.
I have something like the following class:
public class Contract { private boolean cancelled; @Valid private Partnership partnership; }
The partnership contains multiple persons, which also contain several other classes that all have different @Valid-annotations.
Now, if I validate such a contract, the partnership will always be validated. But I only need to validate its partnership, if the contract itself ist not cancelled. It would be great, if @Valid would offer a prerequisite property like that
`
public class Contract {
private boolean cancelled; @Valid(prerequisite = "shouldValidate")
private Partnership partnership;
public boolean shouldValidate() {
return !cancelled;
}
}
`
Since there is no such possibility, I wrote a custom ConstraintValidator.
`
@ApplicationScoped
public class ValidateIfNotCancelledValidator implements ConstraintValidator<ValidateIfNotCancelled, Contract> {
@Override
public boolean isValid(Contract contract, ConstraintValidatorContext context) {
if (contract == null || contract.isCancelled()) {
return true;
}
// validate the partnership here
}
}
`
I do not want to write the validation for the partnership manually. So I need the validator here and also the validation group that the current validation runs with. I wrote some code that actually works, but seems very ugly to me. I guess it is not recommended to do this.
` @dependent
public class ValidateIfNotCancelledValidator implements ConstraintValidator<ValidateIfNotCancelled, Contract> {
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi everybody
I have a validation issue that I think is not that uncommon, but yet I was not able to find any solutions for that.
I have something like the following class:
public class Contract { private boolean cancelled; @Valid private Partnership partnership; }
The partnership contains multiple persons, which also contain several other classes that all have different @Valid-annotations.
Now, if I validate such a contract, the partnership will always be validated. But I only need to validate its partnership, if the contract itself ist not cancelled. It would be great, if @Valid would offer a prerequisite property like that
`
public class Contract {
private boolean cancelled;
@Valid(prerequisite = "shouldValidate")
private Partnership partnership;
}
`
Since there is no such possibility, I wrote a custom ConstraintValidator.
`
@ApplicationScoped
public class ValidateIfNotCancelledValidator implements ConstraintValidator<ValidateIfNotCancelled, Contract> {
}
`
I do not want to write the validation for the partnership manually. So I need the validator here and also the validation group that the current validation runs with. I wrote some code that actually works, but seems very ugly to me. I guess it is not recommended to do this.
`
@dependent
public class ValidateIfNotCancelledValidator implements ConstraintValidator<ValidateIfNotCancelled, Contract> {
}
`
Now I have to annotate Contract several times for each validation-group I want. That seems quite ugly.
@ValidateIfNotCancelled(groups = PreValidation.class) @ValidateIfNotCancelled(groups = ForSave.class) @ValidateIfNotCancelled(groups = Complete.class) public class Contract { // ... }
And I access the validator within a validation. This seems wrong to me, too. But it's the only way it worked somehow.
I found a discussion with the same issue on https://discourse.hibernate.org/t/how-can-i-retrieve-current-validation-contexts-groups-in-a-validator/414. It has been 7 years now without any solutions yet. In the community here, I haven't found anything like that.
Is there a better way to do this? I guess, customizing the Validator as described on https://quarkus.io/guides/validation#hibernate-validator-extension-and-cdi does not help.
Thank you so much for your support!
Beta Was this translation helpful? Give feedback.
All reactions