What is point of constraint validation @Null?
Asked Answered
K

2

29

I was checking list of available constraints in javax.validation package and I noticed that there is an annotation @Null which force the field to be null.

I do not understand what is point of adding it to my field if I already know it should be null.

For example look at this class:

public class MyClass{

    @NotNull
    private String myString1;

    @Null
    private String myString2;

    // getter setters...
}

@NotNull completely makes sense. I do not expect myString1 to be null. but @Null makes myString2 useless. What is point of having a field which should always be null.

Kiefer answered 21/6, 2017 at 18:9 Comment(6)
It should not always be null. It should be null at the time when the object is being validated. Imagine a field that is part of the class, but must not be submitted by the user because it's computed and added later to the object, for example.Witty
also is useful for grouping validation constraints and defining the result of methods.Concupiscence
@JBNizet, I understand your use-case but I do not think that field should be in the user input class because user has nothing to do with it. I should convert user input class to a different class if I want to add extra fields to it. If I keep using the same class, I cannot validate it anymore which is against purpose of these constraint validations.Kiefer
I'm not saying it's a good idea to do that. I'm just saying that it happens, and that Null is useful in that case.Witty
@JBNizet is right. It is simple constraint for different situation. For example entity with ID field that should be null before saving into DB. Or you have object that should be saved into your system but some field, for example some paymentID should be retrieved and updated by 3rd party service, in that case when you are going to save this object you validate this field @Null, and then some other thread (service) will read this object and update that field additionally. So everything depends on your use cases.Eyeful
@JBNizet Please add your comment as an answer and I will accept it. It looks like there is no other use-case except than what you mentioned.Kiefer
T
22

You may want to use @Null in combination with "Validation Group" to validate the null constraint only in certain cases.

Good explanation and example on validation groups provided by Hibernate

You will define validation group as simple interface

public interface FirstSave {}

then use it in constraint

public class MyClass {

    @Null(groups = FirstSave.class)
    private LocalDate lastUpdate;
}

then if lastUpdate is not null, calling validator.validate(myClassInstance) will not produce constraint violation (Default group was used), but calling validator.validate(myClassInstance, FirstSave.class) will.

You also have the possibility to provide your own implementation on how to use the annotation, i.e. I've seen validation method being annotated with @Null where null returned by the method meant that everything is alright. In the background there was probably custom implementation on what to do if annotated method returned not null result, but I didn't go deep into the code...

Thaumatrope answered 1/9, 2018 at 16:1 Comment(0)
S
5

@Null is a very important annotation. It is not useless. Let me show a common usecase. Say, there is DAO (entity) object with Id auto-generated.

If you use separate classes for DTO and DAO, @GetMapping returns list of DTOs without issues. On the other hand, @PostMapping for adding a new element requires that input DTO must not contain Id field and even if present it must be null or undefined. Such an input when converted to and from DTO object, expects Id must be blank. For this, @Null is the only choice

@PutMapping expects id must not be blank so @NotNull is required for id field when we expect update happens to a single object.

@DeleteMapping requires just integer Id , only if we want to delete an object with known Id.

There are alternative complicated cases, which usually are not handled but makes sense

@GetMapping can be used for any provided field but if any of the field other than Id is provided then Id must be blank. If Id is provided then all others must be blank.

There is also a complicated @PutMapping requirement, where in partial information is provided for update and expected remaining fields to be older values. Here non-null fields are updated.

Another annotation @DeleteMapping is used to delete or remove. If it is computed to blank, it can be implented with @Null constraint.

Usual CRUD operations are too simple but not a suitable usecase for practical expectations.

All these mix of requirements can be listed into groups. And constraints can be provided with groups attribute with separate Marked interface @Validated can be applied as per requirement.

Shaver answered 29/5, 2021 at 8:41 Comment(1)
But the confusion comes from: @Null means a field should always be null (as opposed to @Nullable which means it can be null sometimes). So if a field should always be null, and ever being not null is a constraint violation: why have the field at all?Corporal

© 2022 - 2024 — McMap. All rights reserved.