Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
added error msg for objects key --VERIFIED
Browse files Browse the repository at this point in the history
  • Loading branch information
knighttower committed Feb 7, 2024
1 parent c0f3031 commit a5f960b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/TypeCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const runBasicTest = (inputVal, tests) => {
const testResult = test(inputVal);

if (!testResult) {
typeErrorLogs.push({ value: inputVal, tests: String(tests), found: typeOf(inputVal) });
pushToErrorLogs(inputVal, tests);
}
return testResult;
});
Expand Down Expand Up @@ -72,10 +72,14 @@ class HandleObjects {
// '{key1: type, key2: type}'; // all keys
for (const k in this.inputObject) {
if (!this.testCollection.has(k)) {
pushToErrorLogs(
this.inputObject,
`Key: "${k}" not found in the test collection, or use the "any" (any:[type]) key test or "..." after the last key in the test collection {key1: type, key2: type, ...} to only test a few keys.`,
);
return false;
}
}
// when testOnly it will bypass this and check only those found in the test collection
// when testOnly, it will bypass this and check only those found in the test collection
// even if the test value has more keys
break;
}
Expand Down Expand Up @@ -207,17 +211,26 @@ function getSettings(input) {
*/
function typeError(inputVal) {
const errorLog = typeErrorLogs[typeErrorLogs.length - 1];

console.log('\n::::::::::::: Type error or not valid ::::::::::::::');
console.log('Input Value used: ', inputVal);
console.log('---> Value Found:', errorLog.found);
console.log('---> Test Permormed:', errorLog.tests);
//clean the array of error logs
typeErrorLogs.length = 0;
throw new Error(
`\n\n---------------------\nTypeCheck Error ---> The Type used is invalid: "${errorLog.value}". \n see logged error for details\n---------------------\n\n`,
`\n\n---------------------\nTypeCheck Error --->\n\n The value must not be of type (Type found) = "${errorLog.found}". \n\n The Type used is invalid for value: "${errorLog.value}". \n\n see logged error for details\n---------------------\n\n`,
);
}

function pushToErrorLogs(inputVal, tests) {
typeErrorLogs.push({
value: JSON.stringify(inputVal),
tests: JSON.stringify(tests),
found: typeOf(inputVal),
});
}

/**
* _TypeCheck
* @param {any} inputVal
Expand Down
6 changes: 6 additions & 0 deletions test/TypeCheck.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,9 @@ test('objects: {key: any}', () => {
assert.equal(_typeCheck({ y: 33, x: null }, '{y: number, any: number|null}').log().test(), true);
assert.equal(_typeCheck({ x: 'string', y: 10, z: 20 }, '{x: string, y: number, any: number}').test(), true);
});

test('array with objects: [{key: type, key: type}]', () => {
// console.log(typeCheck([{ x: 2, y: 10 }], '[{x: number}]').log());
assert.equal(_typeCheck([{ x: 2, y: 10 }], '[{x: number, ...}]').test(), true);
assert.equal(_typeCheck([{ x: 2, y: 10 }], '[{x: number, any: number}]').test(), true);
});

0 comments on commit a5f960b

Please sign in to comment.