-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add UEN field (#2100) * add uen field in public frontend * add essential uen types * make sure uen field renders * replace isUenValid with isUenField * refactor: simplify isUenValid logic (#2156) * refactor: simplify isUenValid logic * fix: use stricter regex matching Co-authored-by: Aniruddha Adhikary (Ani) <[email protected]>
- Loading branch information
1 parent
7908bbc
commit 7f91e48
Showing
18 changed files
with
286 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Schema } from 'mongoose' | ||
|
||
import { IUenFieldSchema } from '../../../types' | ||
|
||
const createUenFieldSchema = () => new Schema<IUenFieldSchema>() | ||
|
||
export default createUenFieldSchema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { chain, left, right } from 'fp-ts/lib/Either' | ||
import { flow } from 'fp-ts/lib/function' | ||
|
||
import { ProcessedSingleAnswerResponse } from 'src/app/modules/submission/submission.types' | ||
import { ResponseValidator } from 'src/types/field/utils/validation' | ||
|
||
import { isUenValid } from '../../../../shared/util/uen-validation' | ||
|
||
import { notEmptySingleAnswerResponse } from './common' | ||
|
||
type UenValidator = ResponseValidator<ProcessedSingleAnswerResponse> | ||
type UenValidatorConstructor = () => UenValidator | ||
|
||
/** | ||
* Returns a validator to check if uen | ||
* format is correct. | ||
*/ | ||
const uenValidator: UenValidator = (response) => { | ||
return isUenValid(response.answer) | ||
? right(response) | ||
: left(`UenValidator:\tanswer is not a valid UEN`) | ||
} | ||
|
||
/** | ||
* Returns a validation function for a uen field when called. | ||
*/ | ||
export const constructUenValidator: UenValidatorConstructor = () => | ||
flow(notEmptySingleAnswerResponse, chain(uenValidator)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/public/modules/forms/base/componentViews/field-uen.client.view.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<div class="text-field field-group row"> | ||
<!-- Question --> | ||
<label | ||
for="{{ vm.field._id || 'defaultID'}}" | ||
class="field-question col-xs-12" | ||
> | ||
<div id="label-{{ vm.field._id || 'defaultID'}}"> | ||
<span class="field-number" ng-show="vm.field.field_number" | ||
>{{ vm.field.field_number }}.</span | ||
> | ||
<span class="field-title">{{ vm.field.title }}</span> | ||
<span class="field-optional" ng-if="!vm.field.required">(optional)</span> | ||
</div> | ||
<div | ||
id="description-{{ vm.field._id || 'defaultID'}}" | ||
class="field-description" | ||
ng-if="vm.field.description" | ||
ng-bind-html="vm.field.description | linky:'_blank'" | ||
></div> | ||
</label> | ||
|
||
<!-- Input --> | ||
<div class="col-xs-12 field-input"> | ||
<input | ||
id="{{ vm.field._id || 'defaultID'}}" | ||
type="text" | ||
name="{{ vm.field._id || 'defaultID'}}" | ||
class="input-custom input-large" | ||
ng-model="vm.field.fieldValue" | ||
ng-required="vm.field.required" | ||
ng-disabled="vm.field.disabled" | ||
ng-model-options="{ allowInvalid: true }" | ||
ng-keyup="vm.forms.myForm[(vm.field._id || 'defaultID')].$setTouched()" | ||
placeholder="Enter UEN" | ||
autocomplete="off" | ||
ng-trim="true" | ||
ng-class="vm.field.disabled && vm.field.fieldValue ? 'myinfo-disable' : ''" | ||
aria-labelledby="label-{{ vm.field._id || 'defaultID'}}" | ||
aria-describedby="description-{{ vm.field._id || 'defaultID'}}" | ||
validate-uen | ||
/> | ||
</div> | ||
|
||
<!-- Error --> | ||
<div | ||
class="col-xs-12" | ||
ng-show="vm.forms.myForm[(vm.field._id || 'defaultID')].$touched" | ||
ng-messages="vm.forms.myForm[(vm.field._id || 'defaultID')].$error" | ||
> | ||
<field-error-component ng-message="required"></field-error-component> | ||
<div ng-message="uenValidator" class="alert-custom alert-error"> | ||
<i class="bx bx-exclamation bx-md icon-spacing"></i> | ||
<span class="alert-msg">Please enter a valid UEN</span> | ||
</div> | ||
</div> | ||
</div> |
10 changes: 10 additions & 0 deletions
10
src/public/modules/forms/base/components/field-uen.client.component.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
angular.module('forms').component('uenFieldComponent', { | ||
templateUrl: 'modules/forms/base/componentViews/field-uen.client.view.html', | ||
bindings: { | ||
field: '<', | ||
forms: '<', | ||
}, | ||
controllerAs: 'vm', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/public/modules/forms/base/directives/validate-uen.client.directive.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict' | ||
|
||
const { isUenValid } = require('../../../../../shared/util/uen-validation') | ||
|
||
angular.module('forms').directive('validateUen', validateUen) | ||
|
||
function validateUen() { | ||
return { | ||
restrict: 'A', | ||
require: 'ngModel', | ||
link: function (_scope, _elem, _attrs, ctrl) { | ||
ctrl.$validators.uenValidator = function (modelValue) { | ||
return ctrl.$isEmpty(modelValue) ? true : isUenValid(modelValue) | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/** | ||
* Validate entity-type indicators, as per | ||
* https://www.uen.gov.sg/ueninternet/faces/pages/admin/aboutUEN.jspx | ||
*/ | ||
const VALID_ENTITY_TYPE_INDICATORS = new Set([ | ||
'LP', | ||
'LL', | ||
'FC', | ||
'PF', | ||
'RF', | ||
'MQ', | ||
'MM', | ||
'NB', | ||
'CC', | ||
'CS', | ||
'MB', | ||
'FM', | ||
'GS', | ||
'GA', | ||
'GB', | ||
'DP', | ||
'CP', | ||
'NR', | ||
'CM', | ||
'CD', | ||
'MD', | ||
'HS', | ||
'VH', | ||
'CH', | ||
'MH', | ||
'CL', | ||
'XL', | ||
'CX', | ||
'RP', | ||
'TU', | ||
'TC', | ||
'FB', | ||
'FN', | ||
'PA', | ||
'PB', | ||
'SS', | ||
'MC', | ||
'SM', | ||
]) | ||
|
||
/** | ||
* Helper to check whether a string is numeric | ||
* @param s String | ||
* @returns True if string is numeric | ||
*/ | ||
const isNumeric = (s: string): boolean => !!s.match(/^[0-9]+$/) | ||
|
||
/** | ||
* Helper to check whether a string is alphabetic | ||
* @param s string | ||
* @returns True if string is alphabetic | ||
*/ | ||
const isAlphabetic = (s: string): boolean => !!s.match(/^[a-zA-Z]+$/) | ||
|
||
/** | ||
* Validates whether a provided string value adheres to the UIN/FIN format | ||
* as provided on the Singapore Government's National Registration Identity Card. | ||
* @param value The value to be validated | ||
*/ | ||
export const isUenValid = (uen: string): boolean => { | ||
// allow lowercase strings | ||
uen = uen.toUpperCase() | ||
|
||
// check if uen is 9 or 10 digits | ||
if (uen.length < 9 || uen.length > 10) { | ||
return false | ||
} | ||
|
||
// (A) Businesses registered with ACRA | ||
if (uen.length === 9) { | ||
// check that last character is a letter | ||
const lastChar = uen[uen.length - 1] | ||
if (!isAlphabetic(lastChar)) { | ||
return false | ||
} | ||
|
||
// check that first 8 letters are all numbers | ||
const first8Chars = uen.slice(0, 8) | ||
if (!isNumeric(first8Chars)) { | ||
return false | ||
} | ||
|
||
// (A) Businesses registered with ACRA (SUCCESS) | ||
return true | ||
} | ||
|
||
// Length is 10 | ||
// check that last character is a letter | ||
const lastChar = uen[uen.length - 1] | ||
if (!isAlphabetic(lastChar)) { | ||
return false | ||
} | ||
|
||
// (B) Local companies registered with ACRA | ||
const first4Chars = uen.slice(0, 4) | ||
if (isNumeric(first4Chars)) { | ||
// if first 4 are digits then next 5 must be digits too | ||
const next5Chars = uen.slice(4, 9) | ||
return isNumeric(next5Chars) | ||
} | ||
|
||
// (C) All other entities which will be issued new UEN | ||
// check that 1st letter is either T or S or R | ||
const firstChar = uen[0] | ||
if (!['T', 'S', 'R'].includes(firstChar)) { | ||
return false | ||
} | ||
|
||
// check that 2nd and 3rd letters are numbers only | ||
const chars2And3 = uen.slice(1, 3) | ||
if (!isNumeric(chars2And3)) { | ||
return false | ||
} | ||
|
||
// check entity-type indicator | ||
const entityTypeIndicator = uen.slice(3, 5) | ||
if (!VALID_ENTITY_TYPE_INDICATORS.has(entityTypeIndicator)) { | ||
return false | ||
} | ||
|
||
// check that 6th to 9th letters are numbers only | ||
const chars5To8 = uen.slice(5, 9) | ||
return isNumeric(chars5To8) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.