diff --git a/src/param/_abstract.js b/src/param/_abstract.js index e0c78d6b..adb74008 100644 --- a/src/param/_abstract.js +++ b/src/param/_abstract.js @@ -15,7 +15,8 @@ class Abstract { required = true, nullable = false, normalize = true, - getter = null + getter = null, + lowercase = false } = {}) { assert(Object.keys(positionMapping).includes(position), `Unknown Parameter Position: ${position}`); assert( @@ -29,6 +30,7 @@ class Abstract { this.required = required; this.nullable = nullable; this.normalize = normalize; + this.lowercase = lowercase; this.getter = getter; this.type = null; } @@ -38,7 +40,7 @@ class Abstract { } get(event) { - const result = get(event, `${positionMapping[this.position]}.${ + let result = get(event, `${positionMapping[this.position]}.${ this.position === 'header' ? this.name.toLowerCase() : this.name @@ -56,18 +58,23 @@ class Abstract { value: result }); } - const r = this.getter !== null && ![undefined, null].includes(result) + result = this.getter !== null && ![undefined, null].includes(result) ? (params) => this.getter(result, params) : result; - if (typeof r !== 'string' || this.normalize === false) { - return r; + if (typeof result === 'string') { + if (this.normalize !== false) { + result = result + // eslint-disable-next-line no-control-regex + .replace(/[\x00-\x09\x0B\x1F\x7F-\x9F]/g, ' ') + .replace(/(?<=\s) /g, '') + .replace(/ (?=\s)/g, '') + .replace(/\s+$|^\s+/g, ''); + } + if (this.lowercase === true) { + result = result.toLowerCase(); + } } - return r - // eslint-disable-next-line no-control-regex - .replace(/[\x00-\x09\x0B\x1F\x7F-\x9F]/g, ' ') - .replace(/(?<=\s) /g, '') - .replace(/ (?=\s)/g, '') - .replace(/\s+$|^\s+/g, ''); + return result; } } diff --git a/src/param/email.js b/src/param/email.js index fcf86547..876a76c1 100644 --- a/src/param/email.js +++ b/src/param/email.js @@ -2,7 +2,11 @@ import RegEx from './regex.js'; class Email extends RegEx { constructor(name, position, opts) { - super(name, position, { ...opts, regex: /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/ }); + super(name, position, { + ...opts, + regex: /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/, + lowercase: true + }); } } export default Email; diff --git a/test/param/email.spec.js b/test/param/email.spec.js new file mode 100644 index 00000000..20cac284 --- /dev/null +++ b/test/param/email.spec.js @@ -0,0 +1,53 @@ +import { expect } from 'chai'; +import { describe } from 'node-tdd'; +import { Api } from '../../src/index.js'; + +const api = Api(); + +describe('Testing Email Parameter', () => { + describe('Testing query param', () => { + let queryParam; + before(() => { + queryParam = api.Email('value', 'query'); + }); + + it('Testing valid query parameter', () => { + expect(queryParam.get({ + queryStringParameters: { + value: 'TEST@test.ca' + } + })).to.equal('test@test.ca'); + }); + + it('Testing invalid query parameter (rejected string)', () => { + expect(() => queryParam.get({ + queryStringParameters: { + value: '' + } + })).to.throw('Invalid Value for query-Parameter "value" provided.'); + }); + }); + + describe('Testing json param', () => { + let jsonParam; + before(() => { + jsonParam = api.Email('value', 'json'); + }); + + it('Testing valid json parameter', () => { + expect(jsonParam.get({ + body: { + value: 'TEST@test.ca' + } + })).to.equal('test@test.ca'); + }); + + it('Testing invalid json parameter (rejected string)', () => { + expect(() => jsonParam.get({ + body: { + value: 'undefined' + } + })).to.throw('Invalid Value for json-Parameter "value" provided.'); + }); + }); +});