Skip to content

Commit

Permalink
Create validate_ifc_guid.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvianen committed Jul 6, 2024
1 parent eb03df3 commit 36fe502
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/django-ifc/utils/validate_ifc_guid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# -*- coding: utf-8 -*-


# =============================================================================
# Docstring
# =============================================================================

"""
Provides IFC GUID Validation Function
=====================================
This module contains a validation function for ensuring that a given string
meets the IFC Globally Unique Identifier (GUID) specifications, as required by
the Industry Foundation Classes (IFC) standards. The IFC GUID is typically
a 22-character Base64 encoded string, uniquely identifying IFC entities.
"""


# =============================================================================
# Import
# =============================================================================

# Import | Standard Library
# import uuid
import re # Used for regular expression matching

# Import | Libraries
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

# Import | Local Modules


# =============================================================================
# Variables
# =============================================================================

__all__ = ["validate_ifc_guid", ]


# =============================================================================
# Functions
# =============================================================================

def validate_ifc_guid(value):
"""
IFC GUID Validation Function
============================
Validates that the given value conforms to the 22-character Base64
requirement of IfcGloballyUniqueId.
Parameters:
value (str): The string to validate as an IFC Globally Unique
Identifier.
Raises:
ValidationError: If the string does not conform to the expected format.
"""

# Define the regex pattern for a Base64 encoded string of 22 characters
pattern = r'^[A-Za-z0-9+/]{22}$'

# Check if the provided value matches the pattern
if not isinstance(value, str) or not re.match(pattern, value):
raise ValidationError(
_("'%(value)s' is not a valid 22-character Base64 encoded string."), # noqa E501
params={'value': value},
code='invalid' # Including an error code for programmatic handling
)

0 comments on commit 36fe502

Please sign in to comment.