From 042c3855ae029e388fb269c55bad6bea11459cce Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Tue, 2 Jan 2024 21:53:21 -0800 Subject: [PATCH] fix: better validation of manual schemas --- schema.go | 22 ++++++++++++++++++++++ validate_test.go | 12 ++++++++++++ 2 files changed, 34 insertions(+) diff --git a/schema.go b/schema.go index e9819759..06c7fd3f 100644 --- a/schema.go +++ b/schema.go @@ -168,6 +168,28 @@ func (s *Schema) PrecomputeMessages() { } } + if s.propertyNames == nil { + s.propertyNames = make([]string, 0, len(s.Properties)) + for name := range s.Properties { + s.propertyNames = append(s.propertyNames, name) + } + } + + if s.requiredMap == nil { + s.requiredMap = map[string]bool{} + for _, name := range s.Required { + s.requiredMap[name] = true + } + } + + if s.Items != nil { + s.Items.PrecomputeMessages() + } + + for _, prop := range s.Properties { + prop.PrecomputeMessages() + } + for _, sub := range s.OneOf { sub.PrecomputeMessages() } diff --git a/validate_test.go b/validate_test.go index 517fc9de..b685f79f 100644 --- a/validate_test.go +++ b/validate_test.go @@ -893,6 +893,18 @@ var validateTests = []struct { input: map[any]any{"items": []any{map[any]any{}}}, errs: []string{"expected required property value to be present"}, }, + { + name: "manual object property required", + s: &huma.Schema{ + Type: huma.TypeObject, + Required: []string{"value"}, + Properties: map[string]*huma.Schema{ + "value": {Type: huma.TypeString}, + }, + }, + input: map[string]any{}, + errs: []string{"expected required property value to be present"}, + }, { name: "enum success", typ: reflect.TypeOf(struct {