Check if a given guid is valid. Don't check for `None` as `None` guid will trigger "non-optional" validation error either way. :return: `None` if guid is valid, otherwise a string with an error message.
(guid: str)
| 612 | |
| 613 | |
| 614 | def validate_guid(guid: str) -> Union[str, None]: |
| 615 | """Check if a given guid is valid. |
| 616 | |
| 617 | Don't check for `None` as `None` guid will trigger "non-optional" validation error either way. |
| 618 | |
| 619 | :return: `None` if guid is valid, otherwise a string with an error message. |
| 620 | """ |
| 621 | if len(guid) != 22: |
| 622 | return "Guid length should be 22 characters." |
| 623 | if guid[0] not in "0123": |
| 624 | return "Guid first character must be either a 0, 1, 2, or 3." |
| 625 | try: |
| 626 | ifcopenshell.guid.expand(guid) |
| 627 | except: |
| 628 | allowed_characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$" |
| 629 | if any(c for c in guid if c not in allowed_characters): |
| 630 | return "Guid contains invalid characters, allowed characters: '%s'." % allowed_characters |
| 631 | # NOTE: are there actually cases where guid won't expand, besides invalid characters? |
| 632 | return "Couldn't decompress guid, it's not base64 encoded." |
| 633 | return None |
| 634 | |
| 635 | |
| 636 | def to_string_header_entity(header_entity): |