diff --git a/binding/binding_test.go b/binding/binding_test.go index f90488cd..fa501dad 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -747,6 +747,16 @@ func TestBindingBSON(t *testing.T) { string(data[1:])) } +func TestBindingBSONValidation(t *testing.T) { + // Empty Foo violates binding:"required" — validate() must be called after Unmarshal. + var obj FooStruct + data, _ := bson.Marshal(&obj) + req := requestWithBody(http.MethodPost, "/", string(data)) + var result FooStruct + err := BSON.Bind(req, &result) + require.Error(t, err) +} + func TestValidationFails(t *testing.T) { var obj FooStruct req := requestWithBody(http.MethodPost, "/", `{"bar": "foo"}`) diff --git a/binding/bson.go b/binding/bson.go index 464890f0..2fb0bbbd 100644 --- a/binding/bson.go +++ b/binding/bson.go @@ -26,5 +26,8 @@ func (b bsonBinding) Bind(req *http.Request, obj any) error { } func (bsonBinding) BindBody(body []byte, obj any) error { - return bson.Unmarshal(body, obj) + if err := bson.Unmarshal(body, obj); err != nil { + return err + } + return validate(obj) }