konform icon indicating copy to clipboard operation
konform copied to clipboard

Add support for validation oneOf

Open raderio opened this issue 5 years ago • 1 comments

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

raderio avatar Apr 12 '19 11:04 raderio

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:

  • #6 , an openapi generator can generate the relationships as constraints. As a bonus it provides that generator instant support for oneOf,anyOf as you can use validation results to check which type it matches. (allOf is supported by the class structure). You do need to use nullable fields, but it is a small price for this feature.
  • #29 you can use the individual validation statements to set up constraints between fields, as shown in the example.
  • #20 same as #29

Frontrider avatar Jul 18 '23 07:07 Frontrider