Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anti-pattern: (JS-0323) Fix #2039

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@
"engines": {
"node": ">=15.0.0"
}
}
}
20 changes: 15 additions & 5 deletions backend/src/utils/helpers/validation.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// eslint-disable-next-line import/prefer-default-export
export function validateType(key: string, value: any, type: string) {
export function validateType(key: string, value: unknown, type: string) {
if (value === undefined || value === null) return `${key} is required.`;
if (type === 'number' && (Number.isNaN(value) || value <= 0)) {
return `${key} must be a valid positive number.`;

if (type === 'number') {
if (typeof value !== 'number' || isNaN(value) || value <= 0) {
return `${key} must be a valid positive number.`;
}
}
if (type === 'boolean' && !['true', 'false', true, false].includes(value)) {
return `${key} must be true or false.`;
if (type === 'boolean') {
if (typeof value !== 'boolean') {
return `${key} must be true or false.`;
}
}
if (
type === 'string[]' &&
Expand All @@ -14,5 +19,10 @@ export function validateType(key: string, value: any, type: string) {
) {
return `${key} must be an array of non-empty strings.`;
}
if (type === 'string') {
if (typeof value !== 'string' || value.trim() === '') {
return `${key} must be a non-empty string.`;
}
}
return null;
}
2 changes: 1 addition & 1 deletion backend/test/unit/enrichment-service/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('enrichFromCache', () => {
{ id: '2', user_id: 'leadminer-1', email: '[email protected]' }
];

getCache.mockResolvedValue([{} as any]);
getCache.mockResolvedValue([{} as never]);

const result = await enrichFromCache(getCache, enrichmentsDB, contacts);

Expand Down
60 changes: 60 additions & 0 deletions backend/test/unit/validation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect, test} from '@jest/globals';
import { validateType } from '../../src/utils/helpers/validation';


/* TESTING EMPTY VALUE */
test('null/undefined value triggers error', () => {
expect(validateType('port', null, 'number')).toEqual('port is required.');
});


/* TESTING NUMBER VALIDATION */
test('accepts a positive number properly', () => {
expect(validateType('port', 8808, 'number')).toBeNull();
});

test('rejects a negative number', () => {
expect(validateType('port', -1, 'number')).toEqual('port must be a valid positive number.');
});

test('rejects 0', () => {
expect(validateType('port', 0, 'number')).toEqual('port must be a valid positive number.');
});

test('rejects a value that is not a number', () => {
expect(validateType('port', 'error', 'number')).toEqual('port must be a valid positive number.');
});


/* TESTING BOOLEAN VALIDATION */
test('accepts a true boolean properly', () => {
expect(validateType('secure', true, 'boolean')).toBeNull();
});

test('accepts a false boolean properly', () => {
expect(validateType('secure', false, 'boolean')).toBeNull();
});

test('rejects nonboolean values', () => {
expect(validateType('secure', 'error', 'boolean')).toEqual('secure must be true or false.');
});


/* TESTING STRING[] VALIDATION */
test('accepts a string[] properly', () => {
expect(validateType('email', ['t','e','s','t','@','e','m','a','i','l','.','c','o','m'], 'string[]')).toBeNull();
});

test('rejects a nonstring array value', () => {
expect(validateType('email', [1, 2, 3], 'string[]')).toEqual('email must be an array of non-empty strings.');
});


/* TESTING STRING VALIDATION */
test('accepts a string properly', () => {
expect(validateType('email', '[email protected]', 'string')).toBeNull();
});

test('rejects a nonstring value', () => {
expect(validateType('email', [1, 2, 3], 'string')).toEqual('email must be a non-empty string.');
});
2 changes: 1 addition & 1 deletion frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
"vue-tsc": "^2.2.0",
"convert-csv-to-json": "^2.50.0"
}
}
}
6 changes: 3 additions & 3 deletions frontend/src/components/Mining/StepperPanels/MinePanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ const totalEmails = computed<number>(() => {
if (source.value === 'boxes' && boxes.value[0]) {
return objectScan(['**.{total}'], {
joined: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
filterFn: ({ parent, property, value, context }: any) => {
filterFn: ({ parent, property, value, context }: never) => {
if (
property === 'total' &&
parent.key &&
Expand Down Expand Up @@ -215,7 +215,7 @@ onMounted(async () => {

try {
await $leadminerStore.fetchInbox();
} catch (error: any) {
} catch (error) {
if (error?.statusCode === 502 || error?.statusCode === 503) {
$stepper.prev();
throw error;
Expand Down
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
"devDependencies": {
"supabase": "^2.2.1"
}
}
}