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/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: { 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/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", diff --git a/index.html b/index.html index dcda87e..64796a3 100644 --- a/index.html +++ b/index.html @@ -140,4 +140,5 @@ StartWebClient(); +