konform-kt / konform

Portable validations for Kotlin
https://www.konform.io
MIT License
652 stars 39 forks source link

Add support for validation oneOf #9

Open raderio opened 5 years ago

raderio commented 5 years ago

For example a login field can be a phone or and email.

Frontrider commented 1 year ago

I'd see the dsl as this:

oneOf("only one of arrayOne or arrayTwo can be present!",{
        MyData::arrayOne{
                minItems(1)
        }
        MyData::arrayTwo{
                maxItems(0)
        }
},{
        MyData::arrayOne{
                maxItems(0)
        }
        MyData::arrayTwo{
                minItems(1)
        }
})

This creates a validation error, if not only one of the rules passed. This specific validation ensures that only one of the specified arrays have any items. I'd set both of the default values to an empty array, and after I parsed it from say json only the array that was present in the input will contain any items.

anyOf("custom message",{
//first validation
},{
//second validation
})

This throws if none of the validations passed.

This may also be just enough for: