浏览代码

Regex validator added for quantity input

Jovian (Netbook) 5 年之前
父节点
当前提交
c7f03d6874
共有 1 个文件被更改,包括 12 次插入2 次删除
  1. 12 2
      cli/quantity_asker.py

+ 12 - 2
cli/quantity_asker.py

@@ -1,14 +1,24 @@
-from PyInquirer import prompt 
+from PyInquirer import prompt, Validator, ValidationError
+from prompt_toolkit import document
+import regex
 
 # Ask for a quantity
 
+class QuantityValidator(Validator):
+    def validate(self, document):
+        ok = regex.match('^[1-9][0-9]*00$', document.text)
+        if not ok:
+            raise ValidationError(message = 'Quantity must be a positive integer multiple of 100')
+
 class QuantityAsker:
+    """Ask quantity for name entries"""
     def __init__(self):
         self.widget = [
             {
                 'type':'input',
                 'name':'number',
-                'message':'Enter a quantity'
+                'message':'Enter a quantity',
+                'validate':QuantityValidator
             }
         ]