示例#1
0
func validate(scope *gorm.Scope) {
	if _, ok := scope.Get("gorm:update_column"); !ok {
		if result, ok := scope.DB().Get(skipValidations); !(ok && result.(bool)) {
			if !scope.HasError() {
				scope.CallMethod("Validate")
				resource := scope.IndirectValue().Interface()
				_, validatorErrors := govalidator.ValidateStruct(resource)
				if validatorErrors != nil {
					if errors, ok := validatorErrors.(govalidator.Errors); ok {
						for _, err := range flatValidatorErrors(errors) {
							scope.DB().AddError(formattedError(err, resource))
						}
					} else {
						scope.DB().AddError(validatorErrors)
					}
				}
			}
		}
	}
}