MCPcopy
hub / github.com/danielgtaylor/huma / Test_validateWithDiscriminator

Function Test_validateWithDiscriminator

validate_test.go:1628–1738  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1626}
1627
1628func Test_validateWithDiscriminator(t *testing.T) {
1629 registry := huma.NewMapRegistry("#/components/schemas/", huma.DefaultSchemaNamer)
1630 catSchema := registry.Schema(reflect.TypeFor[Cat](), true, "Cat")
1631 dogSchema := registry.Schema(reflect.TypeFor[Dog](), true, "Dog")
1632
1633 s := &huma.Schema{
1634 Type: huma.TypeObject,
1635 Description: "Animal",
1636 OneOf: []*huma.Schema{
1637 {Ref: catSchema.Ref},
1638 {Ref: dogSchema.Ref},
1639 },
1640 Discriminator: &huma.Discriminator{
1641 PropertyName: "kind",
1642 Mapping: map[string]string{
1643 "cat": catSchema.Ref,
1644 "dog": dogSchema.Ref,
1645 },
1646 },
1647 }
1648
1649 pb := huma.NewPathBuffer([]byte(""), 0)
1650 res := &huma.ValidateResult{}
1651
1652 tests := []struct {
1653 name string
1654 input any
1655 wantErrs []string
1656 }{
1657 {
1658 name: "cat - minLength case",
1659 input: map[string]any{
1660 "kind": "cat",
1661 "name": "c",
1662 },
1663 wantErrs: []string{"expected length >= 2"},
1664 },
1665 {
1666 name: "cat - maxLength case",
1667 input: map[string]any{
1668 "kind": "cat",
1669 "name": "aaaaaaaaaaa",
1670 },
1671 wantErrs: []string{"expected length <= 10"},
1672 },
1673 {
1674 name: "cat - invalid schema",
1675 input: map[string]any{
1676 "kind": "dog",
1677 "name": "cat",
1678 },
1679 wantErrs: []string{
1680 "expected required property color to be present",
1681 "unexpected property",
1682 },
1683 },
1684 {
1685 name: "cat - any invalid schema",

Callers

nothing calls this directly

Calls 7

ResetMethod · 0.95
ResetMethod · 0.95
LenMethod · 0.80
SchemaMethod · 0.65
RunMethod · 0.65
ErrorMethod · 0.65
ValidateMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…