A
A
arturgspb2014-12-31 14:02:19
Java
arturgspb, 2014-12-31 14:02:19

What is the best way to do hibernate validation with error codes?

Hello!
I have a SAAS project, I decided to use hibernate validation (4.3.0.Final) on it.
The problem is that my SAAS essentially provides only an API and clients need to understand what kind of error came in response to their request. Throwing text in response, such as "% field% must be not null" or "javax.validation.constraints.NotNull.message" I think is wrong, because in the first option, it's just impossible to use, in the second it's somehow not cool, since highly implementation dependent. In general, only some constants or enumerations will allow the client to understand exactly what came in response.
I thought that it is possible to implement something like what Google has in one of the systems ( https://developers.google.com/adwords/api/docs/ref...
In this regard, I thought somehow to use enums instead of message.
But something like:
@NotNull(message = MaterialType.IMAGE)
Naturally, java does not allow, since the standard NotNull assumes a String in message.
Then I thought about a class with static properties:

public static class Constants {
  public static final String IMAGE = "IMAGE";
}

but that seemed like a bad idea to me.
There are several solutions to the problem in mind:
1) Own validator for all models
2) Sophistication with storage "public static final String IMAGE = "IMAGE";"
3) My own implementations of NotNull, etc. standard, where you can specify the same error constant.
None of the options impresses with the implementation, while writing my own, as it were, I practically don’t consider it, because at first I want to understand what is different, well, in general no way.
The result should be something like - a list of errors (structures):
* fieldPath - the field that caused the error
* trigger - what exactly caused the error
* errorString - the text of the error for clarity
* type - the same enum type (NOT_NULL, MIN, etc.)
Please advise how to solve the problem, I'm sure that it, so to speak, is not new.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanKiLL, 2014-12-31
@arturgspb

NotNull not only assumes a String, but also that it will be a constant, or rather, any annotation that expects a String assumes so.

public enum MessageType {
    NOTNULL("not null!!!"), MINVALUE("min value must be ...");

    private final String message;

    private MessageType(final String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    @Override
    public String toString() {
        return this.message;
    }
}

And call later
@NotNull(message = MessageType.NOTNULL.getMessage())
private String userName;

Such a feint with the ears will not work. Even MessageType.NOTNULL.name()the method that returns the name of Enum'a will not pass.
public class ValidationConstants {

    private ValidationConstants() {} //Приватный конструктор, что бы нельзя было сделать new
  
    public static final String NOT_NULL = "Not nuull...";
    public static final String MIN_VALUE = "Min value must be...";
}

This is quite a ride
@NotNull(message = ValidationConstants.NOT_NULL)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question