Skip to content

Commit

Permalink
fix: better validation of manual schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Jan 3, 2024
1 parent 57554dc commit 042c385
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
12 changes: 12 additions & 0 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 042c385

Please sign in to comment.