Skip to content

Add Jakarta Validation Support to Generated GraphQL Input Classes #1315

@yidafu

Description

@yidafu

Is your feature request related to a problem? Please describe.

On the Kotlin JVM platform, without the @Valid annotation, we can only perform validation manually.

@Controller  
class ClientAddressMutationResolver() {  

  @MutationMapping  
  fun createAddress(  
    @Argument input: AddressInput,  
  ): Address {  
    if (input.detailAddress.length < 5 || input.detailAddress.length > 200) {  
       throw Exception("...")  
    }  
  }  
}

AddressInput is a generated class, such as:

data class AddressInput(  
    val detailAddress: kotlin.String,  
)

Describe the solution you'd like
If AddressInput could be generated with the jakarta.validation.constraints.Size annotation, validation could be automated.

@MutationMapping  
  fun createAddress(  
-    @Argument input: AddressInput,  
+    @Argument @Valid input: AddressInput,  
  ): Address {  
-    if (input.detailAddress.length < 5 || input.detailAddress.length > 200) {  
-       throw Exception("...")  
-    }  
  }

Generate AddressInputas shown below:

data class AddressInput(
+    @field:Size(min = 5, max = 200, message = "Detailed address length must be 5-200 characters")
      val detailAddress: kotlin.String,
}

And include a custom directive in the GraphQL schema definition:

directive @size(min: Int, max: Int, message: String) on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION  

input AddressInput {  
  detailAddress: String!  
    @size(min: 5, max: 200, message: "Detailed address length must be 5-200 characters")  
}

Describe alternatives you've considered

Additional context

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions