Skip to content

Commit

Permalink
better (ansible#803)
Browse files Browse the repository at this point in the history
Predefine `FieldValidatorState` and `FieldValidatorStates`

This is follow up to ansible#790 which replaces the complex inline type with a predefined type.
As I was explaining it to @ssbarnea I realized this might be a better approach. (thanks for mentioning it sorin)

Reviewed-by: Sorin Sbarnea <[email protected]>
Reviewed-by: None <None>
  • Loading branch information
cidrblock authored and ssbarnea committed Jan 30, 2022
1 parent 34a6b9e commit 3f5ca51
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/ansible_navigator/ui_framework/field_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
"""
from dataclasses import dataclass
from typing import Callable
from typing import List
from typing import Union

from .curses_window import Window
from .form_defs import FieldValidationStates
from .form_handler_button import FormHandlerButton
from .sentinels import Unknown
from .validators import FieldValidators


Expand All @@ -31,15 +30,15 @@ def full_prompt(self) -> str:
"""no default to add into the prompt for checkbox"""
return ""

def validate(self, response: List[Union[Unknown, bool]]) -> None:
def validate(self, response: FieldValidationStates) -> None:
"""validate this instance"""
validation = self.validator(response)
if validation.error_msg:
self.disabled = True
else:
self.disabled = False

def conditional_validation(self, response: List[Union[Unknown, bool]]) -> None:
def conditional_validation(self, response: FieldValidationStates) -> None:
"""conditional validation used for
tab
"""
Expand Down
8 changes: 8 additions & 0 deletions src/ansible_navigator/ui_framework/form_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
"""

from enum import Enum
from typing import List
from typing import Union

from .sentinels import Unknown


FieldValidationState = Union[Unknown, bool]
FieldValidationStates = List[FieldValidationState]


class FormType(Enum):
Expand Down
4 changes: 3 additions & 1 deletion src/ansible_navigator/ui_framework/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Union
from urllib.parse import urlparse

from .form_defs import FieldValidationStates
from .sentinels import Unknown
from .sentinels import unknown

Expand Down Expand Up @@ -53,7 +54,8 @@ def masked_or_none(text="", hint: bool = False) -> Union[Validation, str]:

@staticmethod
def none(
text: Union[List[Union[Unknown, bool]], str] = "", hint: bool = False
text: Union[FieldValidationStates, str] = "",
hint: bool = False,
) -> Union[Validation, str]:
"""no validation"""
if hint:
Expand Down

0 comments on commit 3f5ca51

Please sign in to comment.