Validator: Validating unexported fields

Created on 14 Nov 2018  路  6Comments  路  Source: go-playground/validator

Package version eg. v8, v9:

v8 and v9

Question:

I was wondering if it's possible to validate unexported fields. But unexported fields are skipped here.
https://github.com/go-playground/validator/blob/v9/cache.go#L131

Is there any reason for it?

question

Most helpful comment

Should this issue be reopened ?
It could be handy to have a validation on unexported fields when you want your struct to be immutable.

All 6 comments

@Jwata A package can only view the unexported fields of types within its own package and so they must be ignored or there will be an exception; this is true of all packages using reflect even the son package.

OK, so they must be exported to do validate using reflection.

As far as i know reflect can read an unexported field, but write can't. Check the following link: https://yourbasic.org/golang/access-private-field-reflection/. Also you can use reflect2 package as json-iterator/extra do: https://godoc.org/github.com/json-iterator/go/extra#SupportPrivateFields.

Is there's a way that you considirer validate unexported fields?

Many thanks un advance.

@fuintis thanks for the link, I could have sworn this was not possible but I鈥檒l take another look! love to support unexported fields if I can

Should this issue be reopened ?
It could be handy to have a validation on unexported fields when you want your struct to be immutable.

Same here. This issue completely breaks DDD entity pattern.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thaonx picture thaonx  路  3Comments

0505gonzalez picture 0505gonzalez  路  3Comments

romangithub1024 picture romangithub1024  路  5Comments

alexyans picture alexyans  路  3Comments

anam-digicom picture anam-digicom  路  4Comments