From 6bf41f3a69023ea8aaf1bd645cb3c18253fbb3e4 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Sun, 20 Sep 2020 17:15:10 +0530 Subject: [PATCH] fix: Don't allow trailing or leading whitespace in DocType name --- frappe/core/doctype/doctype/doctype.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/frappe/core/doctype/doctype/doctype.py b/frappe/core/doctype/doctype/doctype.py index 5c558f3bd6..5a148c826f 100644 --- a/frappe/core/doctype/doctype/doctype.py +++ b/frappe/core/doctype/doctype/doctype.py @@ -634,13 +634,15 @@ class DocType(Document): if not name: name = self.name + flags = {"flags": re.ASCII} if six.PY3 else {} + + # a DocType name should not start or end with an empty space + if re.match("^[ \t\n\r]+|[ \t\n\r]+$", name, **flags): + frappe.throw(_("DocType's name should not start or end with whitespace"), frappe.NameError) + # a DocType's name should not start with a number or underscore # and should only contain letters, numbers and underscore - if six.PY2: - is_a_valid_name = re.match("^(?![\W])[^\d_\s][\w ]+$", name) - else: - is_a_valid_name = re.match("^(?![\W])[^\d_\s][\w ]+$", name, flags = re.ASCII) - if not is_a_valid_name: + if not re.match("^(?![\W])[^\d_\s][\w ]+$", name, **flags): frappe.throw(_("DocType's name should start with a letter and it can only consist of letters, numbers, spaces and underscores"), frappe.NameError)