diff --git a/schema/any.go b/schema/any.go index 5e7bcaf..54d13a5 100644 --- a/schema/any.go +++ b/schema/any.go @@ -67,12 +67,15 @@ func (a *AnySchema) ValidateCompatibility(typeOrData any) error { case reflect.Map: return nil default: - // Check if it's an actual 'any' schema. - schemaType, ok := typeOrData.(*AnySchema) - if !ok { - // It's not an any schema, so error + // Schema is not a primitive, slice, or map type, so check the complex types + // Explicitly allow object schemas since their reflected type can be a struct if they are struct mapped. + switch typeOrData.(type) { + case *AnySchema, *OneOfSchema[int64], *OneOfSchema[string], *ObjectSchema: + // These are the allowed values. + default: + // It's not an any schema or a type compatible with an any schema, so error return &ConstraintError{ - Message: fmt.Sprintf("unsupported schema type for 'any' type: %T", schemaType), + Message: fmt.Sprintf("schema type `%T` cannot be used as an input for an 'any' type", typeOrData), } } return nil diff --git a/schema/any_test.go b/schema/any_test.go index 2d6f6d2..356d4bc 100644 --- a/schema/any_test.go +++ b/schema/any_test.go @@ -154,6 +154,24 @@ func TestAnyTypeReflectedType(t *testing.T) { func TestAnyValidateCompatibility(t *testing.T) { s1 := schema.NewAnySchema() + properties := map[string]*schema.PropertySchema{ + "field1": schema.NewPropertySchema( + schema.NewIntSchema(nil, nil, nil), + nil, + true, + nil, + nil, + nil, + nil, + nil, + ), + } + type someStruct struct { + //nolint:unused // This is just for test purposes. + field1 int + } + objectSchema := schema.NewObjectSchema("some-id", properties) + structMappedObjectSchema := schema.NewStructMappedObjectSchema[someStruct]("some-id", properties) assert.NoError(t, s1.ValidateCompatibility(schema.NewAnySchema())) assert.NoError(t, s1.ValidateCompatibility(schema.NewStringSchema(nil, nil, nil))) @@ -170,5 +188,13 @@ func TestAnyValidateCompatibility(t *testing.T) { assert.NoError(t, s1.ValidateCompatibility(map[string]any{})) assert.NoError(t, s1.ValidateCompatibility(schema.NewStringEnumSchema(map[string]*schema.DisplayValue{}))) assert.NoError(t, s1.ValidateCompatibility(schema.NewIntEnumSchema(map[int64]*schema.DisplayValue{}, nil))) - + assert.NoError(t, s1.ValidateCompatibility(objectSchema)) + // Test struct mapped since it may have a different reflected type. + assert.NoError(t, s1.ValidateCompatibility(structMappedObjectSchema)) + assert.NoError(t, s1.ValidateCompatibility( + schema.NewOneOfStringSchema[string](map[string]schema.Object{}, "id", false), + )) + assert.NoError(t, s1.ValidateCompatibility( + schema.NewOneOfIntSchema[int64](map[int64]schema.Object{}, "id", false), + )) }