title: Validation Data Class Parameters Kotlin
body: <p>If I am modeling my value objects using Kotlin data classes what is the best way to handle validation. Seems like the init block is the only logical place since it executes after the primary constructor.</p>
  
  <pre><code>data class EmailAddress(val address: String) {
  
      init {
          if (address.isEmpty() || !address.matches(Regex("^[a-zA-Z0-9]+@[a-zA-Z0-9]+(.[a-zA-Z]{2,})$"))) {
              throw IllegalArgumentException("${address} is not a valid email address")
          }
      }
  }
  </code></pre>
  
  <p>Using JSR-303 Example</p>
  
  <p>The downside to this is it requires load time weaving</p>
  
  <pre><code>@Configurable
  data class EmailAddress(@Email val address: String) {
  
      @Autowired
      lateinit var validator: Validator
  
      init {
          validator.validate(this)
      }
  }
  </code></pre>