Skip to content

Commit 986203c

Browse files
committed
chore: add validation for birthday format
1 parent c4207d4 commit 986203c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

contacts.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,20 @@ CNAuthorizationStatus AuthStatus() {
190190
[contact setFamilyName:[NSString stringWithUTF8String:nick_name.c_str()]];
191191
}
192192

193+
if(contact_data.Has("birthday")) {
194+
std::string birth_day = contact_data.Get("birthday").As<Napi::String>().Utf8Value();
195+
NSString *bday = [NSString stringWithUTF8String:birth_day.c_str()];
196+
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
197+
[formatter setDateFormat:@"yyyy-MM-dd"];
198+
199+
NSDate *bday_date = [formatter dateFromString:bday];
200+
201+
NSCalendar *cal = [NSCalendar currentCalendar];
202+
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
203+
NSDateComponents *birthday_components = [cal components:unitFlags fromDate:bday_date];
204+
[contact setBirthday:birthday_components];
205+
}
206+
193207
CNSaveRequest *request = [[CNSaveRequest alloc] init];
194208
[request addContact:contact toContainerWithIdentifier:nil];
195209
bool success = [addressBook executeSaveRequest:request error:nil];

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ function getContactsByName(name) {
88

99
function addNewContact(contact) {
1010
if (!contact || Object.keys(contact).length === 0) {
11-
throw new TypeError('contact must be a nonempty object')
11+
throw new TypeError('contact must be a non-empty object')
1212
} else {
1313
const hasFirstName = contact.hasOwnProperty('firstName')
1414
const hasLastName = contact.hasOwnProperty('lastName')
1515
const hasNickname = contact.hasOwnProperty('nickname')
16+
const hasBirthday = contact.hasOwnProperty('birthday')
1617
const hasPhoneNumbers = contact.hasOwnProperty('phoneNumbers')
1718
const hasPostalAddresses = contact.hasOwnProperty('postalAddresses')
1819
const hasEmailAddresses = contact.hasOwnProperty('emailAddresses')
@@ -23,6 +24,15 @@ function addNewContact(contact) {
2324
if (hasPhoneNumbers && !Array.isArray(contact.phoneNumbers)) throw new TypeError('phoneNumbers must be an array')
2425
if (hasPostalAddresses && !Array.isArray(contact.postalAddresses)) throw new TypeError('postalAddresses must be an array')
2526
if (hasEmailAddresses && !Array.isArray(contact.emailAddresses)) throw new TypeError('emailAddresses must be an array')
27+
28+
if (hasBirthday) {
29+
const datePattern = /^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/
30+
if (typeof contact.birthday !== 'string') {
31+
throw new TypeError('birthday must be a string')
32+
} else if (!contact.birthday.match(datePattern)) {
33+
throw new Error('birthday must use YYYY-MM-DD format')
34+
}
35+
}
2636
}
2737

2838
return contacts.addNewContact.call(this, contact)

0 commit comments

Comments
 (0)