diff --git a/schema/object_test.go b/schema/object_test.go index 64abaeb..5da333c 100644 --- a/schema/object_test.go +++ b/schema/object_test.go @@ -69,7 +69,7 @@ var testStructSchemaPtr = schema.NewTypedObject[*testStructPtr]("testStruct", ma ), }) -func TestObjectUnserialization(t *testing.T) { +func TestObjectUnserialization_Success(t *testing.T) { data := map[string]any{ "Field1": 42, "field3": "Hello world!", @@ -94,6 +94,43 @@ func TestObjectUnserialization(t *testing.T) { }) } +func TestObjectUnserialization_MissingFields(t *testing.T) { + dataMissing1 := map[string]any{ + "field3": "Hello world!", + } + _, err := testStructSchema.Unserialize(dataMissing1) + assert.Error(t, err) + assert.Contains(t, err.Error(), "field is required") + + dataMissing3 := map[string]any{ + "Field1": 42, + } + _, err = testStructSchema.Unserialize(dataMissing3) + assert.Error(t, err) + assert.Contains(t, err.Error(), "field is required") +} + +func TestObjectUnserialization_IncorrectType(t *testing.T) { + dataMissing1 := map[string]any{ + "Field1": "this cannot be represented as an integer", + "field3": "Hello world!", + } + _, err := testStructSchema.Unserialize(dataMissing1) + assert.Error(t, err) + assert.Contains(t, err.Error(), "parsing") +} + +func TestObjectUnserialization_ExtraField(t *testing.T) { + dataMissing1 := map[string]any{ + "Field1": 42, + "wrong": "wrong", + "field3": "Hello world!", + } + _, err := testStructSchema.Unserialize(dataMissing1) + assert.Error(t, err) + assert.Contains(t, err.Error(), "Invalid parameter 'wrong'") +} + type embeddedTestStruct struct { Field1 int64 }