From 62f86455e3bc6ed2bd0364e97cca94ef51ce240e Mon Sep 17 00:00:00 2001 From: Daniil Lohvinov Date: Tue, 24 Oct 2023 18:18:29 +0300 Subject: [PATCH] 23.12.31: ws validator [WTEL-3986] --- package.json | 2 +- src/validators/websocketValidator/Readme.md | 7 +++++ .../__tests__/websocketValidator.spec.js | 28 +++++++++++++++++++ .../websocketValidator/websocketValidator.js | 2 ++ styleguide.config.js | 22 ++++++++++----- 5 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 src/validators/websocketValidator/Readme.md create mode 100644 src/validators/websocketValidator/__tests__/websocketValidator.spec.js create mode 100644 src/validators/websocketValidator/websocketValidator.js diff --git a/package.json b/package.json index 9999dffe..f7116cbf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@webitel/ui-sdk", - "version": "23.12.30", + "version": "23.12.31", "private": false, "scripts": { "build": "vue-cli-service build --target lib --name ui-sdk ./src/install.js", diff --git a/src/validators/websocketValidator/Readme.md b/src/validators/websocketValidator/Readme.md new file mode 100644 index 00000000..6c3059ea --- /dev/null +++ b/src/validators/websocketValidator/Readme.md @@ -0,0 +1,7 @@ +Validator for WebSocket URL string +```javascript +import websocketValidator from '@webitel/ui-sdk/src/validators/websocketValidator/websocketValidator'; + +websocketValidator('wss://example.com') // true +websocketValidator('krokoziabra') // false +``` diff --git a/src/validators/websocketValidator/__tests__/websocketValidator.spec.js b/src/validators/websocketValidator/__tests__/websocketValidator.spec.js new file mode 100644 index 00000000..503dd03a --- /dev/null +++ b/src/validators/websocketValidator/__tests__/websocketValidator.spec.js @@ -0,0 +1,28 @@ +import websocketValidator from '../websocketValidator'; + +describe('websocketValidator', () => { + it('truthy case 1: localhost ws', () => { + expect(websocketValidator('ws://localhost:8080')).toBe(true); + }); + it('truthy case 2: localhost wss', () => { + expect(websocketValidator('wss://localhost:8080')).toBe(true); + }); + it('truthy case 3: ip ws', () => { + expect(websocketValidator('ws://10.10.10.10:8080')).toBe(true); + }); + it('truthy case 4: dns name', () => { + expect(websocketValidator('wss://example.com')).toBe(true); + }); + it('truthy case 5: dns name', () => { + expect(websocketValidator('wss://example.com/ws')).toBe(true); + }); + it('truthy case 6: dns name', () => { + expect(websocketValidator('wss://socket.socket.com/v3/channel_123?api_key=123')).toBe(true); + }); + it('falsy case 1: empty string', () => { + expect(websocketValidator('')).toBe(false); + }); + it('falsy case 2: ws+ws', () => { + expect(websocketValidator('wss://example.com/wswss://example.com/ws')).toBe(false); + }); +}); diff --git a/src/validators/websocketValidator/websocketValidator.js b/src/validators/websocketValidator/websocketValidator.js new file mode 100644 index 00000000..19470583 --- /dev/null +++ b/src/validators/websocketValidator/websocketValidator.js @@ -0,0 +1,2 @@ +// That's strange but i haven't found any npm package with websocket validator +export default (value) => (/^(wss?:\/\/)([0-9]{1,3}(?:\.[0-9]{1,3}){3}|[^\/]+)(\:[0-9]{1,5})?(.*)$/i).test(value); diff --git a/styleguide.config.js b/styleguide.config.js index d90ecaac..51385acb 100644 --- a/styleguide.config.js +++ b/styleguide.config.js @@ -19,13 +19,21 @@ module.exports = { // StyleGuideRenderer: path.join(__dirname, 'styleguide/components/style-guide'), LogoRenderer: path.join(__dirname, 'styleguide/components/logo'), }, - // defaultExample: true, - // sections: [ - // { - // name: 'First Section', - // components: 'src/components/**/[A-Z]*.vue' - // } - // ], + sections: [ + { + name: 'Components', + components: 'src/components/**/*.vue', + }, + { + name: 'Validators', + sections: [ + { + name: 'Websocket Validator', + content: 'src/validators/websocketValidator/Readme.md', + }, + ], + }, + ], // eslint-disable-next-line global-require,import/extensions // webpackConfig: { ...output /* Custom config options */ }, // webpackConfig: {