govalidator icon indicating copy to clipboard operation
govalidator copied to clipboard

How can I cater for the case of conditional "required" ?

Open vcGF opened this issue 6 years ago • 3 comments

Hi.

I have a use case, for which I don't know how to set the flags from the validator.

Those are the required fields

{ "title": "This is the title", "caption": "This is the caption" }

title: []string{"required"}, caption: []string{"required"}

Now there may be a nested structure as such. The "attached_media" key is NOT required in the payload, BUT IF it is provided, then only some fields are required.

{ "title": "This is the title", "caption": "This is the caption", "attached_media": { "Yo": "Plop" } }

So this is my attempt at the settings, which fails, title: []string{"required"}, caption: []string{"required"}` Yo: []string{"required"}

Bottom line, how can I set things u so that some nested keys are required ONLY if the parent key is there ?

vcGF avatar Sep 12 '18 07:09 vcGF

@vcGF Unfortunately it doesn't have the feature right now. Hope next major release must have http.Request object in rules and then you can make your own rules to this particular problem.

thedevsaddam avatar Sep 13 '18 04:09 thedevsaddam

Hi . Just checking if there was any update on this feature :)

vcGF avatar Oct 21 '18 07:10 vcGF

Need a PR to add this feature.

thedevsaddam avatar Oct 26 '18 15:10 thedevsaddam