Skip to content

Feature: automatic validation of enum values #657

@corani

Description

@corani

Based on #656 a custom unmarshal function can be defined on enum types to validate that the provided enum value is actually allowed by the spec. The generated code could look as follows:

func (v *Status) UnmarshalJSON(bs []byte) error {
    var s string

    if err := json.Unmarshal(bs, &s); err != nil {
        return err
    }

    switch Status(s) {
    case StatusSuccess, StatusFailed:
        *v = Status(s)

        return nil
    default:
        return fmt.Errorf("invalid enum value %q", s)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions