Search code examples
validationspring-data-elasticsearch

NotNull constraints aren't being enforced when not set


NotNull constraints aren't being enforced, when the attribute is not set. Is there anything which needs to be additionally set to enforce null values

    import javax.validation.constraints.NotNull;

    @NotNull(message = "PaymentType is required")
    @JsonProperty(PAYMENT_TYPE)
    @Field(type = FieldType.Keyword, name = PAYMENT_TYPE)
    private PaymentType paymentType;

Solution

  • As you are having a @JsonProperty(PAYMENT_TYPE) annotation on that property I assume you are using this entity not only to store data in Elasticsearch, but also are sending it to and receiving from some client application? Then the validation should happen when you get this data in. Spring Data Elasticsearch does not support validation on properties.