Skip to content

Commit

Permalink
Update docs to show inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-wise committed Apr 28, 2022
1 parent 838b164 commit 2801950
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ You can enforce a subset of constraints using https://beanvalidation.org/2.0/spe

snippet::io.micronaut.docs.datavalidation.groups.FinalValidation[tags="clazz", indent=0]

<1> Define a custom validation group.
<1> Define a custom validation group. This one extends `Default` so any validations done with this group will include constraints in the `Default` group.

snippet::io.micronaut.docs.datavalidation.groups.Email[tags="clazz", indent=0]

Expand All @@ -15,8 +15,8 @@ snippet::io.micronaut.docs.datavalidation.groups.EmailController[tags="imports,c

<1> Annotating with api:validation.Validated[] without specifying groups means that the `Default` group will be active. Since this is defined on the class, it will apply to all methods.
<2> Constraints in the `Default` validation group will be enforced, inheriting from the class. The effect is that `@NotBlank` on `email.recipient` will not be enforced when this method is called.
<3> Specifying `groups` means that these validation groups will be enforced when this method is called.
<4> Constraints in the `Default` and `FinalValidation` validation groups will be enforced. The effect is that both `@NotBlank` constraints in `email` will be enforced when this method is called.
<3> Specifying `groups` means that these validation groups will be enforced when this method is called. Note that `FinalValidation` extends `Default` so constraints from both groups will be enforced.
<4> Constraints in the `Default` and `FinalValidation` validation groups will be enforced, since `FinalValidation` extends `Default`. The effect is that both `@NotBlank` constraints in `email` will be enforced when this method is called.

Validation of POJOs using the default validation group is shown in the following test:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import io.micronaut.http.annotation.Post
import io.micronaut.validation.Validated

import javax.validation.Valid
import javax.validation.groups.Default
//end::imports[]

@Requires(property = "spec.name", value = "datavalidationgroups")
Expand All @@ -39,7 +38,7 @@ class EmailController {
}

@Post("/send")
@Validated(groups = [Default, FinalValidation]) // <3>
@Validated(groups = [FinalValidation]) // <3>
HttpResponse send(@Body @Valid Email email) { // <4>
HttpResponse.ok(msg: "OK")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package io.micronaut.docs.datavalidation.groups;

//tag::clazz[]

interface FinalValidation {} // <1>
import javax.validation.groups.Default

interface FinalValidation extends Default {} // <1>

//end::clazz[]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Post
import io.micronaut.validation.Validated
import javax.validation.Valid
import javax.validation.groups.Default
//end::imports[]

@Requires(property = "spec.name", value = "datavalidationgroups")
Expand All @@ -38,7 +37,7 @@ open class EmailController {
}

@Post("/send")
@Validated(groups = [Default::class, FinalValidation::class]) // <3>
@Validated(groups = FinalValidation::class) // <3>
open fun send(@Body @Valid email: Email): HttpResponse<*> { // <4>
return HttpResponse.ok(mapOf("msg" to "OK"))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package io.micronaut.docs.datavalidation.groups

//tag::clazz[]

interface FinalValidation {} // <1>
import javax.validation.groups.Default

interface FinalValidation : Default() {} // <1>

//end::clazz[]
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import io.micronaut.validation.Validated;

import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.Collections;
//end::imports[]

Expand All @@ -40,7 +39,7 @@ public HttpResponse createDraft(@Body @Valid Email email) { // <2>
}

@Post("/send")
@Validated(groups = {Default.class, FinalValidation.class}) // <3>
@Validated(groups = FinalValidation.class) // <3>
public HttpResponse send(@Body @Valid Email email) { // <4>
return HttpResponse.ok(Collections.singletonMap("msg", "OK"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

//tag::clazz[]

public interface FinalValidation {} // <1>
import javax.validation.groups.Default;

public interface FinalValidation extends Default {} // <1>

//end::clazz[]

0 comments on commit 2801950

Please sign in to comment.