Skip to content

Commit c3e134e

Browse files
author
Ovidiu Barabula
committed
feat(util): add helper for function parameter validation
1 parent 2746080 commit c3e134e

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/util/utility-functions.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ chai.use(chaiAsPromised);
55
import { assert, expect } from 'chai';
66
import 'mocha';
77
import { stdout } from 'test-console';
8-
import { ERRORS, getFnName, hasNested, limitFn, range, retry, sleep } from './utility-functions';
8+
import {
9+
ERRORS,
10+
getFnName,
11+
hasNested,
12+
limitFn,
13+
range,
14+
required,
15+
retry,
16+
sleep,
17+
} from './utility-functions';
918

1019
describe('Utility Functions', () => {
1120
describe('hasNested()', () => {
@@ -319,4 +328,21 @@ describe('Utility Functions', () => {
319328
}).timeout(5000);
320329
});
321330
});
331+
332+
333+
describe('required()', () => {
334+
it('throws an error with custom message when called', () => {
335+
assert.throws(() => required('My custom error message'), 'My custom error message');
336+
});
337+
338+
339+
it('throws when called without <message> parameter', () => {
340+
assert.throws(() => required(), ERRORS.REQUIRED_NEEDS_MESSAGE);
341+
});
342+
343+
344+
it('throws when called with non-string <message> parameter', () => {
345+
assert.throws(() => required(1), ERRORS.REQUIRED_NEEDS_MESSAGE);
346+
});
347+
});
322348
});

src/util/utility-functions.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export const ERRORS = {
3333
RETRY_NEEDS_A_FUNCTION: 'retry() requires first argument <fn> to be of type \'function\'',
3434
RETRY_NEEDS_OPTIONS_TO_BE_OBJECT: 'retry() requires second argument <options> to be of type \'object\'',
3535
RETRY_RETRIES_CANNOT_BE_ZERO: 'retry() option <retries> cannot be 0',
36+
37+
// required()
38+
REQUIRED_NEEDS_MESSAGE: 'required() requires first argument <message> of type \'string\'',
3639
};
3740

3841

@@ -246,3 +249,19 @@ export async function retry(
246249
return reject(errors[errors.length - 1]);
247250
});
248251
}
252+
253+
254+
/**
255+
* Helper for function parameter validation
256+
* This is useful for function parameter validation by assigning it as a default value
257+
* If no value will be passed the function will be called automatically
258+
* Usage: function foo(bar = required('<bar> is required')) { ... }
259+
* @param message Custom error message
260+
*/
261+
export function required<T>(message: string): T {
262+
if (typeof message === 'undefined' || typeof message !== 'string') {
263+
throw new Error(ERRORS.REQUIRED_NEEDS_MESSAGE);
264+
}
265+
266+
throw new Error(message);
267+
}

types/util/utility-functions.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export declare const ERRORS: {
1414
RETRY_NEEDS_A_FUNCTION: string;
1515
RETRY_NEEDS_OPTIONS_TO_BE_OBJECT: string;
1616
RETRY_RETRIES_CANNOT_BE_ZERO: string;
17+
REQUIRED_NEEDS_MESSAGE: string;
1718
};
1819
/**
1920
* Check object for path
@@ -55,3 +56,11 @@ export declare function retry(fn: AnyFunction, options?: {
5556
logChannel?: string;
5657
logNamespace?: string;
5758
}): Promise<any>;
59+
/**
60+
* Helper for function parameter validation
61+
* This is useful for function parameter validation by assigning it as a default value
62+
* If no value will be passed the function will be called automatically
63+
* Usage: function foo(bar = required('<bar> is required')) { ... }
64+
* @param message Custom error message
65+
*/
66+
export declare function required<T>(message: string): T;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy