| 1626 | } |
| 1627 | |
| 1628 | func 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", |