Validator: Go 1.8 context supports

Created on 23 Jul 2017  路  3Comments  路  Source: go-playground/validator

Package version eg. v8, v9:

v9

Issue, Question or Enhancement:

Enhancement: I would like to have go 1.8 context support. In my project, I need a custom validation tag which checks value against with DB. In that case, a context.Context would be required for DB connection.
For example, this validator will check userId does exist in DB

validate.RegisterValidation("userId", func(ctx context.Context, fl validator.FieldLevel) bool {
        row, err := sql. QueryContext(ctx, "SELECT * FROM users WHERE users.id = ? LIMIT 0, 1",  fl.Field().String())
                if err != nil {
                       panic(err)
                }
                return row != nil
    })

Code sample, to showcase or reproduce:

I will be great to have

StructCtx(ctx context.Context, s interface{}) (err error)

And

type FuncCtx func(ctx context.Context, fl FieldLevel) bool

Most helpful comment

Thanks @thaonx

I really like this idea! It would actually allow me to solve a few things I hadn't been able to reconcile until now :) like a better way to do translations.

Give me some time to look over the PR I may want to incorporate into v10 and make a bunch of other changes at the same time but from my initial quick look I'll probably be able to merge into v9

All 3 comments

Thanks @thaonx

I really like this idea! It would actually allow me to solve a few things I hadn't been able to reconcile until now :) like a better way to do translations.

Give me some time to look over the PR I may want to incorporate into v10 and make a bunch of other changes at the same time but from my initial quick look I'll probably be able to merge into v9

just letting you know I haven't forgotten about this, just super busy with work, moving and other projects requesting changes.

I promise I will get to it :)

Hey @thaonx this is now added in release v9.5.0

Thanks! lots of great things on the horizon for v10!

Was this page helpful?
0 / 5 - 0 ratings