From 8a0bd604bd38ffef689e020dd04092b0c5cdc176 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Thu, 10 Sep 2020 10:17:26 +0200 Subject: [PATCH] validation: remove invalid characters from message field --- qrbill.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/qrbill.go b/qrbill.go index 020ef3f..fb5a2b9 100644 --- a/qrbill.go +++ b/qrbill.go @@ -149,6 +149,11 @@ var ( nonNumericRe = regexp.MustCompile(`[^0-9]`) nonAlphanumericRe = regexp.MustCompile(`[^A-Za-z0-9]`) nonDecimalRe = regexp.MustCompile(`[^0-9.]`) + + // The SIX Swiss Implementation Guidelines Reference Standard + // Documentation declares the following regular expression pattern in + // https://validation.iso-payments.ch/gp/projectdata/qrrechnung/deliverables/installed/publishingproject/qr__ch.scm/html/en/0247.htm + ustrdRe = regexp.MustCompile(`([a-zA-Z0-9\.,;:'\+\-/\(\)?\*\[\]\{\}\\` + "`" + `´~ ]|[!"#%&<>÷=@_$£]|[àáâäçèéêëìíîïñòóôöùúûüýßÀÁÂÄÇÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜÑ])`) ) func (q *QRCH) Validate() *QRCH { @@ -198,9 +203,14 @@ func (q *QRCH) Validate() *QRCH { clone.RmtInf.Ref = v[:27] } - if v := clone.RmtInf.AddInf.Ustrd; len(v) > 140 { - clone.RmtInf.AddInf.Ustrd = v[:140] + ustrd := clone.RmtInf.AddInf.Ustrd + matches := ustrdRe.FindAllString(ustrd, -1) + ustrd = strings.Join(matches, "") + + if len(ustrd) > 140 { + ustrd = ustrd[:140] } + clone.RmtInf.AddInf.Ustrd = ustrd return clone }