Skip to main content

Field Mapper a Python library for seamless data field validation, transformation, and mapping with support for types, lengths, and custom rules.

Project description

Field Mapper: Documentation

Field Mapper is a Python library designed for validating, mapping, and transforming data fields. It is particularly useful for integrating third-party systems, ensuring compatibility when third-party fields and internal system fields differ. The library supports type checking, length constraints, optional fields, custom validation rules, and seamless data transformation for structured data validation.

Installation

Install the library using pip

pip install field-mapper

Quick Start

  1. Define Fields Create a field map dictionary. Also, Create a fields dictionary to define the rules for your data fields.
field_map = {
    "name": "full_name",
    "email": "contact_email",
    "phone": "mobile_number",
    "income": "monthly_income"
}
fields = {
    "name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
    "email": {"type": str, "max_length": 100, "required_field": True, "required_value":True},
    "phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
    "income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}
  1. Prepare Data The input should be a list of dictionaries.
data = [
    {"name": "Alice", "email": "alice@example.com", "phone": "1234567890"},
    {"name": "Bob", "email": "charlieexample.com", "phone": "453543535", "income":0},
    {"name": "Charlie", "email": "charlie@example.com", "phone": "34534523", "income":0}
]
  1. Data Process Use the process method to check and transform the data.
from field_mapper import FieldMapper

field_map = {
    "name": "full_name",
    "email": "contact_email",
    "phone": "mobile_number",
    "income": "monthly_income"
}

fields = {
    "name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
    "email": {"type": str, "max_length": 100, "required_field": True, "required_value":True},
    "phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
    "income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}

data = [
    {"name": "Alice", "email": "alice@example.com", "phone": "1234567890"},
    {"name": "Bob", "email": "charlieexample.com", "phone": "453543535", "income":0},
    {"name": "Charlie", "email": "charlie@example.com", "phone": "34534523", "income":0}
]

mapper = FieldMapper(fields, field_map)
processed_data = mapper.process(data)
print(processed_data)
print(mapper.error)
  1. Custom Validation Define custom validation logic for specific fields.
def validate_email(value):  
    import re  
    if not re.match(r"[^@]+@[^@]+\.[^@]+", value):  
        raise ValueError(f"Invalid email address: {value}")  

#Add the custom validator in the field definition:
field_map = {
    "name": "full_name",
    "email": "contact_email",
    "phone": "mobile_number",
    "income": "monthly_income"
}
fields = {
    "name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
    "email": {"type": str, "max_length": 100, "required_field": True, "required_value":True, "custom": validate_email},
    "phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
    "income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}

data = [
    {"name": "Alice", "email": "alice@example.com", "phone": "1234567890"},
    {"name": "Bob", "email": "charlieexample.com", "phone": "453543535", "income":0},
    {"name": "Charlie", "email": "charlie@example.com", "phone": "34534523", "income":0}
]
mapper = FieldMapper(fields, field_map)
processed_data = mapper.process(data)
print(processed_data)
print(mapper.error)
  1. Optional Fields Mark fields as optional with required_field: False.
fields = {
    "phone": {"type": str, "max_length": 15, "required_field": False},
}
  1. Required Value If required_value=True is set, their presence is mandatory and values can't be empty.
fields = {"email": {"type": str, "max_length": 100, "required_field": True, "required_value":True}
  1. Skip Duplicates Data If skip_duplicate=True is set, Skip duplicate data. Raises:DuplicatesDataError: If duplicate entries are found.
from field_mapper.mapper import FieldMapper

field_map = {
    "name": "full_name",
    "email": "contact_email",
    "phone": "mobile_number",
    "income": "monthly_income"
}
fields = {
    "name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
    "email": {"type": str, "max_length": 100, "required_field": True, "required_value":True},
    "phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
    "income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}
data = [
    {"name": "Charlie", "email": "charlie@example.com", "phone": "888888", "income":0},
    {"name": "Charlie", "email": "charlie@example.com", "phone": "444444", "income":0},
    {"name": "Charlie", "email": "charlie@example.com", "phone": "444444", "income":0}
]
mapper = FieldMapper(fields, field_map)
processed_data = mapper.process(data, skip_duplicate=True)
print("Output:", processed_data)
print("Error:",mapper.error)
  1. Get Nested Data
from field_mapper.mapper import FieldMapper

field_map = {
    "name": "full_name",
    "email[0]": "contact_email",
    "phone.phone_two": "mobile_number",
    "income[0].Jan": "monthly_income",
    "year_salary[].year": "salary"
}
data = [
    {
        "name": "Alice",
        "email": ["alice@example.com", "alice2@example.com"],
        "phone": {"phone_one": '22222', "phone_two": "2332432"},
        "income": [{"Jan": 2000, "Feb": 3000}],
        "year_salary": [
            {"year": "2020", "Jan": 2000, "Feb": 3000},
            {"year": "2021", "Jan": 2000, "Feb": 3000}
        ]
    }
]
fields = {
    "name": {"type": str, "position": "name", "max_length": 50, "required_field": True, "required_value": True},
    "email": {"type": list, "position": "email[0]", "max_length": 100, "required_field": True, "required_value": True},
    "phone": {"type": dict, "position": "phone.phone_two", "required_field": True, "required_value": True},
    "income": {"type": list, "position": "income[0].Jan", "required_field": True, "required_value": True},
    "year_salary": {"type": list, "position": "year_salary[].year", "required_field": True, "required_value": True}
}
mapper = FieldMapper(fields=fields, field_map=field_map)
processed_data = mapper.process(data, skip_duplicate=True)
print("Output:", processed_data)
print("Error:",mapper.error)

#Output: [{'full_name': 'Alice', 'contact_email': 'alice@example.com', 'mobile_number': '2332432', 'monthly_income': 2000, 'salary': ['2020', '2021']}]
#Error: []

Example usage

from field_mapper import FieldMapper


def validate_email(value: str) -> bool:
    return "@" in value and "." in value

field_map = {
    "name": "full_name",
    "email": "contact_email",
    "phone": "mobile_number",
    "income": "monthly_income"
}

fields = {
    "name": {"type": str, "max_length": 50, "required_field": True, "required_value":True},
    "email": {"type": str, "max_length": 100, "required_field": True, "required_value":True, "custom": validate_email},
    "phone": {"type": str, "max_length": 15, "required_field": False, "required_value":False},
    "income": {"type": int, "max_length": 15, "required_field": True, "required_value":False}
}

data = [
    {"name": "Alice", "email": "alice@example.com", "phone": "1234567890"},
    {"name": "Bob", "email": "charlieexample.com", "phone": "453543535", "income":0},
    {"name": "Charlie", "email": "charlie@example.com", "phone": "34534523", "income":0}
]
mapper = FieldMapper(fields, field_map)
processed_data = mapper.process(data)
print(processed_data)
print(mapper.error)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

field_mapper-0.2.1.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

field_mapper-0.2.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file field_mapper-0.2.1.tar.gz.

File metadata

  • Download URL: field_mapper-0.2.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for field_mapper-0.2.1.tar.gz
Algorithm Hash digest
SHA256 db0e8b29e0bfe57e9e5dc312986f9802e39e709c538b32129dd2079b1b6e7281
MD5 233fbaff11f15e624efceb60dd227337
BLAKE2b-256 23dc6d8cf22949077edf97aa65edede35cfaf96bba7bb6bee86de01b3f9a39a9

See more details on using hashes here.

File details

Details for the file field_mapper-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: field_mapper-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for field_mapper-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c4907304f5891f0e75d2f0b5d22952b8b5c03c5e9891c0bd00b871c010668415
MD5 cf93bb3ae0554a1e372f1e1e409d8dc2
BLAKE2b-256 f08f4b833f7f4a6e1770a987fb0af66aa7619386adc6b2b2dc40f4334d12c87c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page