Skip to content

Commit b135ec0

Browse files
authored
Merge pull request #2309 from blackflux/dev
[Gally]: master <- dev
2 parents b189768 + dc89f4e commit b135ec0

3 files changed

Lines changed: 76 additions & 12 deletions

File tree

src/param/_abstract.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class Abstract {
1515
required = true,
1616
nullable = false,
1717
normalize = true,
18-
getter = null
18+
getter = null,
19+
lowercase = false
1920
} = {}) {
2021
assert(Object.keys(positionMapping).includes(position), `Unknown Parameter Position: ${position}`);
2122
assert(
@@ -29,6 +30,7 @@ class Abstract {
2930
this.required = required;
3031
this.nullable = nullable;
3132
this.normalize = normalize;
33+
this.lowercase = lowercase;
3234
this.getter = getter;
3335
this.type = null;
3436
}
@@ -38,7 +40,7 @@ class Abstract {
3840
}
3941

4042
get(event) {
41-
const result = get(event, `${positionMapping[this.position]}.${
43+
let result = get(event, `${positionMapping[this.position]}.${
4244
this.position === 'header'
4345
? this.name.toLowerCase()
4446
: this.name
@@ -56,18 +58,23 @@ class Abstract {
5658
value: result
5759
});
5860
}
59-
const r = this.getter !== null && ![undefined, null].includes(result)
61+
result = this.getter !== null && ![undefined, null].includes(result)
6062
? (params) => this.getter(result, params)
6163
: result;
62-
if (typeof r !== 'string' || this.normalize === false) {
63-
return r;
64+
if (typeof result === 'string') {
65+
if (this.normalize !== false) {
66+
result = result
67+
// eslint-disable-next-line no-control-regex
68+
.replace(/[\x00-\x09\x0B\x1F\x7F-\x9F]/g, ' ')
69+
.replace(/(?<=\s) /g, '')
70+
.replace(/ (?=\s)/g, '')
71+
.replace(/\s+$|^\s+/g, '');
72+
}
73+
if (this.lowercase === true) {
74+
result = result.toLowerCase();
75+
}
6476
}
65-
return r
66-
// eslint-disable-next-line no-control-regex
67-
.replace(/[\x00-\x09\x0B\x1F\x7F-\x9F]/g, ' ')
68-
.replace(/(?<=\s) /g, '')
69-
.replace(/ (?=\s)/g, '')
70-
.replace(/\s+$|^\s+/g, '');
77+
return result;
7178
}
7279
}
7380

src/param/email.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import RegEx from './regex.js';
22

33
class Email extends RegEx {
44
constructor(name, position, opts) {
5-
super(name, position, { ...opts, regex: /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/ });
5+
super(name, position, {
6+
...opts,
7+
regex: /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/,
8+
lowercase: true
9+
});
610
}
711
}
812
export default Email;

test/param/email.spec.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { expect } from 'chai';
2+
import { describe } from 'node-tdd';
3+
import { Api } from '../../src/index.js';
4+
5+
const api = Api();
6+
7+
describe('Testing Email Parameter', () => {
8+
describe('Testing query param', () => {
9+
let queryParam;
10+
before(() => {
11+
queryParam = api.Email('value', 'query');
12+
});
13+
14+
it('Testing valid query parameter', () => {
15+
expect(queryParam.get({
16+
queryStringParameters: {
17+
value: 'TEST@test.ca'
18+
}
19+
})).to.equal('test@test.ca');
20+
});
21+
22+
it('Testing invalid query parameter (rejected string)', () => {
23+
expect(() => queryParam.get({
24+
queryStringParameters: {
25+
value: ''
26+
}
27+
})).to.throw('Invalid Value for query-Parameter "value" provided.');
28+
});
29+
});
30+
31+
describe('Testing json param', () => {
32+
let jsonParam;
33+
before(() => {
34+
jsonParam = api.Email('value', 'json');
35+
});
36+
37+
it('Testing valid json parameter', () => {
38+
expect(jsonParam.get({
39+
body: {
40+
value: 'TEST@test.ca'
41+
}
42+
})).to.equal('test@test.ca');
43+
});
44+
45+
it('Testing invalid json parameter (rejected string)', () => {
46+
expect(() => jsonParam.get({
47+
body: {
48+
value: 'undefined'
49+
}
50+
})).to.throw('Invalid Value for json-Parameter "value" provided.');
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)