Skip to content

Data Validation #3

Description

@Denny-g6labs

Thank you for your library!

I'm using it to generate an invoice, and I'm wondering what is the best approach to do a data validation.

Example:

        pmt_id = pacs_008_001_08.PaymentIdentification7(
            # Is our transaction ID
            instr_id=transaction["id"],
            # Unique identification, as assigned by the initiating party, to unambiguously identify the transaction. This identification is passed on, unchanged, throughout the entire end-to-end chain.
            end_to_end_id=transaction["reference"],
            # Unique End-to-end Transaction Reference
            uetr=str(uuid.uuid4()),
        )

end_to_end_id has a max_length of 35, but no error is raised, nor is the value cut off when the XML is generated.

How to use these Dataclass field metadata info to do a validation?

Thanks!

EDIT:
Quick self made implementation:

        pmt_id = pacs_008_001_08.PaymentIdentification7(
            # Is our transaction ID
            instr_id=transaction["id"],
            # Unique identification, as assigned by the initiating party, to unambiguously identify the transaction. This identification is passed on, unchanged, throughout the entire end-to-end chain.
            end_to_end_id=self.__validate(
                pacs_008_001_08.PaymentIdentification7, "end_to_end_id", transaction["reference"]
            ),
            # Unique End-to-end Transaction Reference
            uetr=str(uuid.uuid4()),
        )

# or

        # Payment identification
        pmt_id = pacs_008_001_08.PaymentIdentification7(
            # Is our transaction ID
            instr_id=transaction["id"],
            # Unique identification, as assigned by the initiating party, to unambiguously identify the transaction. This identification is passed on, unchanged, throughout the entire end-to-end chain.
            end_to_end_id=transaction["reference"],
            # Unique End-to-end Transaction Reference
            uetr=str(uuid.uuid4()),
        )
        pmt_id.end_to_end_id = self.__validate(
            pacs_008_001_08.PaymentIdentification7, "end_to_end_id", pmt_id.end_to_end_id
        )

# [...]

    def __validate(self, dataclass, field_name, value):
        """
        Validate Metaclass fields (Basic implementation)
        """

        for field_info in fields(dataclass):
            if field_info.name == field_name:
                # Required
                if field_info.metadata.get("required"):
                    if not value:
                        raise BusinessError(
                            f"""Unable to generate ISO 20022: Field {field_name!r} ({dataclass.__name__}) is required but the value is empty"""
                        )

                # Pattern
                if pattern := field_info.metadata.get("pattern"):
                    if not re.findall(pattern, value or ""):
                        raise BusinessError(
                            f"""Unable to generate ISO 20022: Field {field_name!r} ({dataclass.__name__}) has a pattern of {pattern!r} but the value is {value!r}"""
                        )

                # Min Length
                if min_length := field_info.metadata.get("min_length"):
                    if value and len(value) < min_length:  # If value is empty, it is not required (tested above)
                        raise BusinessError(
                            f"""Unable to generate ISO 20022: Field {field_name!r} ({dataclass.__name__}) has a min length of {min_length} but the value is {value!r}"""
                        )

                # Max Length
                if max_length := field_info.metadata.get("max_length"):
                    if len(value or "") > max_length:
                        # raise BusinessError(
                        #     f"""Unable to generate ISO 20022: Field {field_name!r} ({dataclass.__name__}) has a max length of {max_length} but the value is {value!r}"""
                        # )
                        return value[:max_length]

        return value

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions