Skip to content

Validation

Validation of Model and printing errors to console.

ValidationMixin

Validation of Model and printing errors to console.

Source code in src/ramifice/paladins/validation.py
class ValidationMixin:
    """Validation of Model and printing errors to console."""

    async def is_valid(self) -> bool:
        """Check data validity.

        The main use is to check data from web forms.
        It is also used to verify Models that do not migrate to the database.
        """
        result_check: dict[str, Any] = await self.check()
        return result_check["is_valid"]

    def print_err(self) -> None:
        """Printing errors to console.

        Convenient to use during development.
        """
        metadata = self.__class__.META
        descriptor_fields = metadata["all_descriptor_fields"]
        is_err: bool = False

        for f_name in descriptor_fields:
            f_errors = getattr(self, f"{f_name}__core").errors
            if len(f_errors) > 0:
                # title
                if not is_err:
                    print(colored("\nERRORS:", "red", attrs=["bold"]))  # ruff:ignore[print]
                    print(colored("Model: ", "blue", attrs=["bold"]), end="")  # ruff:ignore[print]
                    print(colored(f"`{self.full_model_name()}`", "blue"))  # ruff:ignore[print]
                    is_err = True
                # field name
                print(colored("Field: ", "green", attrs=["bold"]), end="")  # ruff:ignore[print]
                print(colored(f"`{f_name}`:", "green"))  # ruff:ignore[print]
                # error messages
                print(colored("\n".join(f_errors), "red"))  # ruff:ignore[print]

        f_alerts = getattr(self, "id__core").alerts  # ruff:ignore[get-attr-with-constant]
        if len(f_alerts) > 0:
            # title
            print(colored("AlERTS:", "yellow", attrs=["bold"]))  # ruff:ignore[print]
            # messages
            print(colored("\n".join(f_alerts), "yellow"), end="\n\n")  # ruff:ignore[print]
        else:
            print(end="\n\n")  # ruff:ignore[print]

is_valid() async

Check data validity.

The main use is to check data from web forms. It is also used to verify Models that do not migrate to the database.

Source code in src/ramifice/paladins/validation.py
async def is_valid(self) -> bool:
    """Check data validity.

    The main use is to check data from web forms.
    It is also used to verify Models that do not migrate to the database.
    """
    result_check: dict[str, Any] = await self.check()
    return result_check["is_valid"]

print_err()

Printing errors to console.

Convenient to use during development.

Source code in src/ramifice/paladins/validation.py
def print_err(self) -> None:
    """Printing errors to console.

    Convenient to use during development.
    """
    metadata = self.__class__.META
    descriptor_fields = metadata["all_descriptor_fields"]
    is_err: bool = False

    for f_name in descriptor_fields:
        f_errors = getattr(self, f"{f_name}__core").errors
        if len(f_errors) > 0:
            # title
            if not is_err:
                print(colored("\nERRORS:", "red", attrs=["bold"]))  # ruff:ignore[print]
                print(colored("Model: ", "blue", attrs=["bold"]), end="")  # ruff:ignore[print]
                print(colored(f"`{self.full_model_name()}`", "blue"))  # ruff:ignore[print]
                is_err = True
            # field name
            print(colored("Field: ", "green", attrs=["bold"]), end="")  # ruff:ignore[print]
            print(colored(f"`{f_name}`:", "green"))  # ruff:ignore[print]
            # error messages
            print(colored("\n".join(f_errors), "red"))  # ruff:ignore[print]

    f_alerts = getattr(self, "id__core").alerts  # ruff:ignore[get-attr-with-constant]
    if len(f_alerts) > 0:
        # title
        print(colored("AlERTS:", "yellow", attrs=["bold"]))  # ruff:ignore[print]
        # messages
        print(colored("\n".join(f_alerts), "yellow"), end="\n\n")  # ruff:ignore[print]
    else:
        print(end="\n\n")  # ruff:ignore[print]