From 93184e8e35471e2e90bb2fa94a8b2fda010ab776 Mon Sep 17 00:00:00 2001 From: RDW Date: Wed, 10 Nov 2021 12:53:27 +0100 Subject: [PATCH 1/3] Core: Add global validation builtins to streamline error handling This should likely replace the C_Validation API, since validation is a low-level and cross-cutting concern, and not a high-level engine functionality. --- Config/eslint.json | 2 ++ Core/Builtins/Validation.js | 12 ++++++++++++ index.html | 1 + 3 files changed, 15 insertions(+) create mode 100644 Core/Builtins/Validation.js diff --git a/Config/eslint.json b/Config/eslint.json index 5526eb9..a23575e 100644 --- a/Config/eslint.json +++ b/Config/eslint.json @@ -88,6 +88,8 @@ "Table": "writable", "TerrainMap": "writable", "UIParent": "writable", + "validateString": "writable", + "validateNumber": "writable", "Vector3D": "writable", "ViewportContainer": "writable", "WebClient": "writable", diff --git a/Core/Builtins/Validation.js b/Core/Builtins/Validation.js new file mode 100644 index 0000000..678c3fc --- /dev/null +++ b/Core/Builtins/Validation.js @@ -0,0 +1,12 @@ +function validateString(objectToTypecheck, errorMessage) { + // In JavaScript, Strings can be objects or literals. Perhaps they could also be flying dinosaurs? + const isString = typeof objectToTypecheck === "string" || objectToTypecheck instanceof String; + if (!isString) throw new TypeError(errorMessage); +} + +function validateNumber(objectToTypecheck, errorMessage) { + const isNumber = typeof objectToTypecheck === "number" || objectToTypecheck instanceof Number; + // NaN is technically a "number" value, but can't be checked normally, because JavaScript is awesome like that + const isNaN = objectToTypecheck !== objectToTypecheck; // isNaN() should also work, except for when it doesn't... + if (!isNumber || isNaN) throw new TypeError(errorMessage); +} diff --git a/index.html b/index.html index dcda87e..64796a3 100644 --- a/index.html +++ b/index.html @@ -140,4 +140,5 @@ StartWebClient(); + From ac3c0b464e8f7469ffbc4be70821d06ff6f08238 Mon Sep 17 00:00:00 2001 From: RDW Date: Wed, 17 Nov 2021 14:07:36 +0100 Subject: [PATCH 2/3] Tests: Add a basic test suite for the builtin validators --- Tests/Builtins/Validators.js | 9 +++++++++ Tests/run-renderer-tests.js | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Tests/Builtins/Validators.js diff --git a/Tests/Builtins/Validators.js b/Tests/Builtins/Validators.js new file mode 100644 index 0000000..a86c625 --- /dev/null +++ b/Tests/Builtins/Validators.js @@ -0,0 +1,9 @@ +describe("Validators", () => { + let exportedApiSurface = ["validateString", "validateNumber"]; + + exportedApiSurface.forEach((namedExport) => { + it("should export function " + namedExport, () => { + assertEquals(typeof window[namedExport], "function"); + }); + }); +}); diff --git a/Tests/run-renderer-tests.js b/Tests/run-renderer-tests.js index 619f325..8f3b82c 100644 --- a/Tests/run-renderer-tests.js +++ b/Tests/run-renderer-tests.js @@ -1,6 +1,6 @@ const testSuites = { SharedConstants: ["SharedConstants/Paths.js"], - Builtins: ["Builtins/Assertions.js", "Builtins/LocalCacheTests.js"], + Builtins: ["Builtins/Assertions.js", "Builtins/LocalCacheTests.js", "Builtins/Validators.js"], C_Settings: [ "API/C_Settings/validate.js", "API/C_Settings/validateDefaultSettings.js", From c65933cd82b64217b3f628430f3b39e60c0fc7bc Mon Sep 17 00:00:00 2001 From: RDW Date: Wed, 17 Nov 2021 14:18:22 +0100 Subject: [PATCH 3/3] Docs: Mark the C_Validation API as deprecated --- Core/APIs/C_Validation.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Core/APIs/C_Validation.js b/Core/APIs/C_Validation.js index a87a2e3..49389d7 100644 --- a/Core/APIs/C_Validation.js +++ b/Core/APIs/C_Validation.js @@ -1,5 +1,6 @@ var format = require("util").format; +// @deprecated Validators should be global builtins, since APIs are intended to provide high-level engine functionality const C_Validation = { // TODO: Move to a proper schema file addonMetadata: {