Class: Phronomy::Guardrail::Builtin::PIIPatternDetector
- Inherits:
-
InputGuardrail
- Object
- Phronomy::Guardrail::Base
- InputGuardrail
- Phronomy::Guardrail::Builtin::PIIPatternDetector
- Defined in:
- lib/phronomy/guardrail/builtin/pii_pattern_detector.rb
Overview
Input guardrail that detects common PII patterns in the input string.
Four categories are supported and each can be individually toggled:
- +:my_number+ — Japanese My Number (12-digit national ID)
- +:credit_card+ — Credit / debit card numbers
- +:email+ — E-mail addresses
- +:phone+ — Japanese domestic phone numbers
All four categories are active by default.
Constant Summary collapse
- PATTERNS =
Recognised PII categories and their detection patterns.
{ # Japanese My Number: 12 consecutive or grouped digits (4-4-4). my_number: { pattern: /(?<!\d)(?<!\d[- ])\d{4}[- ]?\d{4}[- ]?\d{4}(?![- ]?\d)/, label: "My Number" }, # Credit / debit card: 16 digits, optionally separated by spaces or hyphens. credit_card: { pattern: /\b(?:\d{4}[- ]?){3}\d{4}\b/, label: "credit card number" }, # Email address (simplified RFC 5322). email: { pattern: /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b/, label: "email address" }, # Japanese phone number: starts with 0, groups of 2-5 / 1-4 / 4 digits. phone: { pattern: /\b0\d{1,4}[- ]?\d{1,4}[- ]?\d{4}\b/, label: "phone number" } }.freeze
- ALL_CATEGORIES =
PATTERNS.keys.freeze
Instance Method Summary collapse
- #check(value) ⇒ Object
-
#initialize(detect: ALL_CATEGORIES) ⇒ PIIPatternDetector
constructor
A new instance of PIIPatternDetector.
Methods inherited from Phronomy::Guardrail::Base
Constructor Details
#initialize(detect: ALL_CATEGORIES) ⇒ PIIPatternDetector
Returns a new instance of PIIPatternDetector.
54 55 56 57 58 59 |
# File 'lib/phronomy/guardrail/builtin/pii_pattern_detector.rb', line 54 def initialize(detect: ALL_CATEGORIES) unknown = Array(detect) - ALL_CATEGORIES raise ArgumentError, "Unknown PII categories: #{unknown.inspect}" if unknown.any? @active_patterns = Array(detect).map { |cat| PATTERNS.fetch(cat) } end |
Instance Method Details
#check(value) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/phronomy/guardrail/builtin/pii_pattern_detector.rb', line 64 def check(value) text = value.to_s @active_patterns.each do |entry| fail!("PII detected in input: #{entry[:label]}") if text.match?(entry[:pattern]) end end |