| 162 | } |
| 163 | |
| 164 | func (v *responseObject) Validate(iValidators []interface{}, variablesMapping map[string]interface{}) (err error) { |
| 165 | for _, iValidator := range iValidators { |
| 166 | validator, ok := iValidator.(Validator) |
| 167 | if !ok { |
| 168 | return errors.New("validator type error") |
| 169 | } |
| 170 | // parse check value |
| 171 | checkItem := validator.Check |
| 172 | checkValue := v.searchField(checkItem, variablesMapping) |
| 173 | |
| 174 | // get assert method |
| 175 | assertMethod := validator.Assert |
| 176 | assertFunc, ok := builtin.Assertions[assertMethod] |
| 177 | if !ok { |
| 178 | return errors.New(fmt.Sprintf("unexpected assertMethod: %v", assertMethod)) |
| 179 | } |
| 180 | |
| 181 | // parse expected value |
| 182 | expectValue, err := v.parser.Parse(validator.Expect, variablesMapping) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | validResult := &ValidationResult{ |
| 187 | Validator: Validator{ |
| 188 | Check: validator.Check, |
| 189 | Expect: expectValue, |
| 190 | Assert: assertMethod, |
| 191 | Message: validator.Message, |
| 192 | }, |
| 193 | CheckValue: checkValue, |
| 194 | CheckResult: "fail", |
| 195 | } |
| 196 | |
| 197 | // do assertion |
| 198 | result := assertFunc(v.t, checkValue, expectValue) |
| 199 | if result { |
| 200 | validResult.CheckResult = "pass" |
| 201 | } |
| 202 | v.validationResults = append(v.validationResults, validResult) |
| 203 | log.Info(). |
| 204 | Str("checkExpr", validator.Check). |
| 205 | Str("assertMethod", assertMethod). |
| 206 | Interface("expectValue", expectValue). |
| 207 | Str("expectValueType", builtin.InterfaceType(expectValue)). |
| 208 | Interface("checkValue", checkValue). |
| 209 | Str("checkValueType", builtin.InterfaceType(checkValue)). |
| 210 | Bool("result", result). |
| 211 | Msgf("validate %s", checkItem) |
| 212 | if !result { |
| 213 | v.t.Fail() |
| 214 | log.Error(). |
| 215 | Str("checkExpr", validator.Check). |
| 216 | Str("assertMethod", assertMethod). |
| 217 | Interface("checkValue", checkValue). |
| 218 | Str("checkValueType", builtin.InterfaceType(checkValue)). |
| 219 | Interface("expectValue", expectValue). |
| 220 | Str("expectValueType", builtin.InterfaceType(expectValue)). |
| 221 | Msg("assert failed") |