From a9dc316422f4b686efd84b135647377d5f3e8d3a Mon Sep 17 00:00:00 2001 From: Ludovic Elice Date: Mon, 4 Oct 2021 12:16:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix:=20testLinkedInUrlFormat=20a?= =?UTF-8?q?dd=20more=20restrictions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/__tests__/fieldValidators.js | 60 +++++++++++++++++++++++--------- src/fieldValidators.js | 2 +- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 3ea437a..9841c5e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kilix/functional-validation", - "version": "2.5.0", + "version": "2.5.1", "description": "Lightweight and customisable validation, built through composition", "main": "lib/index.js", "scripts": { diff --git a/src/__tests__/fieldValidators.js b/src/__tests__/fieldValidators.js index d9a8ea9..d92f6ee 100644 --- a/src/__tests__/fieldValidators.js +++ b/src/__tests__/fieldValidators.js @@ -157,26 +157,54 @@ it('testEmailFormat', () => { expect(actual).toBe(expected); }); -it('testLinkedInUrlFormat', () => { - let actual = testLinkedInUrlFormat('https://www.linkedin.com/in/url-test/'); - let expected = null; - expect(actual).toBe(expected); +describe('testLinkedInUrlFormat', () => { + it('Should return notLinkedInUrl error message if param is not an url', () => { + const actual = testLinkedInUrlFormat('Bob'); + expect(actual).toBe('notLinkedInUrl'); + }); + it('Should return notLinkedInUrl error message if url has not http', () => { + const actual = testLinkedInUrlFormat('www.linkedin.com/in/url-test/'); + expect(actual).toBe('notLinkedInUrl'); + }); + it('Should return notLinkedInUrl error message if http is badly written', () => { + let actual = testLinkedInUrlFormat('htps://www.linkedin.com/in/url-test/'); + expect(actual).toBe('notLinkedInUrl'); - actual = testLinkedInUrlFormat('Bob'); - expected = 'notLinkedInUrl'; - expect(actual).toBe(expected); + actual = testLinkedInUrlFormat('https//www.linkedin.com/in/url-test/'); + expect(actual).toBe('notLinkedInUrl'); - actual = testLinkedInUrlFormat(null); - expected = null; - expect(actual).toBe(expected); + actual = testLinkedInUrlFormat('https:www.linkedin.com/in/url-test/'); + expect(actual).toBe('notLinkedInUrl'); + }); + it('Should return notLinkedInUrl error message if dns is different of linkedIn dns', () => { + let actual = testLinkedInUrlFormat('https://www.linkedinsss.com/in/url-test/'); + expect(actual).toBe('notLinkedInUrl'); - actual = testLinkedInUrlFormat('https://www.linkedgin.com/in/url-test/'); - expected = 'notLinkedInUrl'; - expect(actual).toBe(expected); + actual = testLinkedInUrlFormat('https://wwwddd.linkedin.com/in/url-test/'); + expect(actual).toBe('notLinkedInUrl'); - actual = testLinkedInUrlFormat('https://www.linkedin.com/on/url-test/'); - expected = 'notLinkedInUrl'; - expect(actual).toBe(expected); + actual = testLinkedInUrlFormat('https://www.linkedin.fr/in/url-test/'); + expect(actual).toBe('notLinkedInUrl'); + }); + it('Should return notLinkedInUrl error message if url has not /in/', () => { + const actual = testLinkedInUrlFormat('https://www.linkedin.com/on/url-test/'); + expect(actual).toBe('notLinkedInUrl'); + }); + it('Should return null if url format is correct', () => { + // with number in end of the url + let actual = testLinkedInUrlFormat('https://www.linkedin.com/in/99erest/'); + expect(actual).toBe(null); + + // without www. + actual = testLinkedInUrlFormat('https://linkedin.com/in/url-test/'); + expect(actual).toBe(null); + + // with special chars in end of the url + actual = testLinkedInUrlFormat( + 'https://www.linkedin.com/in/%E2?9A=9B&+_#-test-url-716a96119/', + ); + expect(actual).toBe(null); + }); }); describe('checkMaxLength', () => { diff --git a/src/fieldValidators.js b/src/fieldValidators.js index 64cb512..29ddf7e 100644 --- a/src/fieldValidators.js +++ b/src/fieldValidators.js @@ -86,7 +86,7 @@ const testEmailFormat = (value: ?string) => { const testLinkedInUrlFormat = (value: ?string) => { if (!value) return null; - const regexp = /(https?)?:?(\/\/)?(([w]{3}||\w\w)\.)?linkedin.com\/in\//; // eslint-disable-line no-control-regex + const regexp = /(https?):(\/\/)(([w]{3}||\w\w)\.)?linkedin.com\/in\/.+/; // eslint-disable-line no-control-regex return regexp.test(value.trim()) ? null : 'notLinkedInUrl'; };