Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for groups used for validation annotations is missing #1374

Closed
Zeratoxx opened this issue Dec 10, 2021 · 1 comment
Closed

Support for groups used for validation annotations is missing #1374

Zeratoxx opened this issue Dec 10, 2021 · 1 comment
Labels
question Further information is requested

Comments

@Zeratoxx
Copy link

Zeratoxx commented Dec 10, 2021

I am using validator groups for complex objects.

Given the following DTOs / Classes:

AdvertisementDTO

public class AdvertisementDTO {
    public enum Type {
        OFFER {
            public String toString() {
                return "OFFER";
            }
        },
        REQUEST {
            public String toString() {
                return "REQUEST";
            }
        }
    }

    @NotNull( groups = {ValidatorGroup.CreateSecondLevel.class, javax.validation.groups.Default.class} )
    private int id;

    @NotNull(
            groups = {Default.class, ValidatorGroup.Create.class},
            message = "Advertisement type cannot be null")
    private Type type;

    @Valid
    @ConvertGroup(from = ValidatorGroup.Create.class, to = ValidatorGroup.CreateSecondLevel.class)
    @NotNull(
            groups = {
                ValidatorGroup.CreateSecondLevel.class,
                Default.class,
                ValidatorGroup.Create.class
            },
            message = "Advertisement category cannot be null")
    private CategoryDTO category;

    @Valid
    @ConvertGroup(from = ValidatorGroup.Create.class, to = ValidatorGroup.CreateSecondLevel.class)
    @NotNull(
            groups = {
                ValidatorGroup.CreateSecondLevel.class,
                Default.class,
                ValidatorGroup.Create.class
            },
            message = "Advertisement user cannot be null")
    private UserDTO user;

    @NotBlank(
            groups = {Default.class, ValidatorGroup.Create.class},
            message = "Advertisement title is mandatory")
    private String title;

    @NotBlank(
            groups = {Default.class, ValidatorGroup.Create.class},
            message = "Advertisement title is mandatory")
    private String description;

    @Min(
            groups = {ValidatorGroup.Create.class, Default.class},
            value = 0)
    private Integer price;

    private String location;

    private Date created;
}

UserDTO

public class UserDTO {

    @NotNull( groups = {ValidatorGroup.CreateSecondLevel.class, javax.validation.groups.Default.class} )
    private int id;

    @Email(
            groups = ValidatorGroup.Create.class,
            regexp = "(\\w)+@*.*",
            flags = {Pattern.Flag.CASE_INSENSITIVE, Pattern.Flag.UNICODE_CASE})
    @NotBlank(groups = ValidatorGroup.Create.class, message = "User email is mandatory")
    private String email;

    @NotBlank(groups = ValidatorGroup.Create.class, message = "User password is mandatory")
    @Length(groups = ValidatorGroup.Create.class, min = 6)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;

    @NotBlank(groups = ValidatorGroup.Create.class, message = "User firstName is mandatory")
    @Length(groups = ValidatorGroup.Create.class, max = 255)
    private String firstName;

    @NotBlank(groups = ValidatorGroup.Create.class, message = "User lastName is mandatory")
    @Length(groups = ValidatorGroup.Create.class, max = 255)
    private String lastName;

    private String phone;
    private String location;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Date created;
}

CategoryDTO

public class CategoryDTO {

    @NotNull( groups = {ValidatorGroup.CreateSecondLevel.class, javax.validation.groups.Default.class} )
    private int id;

    @NotBlank(
            groups = {ValidatorGroup.Create.class},
            message = "Category name cannot be null or empty")
    private String name;

    private CategoryDTO parentCategory;

    public static CategoryDTO toCategoryDTO(Category entity) {
        CategoryMapper mapper = Mappers.getMapper(CategoryMapper.class);
        return mapper.toCategoryDTO(entity);
    }

    public static Category toCategoryEntity(CategoryDTO dto) {
        CategoryMapper mapper = Mappers.getMapper(CategoryMapper.class);
        return mapper.toCategoryEntity(dto);
    }
}

ValidatorGroup

public interface ValidatorGroup {
    interface Create {}

    interface CreateSecondLevel {}
}

JSON Payloads

POST newAdvertisement

In POST newAdvertisement, there needs to be a JSON payload like that:

{
"type":"OFFER",
"category": {
"id":5
},
"user": {
"id":4711
},
"title":"4 room apartment for rent",
"description":"It is a apartment with 4 rooms near a lake.",
"price":400,
"location":"7th Ave, NYC"
}

As you can see, for category and user, only the id is needed but the id of the Advertisement itself is missing (it gets generated by JPA).

POST newUser

On the other hand, if you want to create a new User, the JSON payload looks like that:

{
"email":"name@mail.example",
"password":"secret",
"firstName":"David",
"lastName":"Herold",
"phone":"069-123456",
"location":"NYC"
}

All informations except the id.

Conclusion

In the current release, the groups are ignored.

@Zeratoxx Zeratoxx changed the title Support for groups used for validation annotations Support for groups used for validation annotations is missing Dec 10, 2021
@bnasslahsen
Copy link
Contributor

@Zeratoxx,

There is no plans for this support.
Feel free to propose a PR if you feel it useful for the community, and we will have a look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants