diff --git a/internals/panics.go b/internals/panics.go index 56bee5d..a898893 100644 --- a/internals/panics.go +++ b/internals/panics.go @@ -3,9 +3,10 @@ package internals import "fmt" const ( - PanicTypeCast = "Zog Panic: Type Cast Error\n Current context: %s\n Expected valPtr type to correspond with type defined in schema. But it does not. Expected type: *%T, got: %T\nFor more information see: https://zog.dev/panics#type-cast-errors" - PanicTypeCastCoercer = "Zog Panic: Type Cast Error\n Current context: %s\n Expected coercer return value to correspond with type defined in schema. But it does not. Expected type: *%T, got: %T\nFor more information see: https://zog.dev/panics#type-cast-errors" - PanicMissingStructField = "Zog Panic: Struct Schema Definition Error\n Current context: %s\n Provided struct is missing expected schema key: %s.\n This means you have made a mistake in your schema definition.\nFor more information see: https://zog.dev/panics#schema-definition-errors" + PanicTypeCast = "Zog Panic: Type Cast Error\n Current context: %s\n Expected valPtr type to correspond with type defined in schema. But it does not. Expected type: *%T, got: %T\nFor more information see: https://zog.dev/panics#type-cast-errors" + PanicTypeCastCoercer = "Zog Panic: Type Cast Error\n Current context: %s\n Expected coercer return value to correspond with type defined in schema. But it does not. Expected type: *%T, got: %T\nFor more information see: https://zog.dev/panics#type-cast-errors" + PanicMissingStructField = "Zog Panic: Struct Schema Definition Error\n Current context: %s\n Provided struct is missing expected schema key: %s.\n This means you have made a mistake in your schema definition.\nFor more information see: https://zog.dev/panics#schema-definition-errors" + PanicInvalidArgumentsExpectedPointer = "Zog Panic: Expected destination value to be a pointer but it was not. This is generally caused by forgetting to pass a pointer to your Validate/Parse function. Do schema.Validate(&myStruct), not schema.Validate(myStruct) " ) func Panicf(format string, args ...any) { diff --git a/struct.go b/struct.go index 5fb08a1..71d5267 100644 --- a/struct.go +++ b/struct.go @@ -86,7 +86,12 @@ func (v *StructSchema) process(ctx *p.SchemaCtx) { } // 3. Process / validate struct fields - structVal := reflect.ValueOf(ctx.ValPtr).Elem() + structRefVal := reflect.ValueOf(ctx.ValPtr) + kind := structRefVal.Kind() + if kind != reflect.Pointer && kind != reflect.Interface { + p.Panicf(p.PanicInvalidArgumentsExpectedPointer) + } + structVal := structRefVal.Elem() subCtx := ctx.NewSchemaCtx(ctx.Data, ctx.ValPtr, ctx.Path, v.getType()) defer subCtx.Free() for key, processor := range v.schema {