Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Config/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
"Table": "writable",
"TerrainMap": "writable",
"UIParent": "writable",
"validateString": "writable",
"validateNumber": "writable",
"Vector3D": "writable",
"ViewportContainer": "writable",
"WebClient": "writable",
Expand Down
1 change: 1 addition & 0 deletions Core/APIs/C_Validation.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
12 changes: 12 additions & 0 deletions Core/Builtins/Validation.js
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 9 additions & 0 deletions Tests/Builtins/Validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe("Validators", () => {
let exportedApiSurface = ["validateString", "validateNumber"];

exportedApiSurface.forEach((namedExport) => {
it("should export function " + namedExport, () => {
assertEquals(typeof window[namedExport], "function");
});
});
});
2 changes: 1 addition & 1 deletion Tests/run-renderer-tests.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@
StartWebClient();
</script>
</body>
<script src="Core/Builtins/Validation.js"></script>
</html>