Skip to content

Commit

Permalink
Support for Roman Numerals in names + more robust dob formats (#120)
Browse files Browse the repository at this point in the history
1.3.8
  • Loading branch information
kennsippell authored Apr 2, 2024
1 parent a1349b9 commit b68916b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cht-user-management",
"version": "1.3.7",
"version": "1.3.8",
"main": "dist/index.js",
"dependencies": {
"@fastify/autoload": "^5.8.0",
Expand Down
9 changes: 8 additions & 1 deletion src/config/chis-ug/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"contact_type": "district_hospital",
"type": "name",
"required": true,
"parameter": ["\\sH[ /]*C\\s"],
"level": 1
}
],
Expand All @@ -52,35 +53,41 @@
"friendly_name": "District",
"property_name": "district",
"type": "name",
"parameter": ["\\sdistrict\\s"],
"required": true
},
{
"friendly_name": "Sub District",
"property_name": "sub_district",
"parameter": ["\\ssub\\s", "\\sdistrict\\s"],
"type": "name",
"required": true
"required": false
},
{
"friendly_name": "County",
"property_name": "county",
"type": "name",
"parameter": ["\\scounty\\s"],
"required": true
},
{
"friendly_name": "Sub County",
"property_name": "sub_county",
"type": "name",
"parameter": ["\\ssub\\s", "\\scounty\\s"],
"required": true
},
{
"friendly_name": "Parish",
"property_name": "parish",
"parameter": ["\\sparish\\s"],
"type": "name",
"required": true
},
{
"friendly_name": "Village",
"property_name": "village",
"parameter": ["\\svillage\\s"],
"type": "name",
"required": true
}
Expand Down
13 changes: 8 additions & 5 deletions src/lib/validator-dob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ export default class ValidatorDateOfBirth implements IValidator {

format(input : string) : string {
const parsed = this.parse(input);
const asISODate = parsed.toISODate();
if (!this.isValid(input) || !asISODate) {
if (!this.isValid(input)) {
return input;
}

return asISODate;
const asISODate = parsed.toISODate();
return asISODate ?? input;
}

get defaultError(): string {
return 'Not a valid Date of Birth (eg. 1990-02-26 or 25)';
return 'Not a valid Date of Birth (eg. 1990-02-26 or 26/2/1985 or 38)';
}

private parse(input: string): DateTime {
Expand All @@ -32,6 +32,9 @@ export default class ValidatorDateOfBirth implements IValidator {
return DateTime.now().minus({ years: asNumber });
}

return DateTime.fromISO(strippedInput);
const hasSlash = strippedInput.includes('/');
return hasSlash ?
DateTime.fromFormat(strippedInput, 'd/M/yyyy')
: DateTime.fromISO(strippedInput);
}
}
6 changes: 4 additions & 2 deletions src/lib/validator-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ export default class ValidatorName implements IValidator {

private titleCase(value: string): string {
const words = value.toLowerCase().split(' ');
const titleCase = (word: string) => word[0].toUpperCase() + word.slice(1);
const isRomanNumeral = /^[ivx]+$/ig;
const titleCased = words
.filter(x => x)
.map(word => word[0].toUpperCase() + word.slice(1)).join(' ');
.filter(Boolean)
.map(word => word.match(isRomanNumeral) ? word.toUpperCase() : titleCase(word)).join(' ');
return titleCased.replace(/ '/g, '\'');
}
}
5 changes: 5 additions & 0 deletions test/lib/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const scenarios: Scenario[] = [
{ type: 'name', prop: 'NZATANI / ILALAMBYU', isValid: true, altered: 'Nzatani / Ilalambyu' },
{ type: 'name', prop: 'Sam\'s CHU', propertyParameter: ['CHU', 'Comm Unit'], isValid: true, altered: 'Sam\'s' },
{ type: 'name', prop: 'Jonathan M.Barasa', isValid: true, altered: 'Jonathan M Barasa' },
{ type: 'name', prop: 'Robert xiv', isValid: true, altered: 'Robert XIV' },

{ type: 'name', prop: ' ', isValid: true, altered: '' },

{ type: 'dob', prop: '', isValid: false },
Expand All @@ -61,6 +63,9 @@ const scenarios: Scenario[] = [
{ type: 'dob', prop: 'abc', isValid: false, altered: 'abc' },
{ type: 'dob', prop: ' 1 0 0 ', isValid: true, altered: DateTime.now().minus({ years: 100 }).toISODate() },
{ type: 'dob', prop: '-1', isValid: false, altered: '-1' },
{ type: 'dob', prop: '15/2/1985', isValid: true, altered: '1985-02-15' },
{ type: 'dob', prop: '1/2/1 985', isValid: true, altered: '1985-02-01' },
{ type: 'dob', prop: '1/13/1985', isValid: false },

{ type: 'select_one', prop: ' male', isValid: true, propertyParameter: GENDER_OPTIONS },
{ type: 'select_one', prop: 'female ', isValid: true, propertyParameter: GENDER_OPTIONS },
Expand Down

0 comments on commit b68916b

Please sign in to comment.