implier icon indicating copy to clipboard operation
implier copied to clipboard

Data validation

Open y9vad9 opened this issue 2 years ago • 3 comments

The most complex thing here is how to design validators. I see three ways:

  • (Not convenient for me, but still the way) design it with notations and annotations:
    // object here is notation, we assuming that user will
    // create object here, but not class. Anyway this will
    // be checked at compile time
    object NameValidator : Validator<String> {
        override fun validate(value: String): Boolean = ...
    }
    
    @Validate(NameValidator::class)
    val name: String = ""
    
  • (Not convenient for me, but still the way) design it with function name notations:
    var name: String = ...
    fun _checkName_(): Boolean = ...
    
    Transformed to →
    private var _name: String
    var name: String get() = _name set() = if(/* code from _checkName_() */) ... else ...
    
  • (Now I'm looking forward this) Wrap it with a type:
    val name: Validate<String> = Validate(default = ...) { name -> require(name.length in 1..30) { "Provide a correct name" } }
    
    Transformed to →
    var _name: String = default
    var name get() = _name set() = if(/* code from name.validate() */) ... else ...
    

Originally posted by @y9san9 in https://github.com/y9vad9/implier/issues/1#issuecomment-984492558

y9vad9 avatar Dec 02 '21 21:12 y9vad9