GoogleCloudPlatform / go-endpoints

Cloud Endpoints for Go
https://go-endpoints.appspot.com
Apache License 2.0
255 stars 56 forks source link

req not working for nested structs #107

Open enj opened 9 years ago

enj commented 9 years ago

Does "req" not work with nested structs? The following does not throw an error when {"a":"s"} is sent as a Thing request value (I would think t and t.value would be required).

type Tester struct {
    Value string `json:"value" endpoints:"req"`
}

type Thing struct {
    A string `json:"a" endpoints:"req"`
    T Tester `json:"t" endpoints:"req"`
}
aneshas commented 8 years ago

I can confirm that this does not work even if you use pointers, eg:

type Tester struct {
    Value string `json:"value" endpoints:"req"`
}

type Thing struct {
    A string `json:"a" endpoints:"req"`
    T *Tester `json:"t" endpoints:"req"`
}

It would be great if we could get any feedback on this.

aneshas commented 8 years ago

@campoy Ok, I did a fork and started implementing nested struct validation, it seemed pretty straight forward, and it basicaally is, but how do we distinguish between our own struct types and predefined ones we do not wish to validate.

Example:

type Tester struct {
    Value string `json:"value" endpoints:"req"`
}

type Thing struct {
    A string `json:"a" endpoints:"req"`
    Timestamp time.Time `json:"timestamp" endpoints:"req"
    T Tester `json:"t" endpoints:"req"`
}

time.Time is also a struct but how and we wouldn't wish to validate it, what is your opinion on how do we distinguish it from for example Tester struct. (add another tag maybe ???)

aneshas commented 8 years ago

PS: I'm talking about validating the contents (fields) of those struct fields not the fields themselves. We want to validate time.Time field but not it's members.

aneshas commented 8 years ago

@enj Ok I got it working, nested structs are now validated with req, you can find it here https://github.com/aneshas/go-endpoints @campoy don't know if you would accept a PR

campoy commented 8 years ago

Hey sorry for the delay, I totally missed this bug! :disappointed:

Could you create an actual PR so I can easily see the changes?

Thanks, @aneshas !

aneshas commented 8 years ago

@campoy Hey, yes, I'll post it today