Coverage for src / questions / validators / vartype.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-21 16:36 +0000

1from prompt_toolkit.validation import ValidationError 

2 

3from ... import utils 

4 

5 

6def validate(value, schema): 

7 """ 

8 Return { value to use, stop (True/False) } 

9 

10 If stop is True, the rest of the validators are skipped. 

11 """ 

12 # ctrl-d gives us a None 

13 if value is None: 

14 return None, False 

15 

16 vartype = schema.get("type", "string") 

17 if vartype == "string": 

18 value = str(value) 

19 

20 if vartype == "integer": 

21 try: 

22 value = int(value) 

23 except ValueError: 

24 raise ValidationError(message="Invalid type. Expected an integer") from None 

25 

26 if vartype == "boolean": 

27 try: 

28 value = utils.string_as_bool(value) 

29 return value, True 

30 except ValueError: 

31 raise ValidationError(message="Invalid type. Expected a boolean") from None 

32 

33 return value, False