Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to validate related properties #9

Closed
davidmoshal opened this issue Feb 1, 2020 · 4 comments
Closed

How to validate related properties #9

davidmoshal opened this issue Feb 1, 2020 · 4 comments

Comments

@davidmoshal
Copy link

davidmoshal commented Feb 1, 2020

Promising library, but examples are only given for the simplest case, namely validating individual properties.
Question: can this library be used to validate related properties.

Take the following example:

data class Foo(val bar:String, val baz:String)

How would one determine if bar and baz are both non-zero numbers, and
also that bar x baz is less than, say, 100 ?

Ideally one would be able to convert the individual properties into their typed representations, and then use them subsequently.

For example something like this (pseudo code):

val spec = validationSpec {
       constraints<Foo> {
         val bar:Long = property(Foo::bar) {
             min(0)
         }

         val baz:Long = property(Foo::bar) {
             min(0)
         }
  
        validate("total", {bar * baz} ){
             min(total, 0)
        }
     }
 }

ie: if bar and baz are invalid, then the subsequent calculation won't be run, otherwise it runs, with the values guaranteed to be valid.

@byteground
Copy link

+1 @davidmoshal

@clojj
Copy link
Contributor

clojj commented Apr 16, 2021

+1
this seems like a somewhat related workaround to me: #4 (comment)

@clojj
Copy link
Contributor

clojj commented Apr 20, 2021

Promising library, but examples are only given for the simplest case, namely validating individual properties.

Question: can this library be used to validate related properties.

Take the following example:

data class Foo(val bar:String, val baz:String)

How would one determine if bar and baz are both non-zero numbers, and

also that bar x baz is less than, say, 100 ?

Ideally one would be able to convert the individual properties into their typed representations, and then use them subsequently.

For example something like this (pseudo code):

val spec = validationSpec {

       constraints<Foo> {

         val bar:Long = property(Foo::bar) {

             min(0)

         }



         val baz:Long = property(Foo::bar) {

             min(0)

         }

  

        validate("total", {bar * baz} ){

             min(total, 0)

        }

     }

 }

ie: if bar and baz are invalid, then the subsequent calculation won't be run, otherwise it runs, with the values guaranteed to be valid.

Hi, Kalidation is nice because it returns Validated, a powerful arrow type.
I think for any such case, we can just use Validated's combinators like this https://arrow-kt.io/docs/apidocs/arrow-core/arrow.core/-validated/#improving-the-validation

@clojj
Copy link
Contributor

clojj commented Apr 20, 2021

So just to give some silly example, in which a 3rd validation only checks, when the 2 validations it depends upon are valid cases:

    val specFoo = validationSpec {
        constraints<Foo> {
            property(Foo::foo) {
                notBlank()
                inValues("GREEN", "WHITE")
            }
        }
    }

    val specBar = validationSpec {
        constraints<Bar> {
            property(Bar::bar) {
                notBlank()
                inValues("CYAN", "BLACK")
            }
        }
    }

    val foo = Foo("GREEN")
    val bar = Bar("BLAC")

    val validFoo: Validated<Nel<String>, Foo> = specFoo.validateNel(foo)
    val validBar: Validated<Nel<String>, Bar> = specBar.validateNel(bar)

    validFoo.zip(validBar) { a, b ->
        if (a.foo == "GREEN" && b.bar == "BLACK")
            "must be green and cyan".nel().invalid()
        else Pair(a, b).valid()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants