Skip to content

Commit

Permalink
test: additional tests with custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
Oudwins committed Feb 7, 2025
1 parent ba21cda commit dce828f
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,33 +320,43 @@ func TestStructRequired(t *testing.T) {
assert.Equal(t, "custom_required", errs["$root"][0].Message())
}

// func TestStructOptional(t *testing.T) {
// type TestStruct struct {
// OptionalField string
// }

// schema := Struct(Schema{
// "optionalField": String().Optional(),
// })

// t.Run("Optional field present", func(t *testing.T) {
// var output TestStruct
// data := map[string]any{"optionalField": "present"}

// errs := schema.Parse(data, &output)
// assert.Nil(t, errs)
// assert.Equal(t, "present", output.OptionalField)
// })

// t.Run("Optional field missing", func(t *testing.T) {
// var output TestStruct
// data := map[string]any{}

// errs := schema.Parse(data, &output)
// assert.Nil(t, errs)
// assert.Equal(t, "", output.OptionalField)
// })
// }
type Custom int

const (
Custom1 Custom = 1
Custom2 Custom = 2
)

func TestStructCustomType(t *testing.T) {
s := struct {
Custom int
}{}
schema := Struct(Schema{
"custom": Int().OneOf([]int{int(Custom1), int(Custom2)}),
})
errs := schema.Parse(map[string]any{"custom": int(Custom1)}, &s)
assert.Nil(t, errs)
assert.Equal(t, int(Custom1), s.Custom)
}

type Customs = int

const (
Customs1 Customs = 1
Customs2 Customs = 2
)

func TestStructCustomType2(t *testing.T) {
s := struct {
Custom Customs
}{}
schema := Struct(Schema{
"custom": Int().OneOf([]int{Customs1, Customs2}),
})
errs := schema.Parse(map[string]any{"custom": Customs1}, &s)
assert.Nil(t, errs)
assert.Equal(t, Customs1, s.Custom)
}

func TestStructGetType(t *testing.T) {
s := Struct(Schema{
Expand Down

0 comments on commit dce828f

Please sign in to comment.