Skip to content

Combine required_if with a validation rule #162

Description

@OscarVanL

Hi,

I have a field that is only present if another field has a certain value.

type A struct {
    Type string `validate:"in:B,C"`
    B
    C
}

type B struct {
    ID string `validate:"required_if:A.Type,B|uuid4"
}

So B's ID must be a UUID, but only if A's type is B.

So this is allowed:

A{
    Type: "B", 
    B: {
        ID: "94e48bd3-e990-405e-bd10-304e767cd3fd"
    },
}

But this is not allowed:

A{
    Type: "C", 
    B: {
        ID: "94e48bd3-e990-405e-bd10-304e767cd3fd"
    },
}

And this is not allowed:

A{
    Type: "B", 
    B: {
        ID: ""
    },
}

Do you know how I would achieve this with your library? Thank you!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions