Skip to content

Commit 0fa94d3

Browse files
committed
test: added parse tests
1 parent a4ff802 commit 0fa94d3

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

union_validate_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,47 @@ func TestValidateUnionAllSchemasFail(t *testing.T) {
5050
assert.Equal(t, 5, dest)
5151
}
5252

53+
func TestValidateUnionDefaultBranchSucceeds(t *testing.T) {
54+
validator := Union([]ZogSchema{
55+
String().Required(),
56+
Int().Default(42),
57+
})
58+
dest := 0
59+
60+
errs := validator.Validate(&dest)
61+
62+
assert.Empty(t, errs)
63+
assert.Equal(t, 42, dest)
64+
}
65+
66+
func TestValidateUnionCatchBranchSucceeds(t *testing.T) {
67+
validator := Union([]ZogSchema{
68+
String().Required(),
69+
Int().GT(10, Message("must be greater than 10")).Catch(42),
70+
})
71+
dest := 5
72+
73+
errs := validator.Validate(&dest)
74+
75+
assert.Empty(t, errs)
76+
assert.Equal(t, 42, dest)
77+
}
78+
79+
func TestValidateUnionInvalidDestinationTypeAllSchemasFail(t *testing.T) {
80+
validator := Union([]ZogSchema{
81+
String().Required(),
82+
Int().Required(),
83+
})
84+
dest := true
85+
86+
errs := validator.Validate(&dest)
87+
88+
assert.Len(t, errs, 2)
89+
assert.Equal(t, zconst.IssueCodeInvalidType, errs[0].Code)
90+
assert.Equal(t, zconst.IssueCodeInvalidType, errs[1].Code)
91+
assert.True(t, dest)
92+
}
93+
5394
func TestValidateUnionStringOrInt(t *testing.T) {
5495
validator := Union([]ZogSchema{
5596
String().Required(),

0 commit comments

Comments
 (0)