11

I'm using javax.validation to validate some bean fields' values.

This is what I use normally:

public class Market {

    @NotNull
    @Size(max=4)
    private String marketCode;

    @Digits(integer=4, fraction=0)
    private Integer stalls;

    // getters/setters
}

This will make sure that every Market instance has a market code with a maximum length of 4 characters and a number of stall with a maximum of 4 integer digits and 0 decimal digits.

Now, I use this bean to load/store data from/to DB.

In the DB I have table Markets defined like this:

CREATE TABLE MARKETS (
    MARKET_CODE VARCHAR2(4 BYTE) NOT NULL,
    STALLS NUMBER(4,0)
)

As you can see, I have MARKET_CODE which can be at most 4 bytes long. The @Size annotation will check if the string is at most 4 characters long, which is wrong.

So, the question is: is there an annotation like @Size that will check for the string bytes instead of the characters?

6
  • 1
    4 bytes in what encoding? You'll probably need your own validator which effectively checks "string".getBytes(Charsets.UTF_8).length Commented Oct 13, 2014 at 9:56
  • Are the characters in the marketCode guaranteed to fit in one byte each? Commented Oct 13, 2014 at 9:58
  • @ptomli They seem to be WE8MSWIN1252. The problem is that I'm on a testing environment, I don't know if the DB on the production environment will have the same encoding... Commented Oct 13, 2014 at 10:01
  • @Simon No. If it was guaranteed, I wouldn't be searching for an annotations that checks bytes :) Commented Oct 13, 2014 at 10:02
  • 1
    @BackSlash then your problem is underspecified. A character is not a byte, and without knowing how your application is going to convert characters into bytes, known as encoding, then you simply cannot tell how many bytes any given string will occupy. Either your application specifies which encoding, as part of a custom annotation, or your application specification documents what will be used. You need to know the encoding to be able to specify the number of bytes, which you're trying to do in static code. This puzzle is missing some pieces Commented Oct 13, 2014 at 10:06

1 Answer 1

11

Check the Hibernate Validator documentation on Creating custom constraints.

Your validator will need to encode the String into a byte[], using some default or specified Charset. I imagine you might well use UTF-8.

Maybe something like this, which uses a hard coded UTF-8 encoding and assumes a suitable annotation, as outlined in the Hibernate documentation linked.

public class MaxByteLengthValidator implements ConstraintValidator<MaxByteLength, String> {
    private int max;
    public void initialize(MaxByteLength constraintAnnotation) {
        this.max = constraintAnnotation.value();
    }
    public boolean isValid(String object, ConstraintValidatorContext constraintContext) {
        return object == null || object.getBytes(Charsets.UTF_8).length <= this.max;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.