-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (46 loc) · 1.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const VALIDATOR_REGEXPS = Object.seal({
// Adapted from source: https://github.com/solvvy/redact-pii/blob/da5f16f/src/built-ins/simple-regexp-patterns.ts
EMAIL: /([a-z0-9_\-.+]+)@\w+(\.\w+)*/,
PHONE: /(\(?\+?[0-9]{1,2}\)?[-. ]?)?(\(?[0-9]{3}\)?|[0-9]{3})[-. ]?([0-9]{3}[-. ]?[0-9]{4}|\b[A-Z0-9]{7}\b)/,
PROPERTY_UNIT_NUMBER: /(apt|bldg|dept|fl|hngr|lot |pier|rm|ste|slip|trlr|unit |#)\s*\.?#?\s*[0-9]+[a-z0-9-]*\b/i,
PROPERTY_STREET_ADDRESS: /\d+(\s+[nsew]\.?)?(\s+\w+){1,}\s+(?:st(?:\.|reet)?|dr(?:\.|ive)?|pl(?:\.|ace)?|ave(?:\.|nue)?|rd|road|lane|boulevard|blvd|loop|way|circle|cir|court|ct|plaza|square|run|parkway|point|pike|square|driveway|trace|park|terrace)(\s|[^a-z]|$)/,
SSN: /\b\d{3}[ -.]\d{2}[ -.]\d{4}\b/
});
const VALIDATOR_REGEXPS_KEYS = Object.keys(VALIDATOR_REGEXPS);
const VALIDATOR_ERROR_MESSAGES = Object.seal({
EMAIL: 'Contains an email address',
PHONE: 'Contains a phone number',
PROPERTY_UNIT_NUMBER: 'Contains a condo-unit number',
PROPERTY_STREET_ADDRESS: 'Contains a street address',
SSN: 'Contains a social-security number'
});
const VALIDATOR_ERROR_REASONS = Object.seal({
EMAIL: 'Email addresses disallowed in field',
PHONE: 'Phone numbers disallowed in field',
PROPERTY_UNIT_NUMBER: 'Condo-unit numbers disallowed in field',
PROPERTY_STREET_ADDRESS: 'Street addresses disallowed in field',
SSN: 'Social-Security Numbers disallowed in field'
});
const containsPII = str => {
const strToTest = (str || '')
.replace(/\s+/g, ' ')
.replace(/\s/g, ' ')
.toLocaleLowerCase();
if (!strToTest) {
return false;
}
let key;
let errors = [];
for (let i = 0; i < VALIDATOR_REGEXPS_KEYS.length; i++) {
key = VALIDATOR_REGEXPS_KEYS[i];
if (VALIDATOR_REGEXPS[key].test(strToTest)) {
errors.push({
type: key,
message: VALIDATOR_ERROR_MESSAGES[key],
reason: VALIDATOR_ERROR_REASONS[key]
});
}
}
return errors.length ? errors : false;
};
module.exports = containsPII;