Skip to content

Commit 260c991

Browse files
Harrison IfeanyichukwuHarrison Ifeanyichukwu
authored andcommitted
fix: fix typings
1 parent 830a5d0 commit 260c991

7 files changed

Lines changed: 55 additions & 54 deletions

File tree

src/@types/index.d.ts

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import {
2222
} from './rules/NumberRules';
2323
import RangeRule, { RangeOptions } from './rules/RangeRule';
2424
import ChoiceRule, { ChoiceOptions } from './rules/ChoiceRule';
25-
import BaseRule, { BaseOptions, SuccessOrErrorMessage } from './rules/BaseRule';
25+
import BaseRule, {
26+
BaseOptions,
27+
SuccessOrErrorMessage,
28+
DBCheck
29+
} from './rules/BaseRule';
2630
import {
2731
FileRule,
2832
ImageFileRule,
@@ -107,8 +111,6 @@ export type DataType =
107111
| 'document'
108112
| 'archive';
109113

110-
export type DBCheckType = 'exists' | 'notExists';
111-
112114
export type RequiredIf<F extends string> =
113115
| {
114116
if: 'checked' | 'notChecked';
@@ -168,25 +170,6 @@ export type Options<F extends string> =
168170
| PhoneNumberOptions<F>
169171
| PasswordOptions<F>;
170172

171-
export interface ModelDBCheck {
172-
if: DBCheckType;
173-
model: object;
174-
field?: string;
175-
query?: object;
176-
err?: string;
177-
}
178-
export interface CallbackDBCheck<F extends string> {
179-
if: DBCheckType;
180-
callback: (
181-
value: DataValue,
182-
index: number,
183-
data: Data<F>,
184-
handler: Handler<F>
185-
) => Promise<SuccessOrErrorMessage> | SuccessOrErrorMessage;
186-
err?: string;
187-
}
188-
export type DBCheck<F extends string> = CallbackDBCheck<F> | ModelDBCheck;
189-
190173
export type Rule<F extends string> =
191174
| BooleanRule<F>
192175
| CheckboxRule<F>

src/@types/rules/BaseRule.d.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1-
import { DataValue, RequiredIf, Filters, DBCheck, DataType, Data } from '..';
1+
import { DataValue, RequiredIf, Filters, DataType, Data } from '..';
22
import Handler from '../../Handler';
33

44
export type SuccessOrErrorMessage = boolean | string;
55

6+
export type DBCheckType = 'exists' | 'notExists';
7+
8+
export interface ModelDBCheck {
9+
if: DBCheckType;
10+
model: object;
11+
field?: string;
12+
query?: object;
13+
err?: string;
14+
}
15+
16+
export type DBCheckCallback<F extends string> = (
17+
value: DataValue,
18+
index: number,
19+
data: Data<F>,
20+
handler: Handler<F>
21+
) => Promise<SuccessOrErrorMessage> | SuccessOrErrorMessage;
22+
23+
export type DBCheck<F extends string> = DBCheckCallback<F> | ModelDBCheck;
24+
625
export interface ShouldMatchObject<F extends string> {
726
/**
827
* the target field

src/Common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { ErrorBag, DBCheck, Options } from './@types';
1+
import { ErrorBag, Options } from './@types';
22
import { isNumeric } from '@forensic-js/utils';
33
import { replaceCallback } from '@forensic-js/regex';
44
import StateException from './Exceptions/StateException';
5+
import { DBCheck } from './@types/rules/BaseRule';
56

67
export default class Common<F extends string = string> {
78
protected errors: ErrorBag<F> = {} as ErrorBag<F>;

src/DBChecker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Common from './Common';
2-
import { DataValue, ModelDBCheck, ResolvedRules, DataType } from './@types';
2+
import { DataValue, DataType } from './@types';
33
import { DB_MODELS } from './Constants';
44
import { pickValue, applyCase } from '@forensic-js/utils';
5+
import { ModelDBCheck } from './@types/rules/BaseRule';
56

67
export default class DBChecker<F extends string = string> extends Common<F> {
78
private dbModel: number;

src/Handler.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ import {
1212
Options,
1313
ErrorBag,
1414
RequiredIf,
15-
DBCheck,
16-
CallbackDBCheck,
17-
ModelDBCheck,
18-
DBCheckType,
1915
Data
2016
} from './@types';
2117
import { DB_MODELS } from './Constants';
@@ -65,13 +61,14 @@ import {
6561
ordinalize,
6662
capitalize
6763
} from 'inflection';
64+
import { DBCheckType, DBCheck, ModelDBCheck } from './@types/rules/BaseRule';
6865

6966
const globalConfig = {
7067
dbModel: DB_MODELS.NOSQL,
7168
dbCaseStyle: CASE_STYLES.CAMEL_CASE
7269
};
7370

74-
export default class Handler<F extends string = string, Exports = Data<F>> {
71+
export default class Handler<F extends string = string> {
7572
/**
7673
* supported database models
7774
*/
@@ -317,13 +314,8 @@ export default class Handler<F extends string = string, Exports = Data<F>> {
317314
index: number
318315
) {
319316
for (const check of checks) {
320-
if (isTypeOf<CallbackDBCheck<F>>('callback', check)) {
321-
const result = await check.callback(
322-
value,
323-
index,
324-
this.data,
325-
this as any
326-
);
317+
if (isCallable(check)) {
318+
const result = await check(value, index, this.data, this);
327319
if (result !== true) {
328320
return this.setError(
329321
field,
@@ -339,7 +331,7 @@ export default class Handler<F extends string = string, Exports = Data<F>> {
339331
required,
340332
field,
341333
value,
342-
check as ModelDBCheck,
334+
check,
343335
index
344336
);
345337
if (this.dbChecker.fails()) {
@@ -1239,8 +1231,8 @@ export default class Handler<F extends string = string, Exports = Data<F>> {
12391231
/**
12401232
* creates and returns a model instance, that can be exported
12411233
*/
1242-
model(): Model<F, Exports> {
1243-
return new Model<F, Exports>(this);
1234+
model(): Model<F> {
1235+
return new Model<F>(this);
12441236
}
12451237

12461238
/**

src/Model.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { expandProperty, applyCase } from '@forensic-js/utils';
22
import Handler from './Handler';
33
import { Data } from './@types';
44

5-
export default class Model<F extends string = string, Exports = Data<F>> {
5+
export default class Model<F extends string = string> {
66
private handler: Handler<F>;
77

88
private fieldsToSkip: string[] = [];
99

1010
private fieldsToRename: { [old: string]: string } = {};
1111

12-
constructor(handler: Handler<F, Exports>) {
12+
constructor(handler: Handler<F>) {
1313
this.handler = handler;
1414
}
1515

@@ -37,7 +37,9 @@ export default class Model<F extends string = string, Exports = Data<F>> {
3737
* @param fields object of field old name to new name value pairs
3838
*/
3939
renameFields(fields: { [oldName: string]: string }) {
40-
Object.keys(fields).forEach(oldName => (this.fieldsToRename[oldName] = fields[oldName]));
40+
Object.keys(fields).forEach(
41+
oldName => (this.fieldsToRename[oldName] = fields[oldName])
42+
);
4143
return this;
4244
}
4345

@@ -46,20 +48,29 @@ export default class Model<F extends string = string, Exports = Data<F>> {
4648
* @param target model to export data to
4749
* @param expandProperties boolean indicating if properties should be expanding
4850
*/
49-
export<T extends object>(target: T = {} as T, expandProperties: boolean = true): T & Exports {
51+
export<T extends object>(
52+
target: T = {} as T,
53+
expandProperties: boolean = true
54+
): T & Data<F> {
5055
const { handler, fieldsToSkip, fieldsToRename } = this;
5156
Object.keys(handler.data).forEach(field => {
5257
if (!fieldsToSkip.includes(field)) {
5358
const newName = fieldsToRename[field] || field;
5459
const value = handler.data[field];
5560

5661
if (expandProperties) {
57-
expandProperty(target, newName, value, undefined, handler.getDBCaseStyle());
62+
expandProperty(
63+
target,
64+
newName,
65+
value,
66+
undefined,
67+
handler.getDBCaseStyle()
68+
);
5869
} else {
5970
target[applyCase(newName, handler.getDBCaseStyle())] = value;
6071
}
6172
}
6273
});
63-
return target as T & Exports;
74+
return target as T & Data<F>;
6475
}
6576
}

tests/Handler.spec.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,16 +1402,10 @@ describe('Handler Module', function() {
14021402
const callback = jest.fn(async () => true);
14031403
handler.setRules({
14041404
firstName: {
1405-
checks: {
1406-
if: 'exists',
1407-
callback
1408-
}
1405+
checks: callback
14091406
},
14101407
email: {
1411-
checks: {
1412-
if: 'exists',
1413-
callback: async () => false
1414-
}
1408+
checks: async () => false
14151409
}
14161410
});
14171411

0 commit comments

Comments
 (0)