BBizKit

redact.matchFieldType

v0.1.0 latest

Detect a PCI/GDPR-relevant PII field type from a field name using exact-match lookup against a built-in name map.

redactfield-detectionpiiclassificationrouting

Detect a PCI/GDPR-relevant PII field type from a field name using exact-match lookup against a built-in name map.

Signature

function matchFieldType(lastSegment: string): RedactFieldType | null

Type Definitions:

Problem

When redacting objects with arbitrary field names, the redaction strategy depends on the data type (email vs phone vs IP). Manual if/else chains for field name detection are error-prone and inconsistent across codebases. A centralized field type registry ensures uniform detection.

How It Works

Performs an exact match of the last path segment against a built-in map of known field names. Known mappings: email/mail → 'email', phone/mobile/tel → 'phone', ip/ipAddress/remoteAddress → 'ip'. Returns null for unrecognized names.

Boundaries

  • Uses exact string matching only — no substring, regex, or fuzzy matching.
  • The built-in map contains 9 entries across 3 field types; it is not user-extensible.
  • Returns null for unrecognized field names — the caller decides the fallback.

Replaces

Common boilerplate this function replaces:

if (name === 'email' || name === 'mail') return 'email'; else if (name === 'phone' || name === 'mobile' || name === 'tel') return 'phone'; else if (name === 'ip' || name === 'ipAddress' || name === 'remoteAddress') return 'ip'; else return null;

Examples

redact.matchFieldType("email");  // "email"
redact.matchFieldType("ipAddress");  // "ip"
redact.matchFieldType("phone");  // "phone"
redact.matchFieldType("username");  // null
redact.matchFieldType("mail");  // "email"

Standards

Caveats

  • Field name matching is case-sensitive: 'Email' and 'EMAIL' return null, only 'email' matches.
  • Used internally by redactObject() for 'partial' strategy routing; can also be called standalone.

FAQ

How to detect if a field contains email data for redaction?

redact.matchFieldType('email') returns 'email'. The function recognizes 'email' and 'mail' as email field names.

What field names are recognized for phone number detection?

The function recognizes 'phone', 'mobile', and 'tel' as phone field names, returning 'phone' for all three.

How to detect IP address fields for redaction?

The function recognizes 'ip', 'ipAddress', and 'remoteAddress' as IP field names, returning 'ip'.

What happens with unrecognized field names?

The function returns null. The caller can then apply a default redaction strategy (e.g., token masking).

Is field name matching case-sensitive?

Yes — matching is case-sensitive and exact. Only lowercase names in the built-in map are recognized.