| 121 | } |
| 122 | |
| 123 | func TestDictionaryValidate(t *testing.T) { |
| 124 | a := assert.New(t) |
| 125 | |
| 126 | checker := func(i int) error { |
| 127 | if i < 0 { |
| 128 | return fmt.Errorf("some error") |
| 129 | } |
| 130 | return nil |
| 131 | } |
| 132 | tb := Dictionary{}.Int("a", 1, "attribute a", checker).Float("b", float32(1), "attribute b", nil) |
| 133 | a.NoError(tb.Validate(map[string]interface{}{"a": 1})) |
| 134 | a.EqualError(tb.Validate(map[string]interface{}{"a": -1}), "attribute a error: some error") |
| 135 | a.EqualError(tb.Validate(map[string]interface{}{"_a": -1}), fmt.Sprintf(errUnsupportedAttribute, "_a")) |
| 136 | a.EqualError(tb.Validate(map[string]interface{}{"a": 1.0}), "attribute a must be of type int, but got float64") |
| 137 | a.NoError(tb.Validate(map[string]interface{}{"b": float32(1.0)})) |
| 138 | a.NoError(tb.Validate(map[string]interface{}{"b": 1})) |
| 139 | } |
| 140 | |
| 141 | func TestParamsDocs(t *testing.T) { |
| 142 | a := assert.New(t) |