Skip to content

Commit 8706a28

Browse files
authored
SDK-2588 Extend parsing of age verification attribute name (#519)
Updated the data_type AgeVerification to handle the name parsing so it accepts an optional additional value (an age buffer - literally).
1 parent 3ab6f0d commit 8706a28

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/data_type/age.verification.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ class AgeVerification {
1818
constructor(name, value) {
1919
Validation.isString(name, 'name');
2020
Validation.oneOf(value, ['true', 'false'], 'value');
21-
Validation.matchesPattern(name, /^[^:]+:(?!.*:)[0-9]+$/, 'attribute.name');
21+
Validation.matchesPattern(name, /^[^:]+:[0-9]+(?::[0-9]+)?$/, 'attribute.name');
2222

23-
const split = name.split(':');
23+
const [type, age, ageBuffer] = name.split(':');
2424
/** @private */
25-
this.checkType = split[0];
25+
this.checkType = type;
2626

2727
/** @private */
28-
this.age = parseInt(split[1], 10);
28+
this.age = parseInt(age, 10);
29+
/** @private */
30+
this.ageBuffer = ageBuffer ? parseInt(ageBuffer, 10) : undefined;
2931
/** @private */
3032
this.result = value === 'true';
3133
}
@@ -50,6 +52,15 @@ class AgeVerification {
5052
return this.age;
5153
}
5254

55+
/**
56+
* The age buffer allowed
57+
*
58+
* @returns {number}
59+
*/
60+
getAgeBuffer() {
61+
return this.ageBuffer;
62+
}
63+
5364
/**
5465
* Whether the profile passed the age check.
5566
*

tests/data_type/age.verification.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { AgeVerification } = require('../../src/data_type/age.verification');
22
const { ATTR_AGE_OVER, ATTR_AGE_UNDER } = require('../../src/yoti_common/constants');
33

4-
const EXPECTED_PATTERN = /^[^:]+:(?!.*:)[0-9]+$/;
4+
const EXPECTED_PATTERN = /^[^:]+:[0-9]+(?::[0-9]+)?$/;
55

66
describe('AgeVerification', () => {
77
describe('when malformed age derivation is provided', () => {
@@ -15,7 +15,7 @@ describe('AgeVerification', () => {
1515
':age_over:18',
1616
'age_over::18',
1717
'age_over:18:',
18-
'age_over:18:21',
18+
'age_over:18:21:',
1919
].forEach((name) => {
2020
expect(() => new AgeVerification(name, 'true'))
2121
.toThrow(new TypeError(`'attribute.name' value '${name}' does not match format '${EXPECTED_PATTERN}'`));
@@ -50,4 +50,21 @@ describe('AgeVerification', () => {
5050
expect(ageVerification.getResult()).toBe(true);
5151
});
5252
});
53+
54+
describe(`when well formed age derivation is provided (with age buffer): ${ATTR_AGE_OVER}21:5`, () => {
55+
const ageVerification = new AgeVerification(`${ATTR_AGE_OVER}21:5`, 'true');
56+
57+
it('should parse check type', () => {
58+
expect(ageVerification.getCheckType()).toBe('age_over');
59+
});
60+
it('should parse age', () => {
61+
expect(ageVerification.getAge()).toBe(21);
62+
});
63+
it('should parse age buffer', () => {
64+
expect(ageVerification.getAgeBuffer()).toBe(5);
65+
});
66+
it('should parse result', () => {
67+
expect(ageVerification.getResult()).toBe(true);
68+
});
69+
});
5370
});

types/src/data_type/age.verification.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export class AgeVerification {
1212
/** @private */
1313
private age;
1414
/** @private */
15+
private ageBuffer;
16+
/** @private */
1517
private result;
1618
/**
1719
* The type of age check performed, as specified on Yoti Hub.
@@ -27,6 +29,12 @@ export class AgeVerification {
2729
* @returns {number}
2830
*/
2931
getAge(): number;
32+
/**
33+
* The age buffer allowed
34+
*
35+
* @returns {number}
36+
*/
37+
getAgeBuffer(): number;
3038
/**
3139
* Whether the profile passed the age check.
3240
*

0 commit comments

Comments
 (0)