diff --git a/validation/errors.go b/validation/errors.go index 0bb4fea95..cc4f47c18 100644 --- a/validation/errors.go +++ b/validation/errors.go @@ -137,6 +137,14 @@ func HasErrorCode(err error, code ErrorCode) bool { return false } +var newLineReplacer = strings.NewReplacer("\n", "\\n", "\r", "\\r") + +// propertyValueString returns the string representation of the given value. +// Structs, interfaces, maps and slices are converted to compacted JSON strings. +// It tries to improve readability by: +// - limiting the string to 100 characters +// - removing leading and trailing whitespaces +// - escaping newlines func propertyValueString(v interface{}) string { if v == nil { return "" @@ -151,11 +159,14 @@ func propertyValueString(v interface{}) string { s = string(raw) } case reflect.Invalid: - s = "" + return "" default: s = fmt.Sprint(ft.Interface()) } - return limitString(s, 100) + s = limitString(s, 100) + s = strings.TrimSpace(s) + s = newLineReplacer.Replace(s) + return s } // ruleSetError is a container for transferring multiple errors reported by RuleSet. diff --git a/validation/errors_test.go b/validation/errors_test.go index e91897a52..7aa2f0691 100644 --- a/validation/errors_test.go +++ b/validation/errors_test.go @@ -79,12 +79,28 @@ func TestNewPropertyError(t *testing.T) { InputValue: nil, ExpectedValue: "", }, + "blank lines": { + InputValue: ` SELECT value FROM my-table WHERE value = "abc" `, + ExpectedValue: `SELECT value FROM my-table WHERE value = "abc"`, + }, + "multiline": { + InputValue: ` +SELECT value FROM +my-table WHERE value = "abc" +`, + ExpectedValue: "SELECT value FROM\\nmy-table WHERE value = \"abc\"", + }, + "carriage return": { + InputValue: "return\rcarriage", + ExpectedValue: "return\\rcarriage", + }, } { t.Run(name, func(t *testing.T) { err := NewPropertyError( "name", test.InputValue, &RuleError{Message: "msg"}) + fmt.Println(err) assert.Equal(t, &PropertyError{ PropertyName: "name", PropertyValue: test.ExpectedValue,