From f6aa6f7979dae5a44ff4597b7338027c5aab869a Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Tue, 5 Oct 2021 15:29:14 +0300 Subject: [PATCH] fix: schema for web socket server type (#3913) --- bin/cli-flags.js | 17 ++++++++--- lib/options.json | 14 ++++++++-- .../validate-options.test.js.snap.webpack4 | 28 +++++++++++++++++-- .../validate-options.test.js.snap.webpack5 | 28 +++++++++++++++++-- test/validate-options.test.js | 3 ++ 5 files changed, 80 insertions(+), 10 deletions(-) diff --git a/bin/cli-flags.js b/bin/cli-flags.js index 55e36d7bc4..f755b96309 100644 --- a/bin/cli-flags.js +++ b/bin/cli-flags.js @@ -297,21 +297,30 @@ module.exports = { "web-socket-server": { configs: [ { - type: "enum", - values: [false, "sockjs", "ws"], - multiple: false, description: "Allows to set web socket server and options (by default 'ws').", + multiple: false, path: "webSocketServer", + type: "enum", + values: [false], }, { - type: "string", + description: + "Allows to set web socket server and options (by default 'ws').", multiple: false, + path: "webSocketServer", + type: "enum", + values: ["sockjs", "ws"], + }, + { description: "Allows to set web socket server and options (by default 'ws').", + multiple: false, path: "webSocketServer", + type: "string", }, ], + description: "Allows to set web socket server and options (by default 'ws').", simpleType: "string", diff --git a/lib/options.json b/lib/options.json index f1c01e5d91..8b1834294a 100644 --- a/lib/options.json +++ b/lib/options.json @@ -790,8 +790,18 @@ "description": "Allows to set web socket server and options (by default 'ws').", "link": "https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver" }, + "WebSocketServerType": { + "enum": ["sockjs", "ws"] + }, "WebSocketServerEnum": { - "enum": [false, "sockjs", "ws"] + "anyOf": [ + { + "enum": [false] + }, + { + "$ref": "#/definitions/WebSocketServerType" + } + ] }, "WebSocketServerFunction": { "instanceof": "Function" @@ -805,7 +815,7 @@ "type": { "anyOf": [ { - "$ref": "#/definitions/WebSocketServerEnum" + "$ref": "#/definitions/WebSocketServerType" }, { "$ref": "#/definitions/WebSocketServerString" diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack4 b/test/__snapshots__/validate-options.test.js.snap.webpack4 index 79c47d8241..93171a058e 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack4 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack4 @@ -721,6 +721,22 @@ exports[`options validate should throw an error on the "webSocketServer" option object { type?, options? }" `; +exports[`options validate should throw an error on the "webSocketServer" option with '{"type":false}' value 1`] = ` +"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. + - options.webSocketServer should be one of these: + false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? } + -> Allows to set web socket server and options (by default 'ws'). + -> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver + Details: + * options.webSocketServer.type should be one of these: + \\"sockjs\\" | \\"ws\\" | non-empty string | function + Details: + * options.webSocketServer.type should be one of these: + \\"sockjs\\" | \\"ws\\" + * options.webSocketServer.type should be a non-empty string. + * options.webSocketServer.type should be an instance of function." +`; + exports[`options validate should throw an error on the "webSocketServer" option with '{"type":true}' value 1`] = ` "ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. - options.webSocketServer should be one of these: @@ -729,10 +745,10 @@ exports[`options validate should throw an error on the "webSocketServer" option -> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver Details: * options.webSocketServer.type should be one of these: - false | \\"sockjs\\" | \\"ws\\" | non-empty string | function + \\"sockjs\\" | \\"ws\\" | non-empty string | function Details: * options.webSocketServer.type should be one of these: - false | \\"sockjs\\" | \\"ws\\" + \\"sockjs\\" | \\"ws\\" * options.webSocketServer.type should be a non-empty string. * options.webSocketServer.type should be an instance of function." `; @@ -746,6 +762,10 @@ exports[`options validate should throw an error on the "webSocketServer" option Details: * options.webSocketServer should be one of these: false | \\"sockjs\\" | \\"ws\\" + Details: + * options.webSocketServer should be false. + * options.webSocketServer should be one of these: + \\"sockjs\\" | \\"ws\\" * options.webSocketServer should be a non-empty string. * options.webSocketServer should be an instance of function. * options.webSocketServer should be an object: @@ -761,6 +781,10 @@ exports[`options validate should throw an error on the "webSocketServer" option Details: * options.webSocketServer should be one of these: false | \\"sockjs\\" | \\"ws\\" + Details: + * options.webSocketServer should be false. + * options.webSocketServer should be one of these: + \\"sockjs\\" | \\"ws\\" * options.webSocketServer should be a non-empty string. * options.webSocketServer should be an instance of function. * options.webSocketServer should be an object: diff --git a/test/__snapshots__/validate-options.test.js.snap.webpack5 b/test/__snapshots__/validate-options.test.js.snap.webpack5 index 79c47d8241..93171a058e 100644 --- a/test/__snapshots__/validate-options.test.js.snap.webpack5 +++ b/test/__snapshots__/validate-options.test.js.snap.webpack5 @@ -721,6 +721,22 @@ exports[`options validate should throw an error on the "webSocketServer" option object { type?, options? }" `; +exports[`options validate should throw an error on the "webSocketServer" option with '{"type":false}' value 1`] = ` +"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. + - options.webSocketServer should be one of these: + false | \\"sockjs\\" | \\"ws\\" | non-empty string | function | object { type?, options? } + -> Allows to set web socket server and options (by default 'ws'). + -> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver + Details: + * options.webSocketServer.type should be one of these: + \\"sockjs\\" | \\"ws\\" | non-empty string | function + Details: + * options.webSocketServer.type should be one of these: + \\"sockjs\\" | \\"ws\\" + * options.webSocketServer.type should be a non-empty string. + * options.webSocketServer.type should be an instance of function." +`; + exports[`options validate should throw an error on the "webSocketServer" option with '{"type":true}' value 1`] = ` "ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema. - options.webSocketServer should be one of these: @@ -729,10 +745,10 @@ exports[`options validate should throw an error on the "webSocketServer" option -> Read more at https://webpack.js.org/configuration/dev-server/#devserverwebsocketserver Details: * options.webSocketServer.type should be one of these: - false | \\"sockjs\\" | \\"ws\\" | non-empty string | function + \\"sockjs\\" | \\"ws\\" | non-empty string | function Details: * options.webSocketServer.type should be one of these: - false | \\"sockjs\\" | \\"ws\\" + \\"sockjs\\" | \\"ws\\" * options.webSocketServer.type should be a non-empty string. * options.webSocketServer.type should be an instance of function." `; @@ -746,6 +762,10 @@ exports[`options validate should throw an error on the "webSocketServer" option Details: * options.webSocketServer should be one of these: false | \\"sockjs\\" | \\"ws\\" + Details: + * options.webSocketServer should be false. + * options.webSocketServer should be one of these: + \\"sockjs\\" | \\"ws\\" * options.webSocketServer should be a non-empty string. * options.webSocketServer should be an instance of function. * options.webSocketServer should be an object: @@ -761,6 +781,10 @@ exports[`options validate should throw an error on the "webSocketServer" option Details: * options.webSocketServer should be one of these: false | \\"sockjs\\" | \\"ws\\" + Details: + * options.webSocketServer should be false. + * options.webSocketServer should be one of these: + \\"sockjs\\" | \\"ws\\" * options.webSocketServer should be a non-empty string. * options.webSocketServer should be an instance of function. * options.webSocketServer should be an object: diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 7bc528b944..f79893f24b 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -448,6 +448,9 @@ const tests = { failure: [ null, true, + { + type: false, + }, { notAnOption: true, },