topkat-utils 1.1.14 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +9 -13
- package/dist/index.js.map +1 -1
- package/dist/src/array-utils.js +23 -45
- package/dist/src/array-utils.js.map +1 -1
- package/dist/src/clean-stack-trace.d.ts +1 -0
- package/dist/src/clean-stack-trace.js +46 -0
- package/dist/src/clean-stack-trace.js.map +1 -0
- package/dist/src/config.d.ts +1 -1
- package/dist/src/config.js +5 -16
- package/dist/src/config.js.map +1 -1
- package/dist/src/date-utils.js +91 -141
- package/dist/src/date-utils.js.map +1 -1
- package/dist/src/env-utils.js +4 -4
- package/dist/src/env-utils.js.map +1 -1
- package/dist/src/error-utils.d.ts +23 -2
- package/dist/src/error-utils.js +95 -105
- package/dist/src/error-utils.js.map +1 -1
- package/dist/src/isset.js +2 -6
- package/dist/src/isset.js.map +1 -1
- package/dist/src/logger-utils.d.ts +1 -1
- package/dist/src/logger-utils.js +139 -268
- package/dist/src/logger-utils.js.map +1 -1
- package/dist/src/loop-utils.d.ts +1 -1
- package/dist/src/loop-utils.js +35 -121
- package/dist/src/loop-utils.js.map +1 -1
- package/dist/src/math-utils.js +8 -24
- package/dist/src/math-utils.js.map +1 -1
- package/dist/src/mongo-utils.js +6 -20
- package/dist/src/mongo-utils.js.map +1 -1
- package/dist/src/object-utils.d.ts +1 -1
- package/dist/src/object-utils.js +74 -114
- package/dist/src/object-utils.js.map +1 -1
- package/dist/src/regexp-utils.js +6 -7
- package/dist/src/regexp-utils.js.map +1 -1
- package/dist/src/remove-circular-json-stringify.js +4 -5
- package/dist/src/remove-circular-json-stringify.js.map +1 -1
- package/dist/src/string-utils.d.ts +1 -1
- package/dist/src/string-utils.js +49 -107
- package/dist/src/string-utils.js.map +1 -1
- package/dist/src/tests-utils.d.ts +1 -1
- package/dist/src/tests-utils.js +19 -22
- package/dist/src/tests-utils.js.map +1 -1
- package/dist/src/timer-utils.js +37 -121
- package/dist/src/timer-utils.js.map +1 -1
- package/dist/src/transaction-utils.js +44 -129
- package/dist/src/transaction-utils.js.map +1 -1
- package/dist/src/types.d.ts +5 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/validation-utils.d.ts +1 -1
- package/dist/src/validation-utils.js +80 -134
- package/dist/src/validation-utils.js.map +1 -1
- package/dist/src/wtf-utils.js +35 -40
- package/dist/src/wtf-utils.js.map +1 -1
- package/index.ts +1 -1
- package/package.json +4 -4
- package/src/clean-stack-trace.ts +40 -0
- package/src/config.ts +1 -1
- package/src/date-utils.ts +5 -5
- package/src/error-utils.ts +86 -44
- package/src/logger-utils.ts +2 -2
- package/src/loop-utils.ts +1 -1
- package/src/object-utils.ts +6 -6
- package/src/string-utils.ts +1 -1
- package/src/tests-utils.ts +2 -2
- package/src/timer-utils.ts +2 -2
- package/src/{private/types.ts → types.ts} +0 -0
- package/src/validation-utils.ts +8 -8
- package/tsconfig.json +1 -1
- package/src/private/error-handler.ts +0 -21
package/src/validation-utils.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
//----------------------------------------
|
|
2
2
|
// VALIDATION UTILS
|
|
3
3
|
//----------------------------------------
|
|
4
|
-
import { dataValidationUtilErrorHandler } from "./private/error-handler"
|
|
5
4
|
import { isset } from "./isset"
|
|
6
5
|
import { isDateIsoOrObjectValid, isDateIntOrStringValid, isTimeStringValid } from "./date-utils"
|
|
7
6
|
import { asArray } from "./array-utils"
|
|
8
7
|
import { configFn } from "./config"
|
|
9
8
|
import { isEmpty } from "./is-empty"
|
|
9
|
+
import { DescriptiveError } from "./error-utils"
|
|
10
10
|
import { removeCircularJSONstringify } from "./remove-circular-json-stringify"
|
|
11
11
|
|
|
12
12
|
export type BaseTypes = 'objectId' | 'dateInt6' | 'dateInt' | 'dateInt8' | 'dateInt12' | 'time' | 'humanReadableTimestamp' | 'date' | 'dateObject' | 'array' | 'object' | 'buffer' | 'string' | 'function' | 'boolean' | 'number' | 'bigint' | 'year' | 'email' | 'any'
|
|
@@ -22,7 +22,7 @@ export function checkAllObjectValuesAreEmpty(o) { return Object.values(o).every(
|
|
|
22
22
|
|
|
23
23
|
/** Throw an error in case data passed is not a valid ctx */
|
|
24
24
|
export function checkCtxIntegrity(ctx) {
|
|
25
|
-
if (!isset(ctx) || !isset(ctx.user)) throw new
|
|
25
|
+
if (!isset(ctx) || !isset(ctx.user)) throw new DescriptiveError('ctxNotSet', { code: 500 })
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
@@ -90,7 +90,7 @@ export type ValidatorObject = {
|
|
|
90
90
|
}
|
|
91
91
|
export function validator(...paramsToValidate: ValidatorObject[]) {
|
|
92
92
|
const errArray = validatorReturnErrArray(...paramsToValidate)
|
|
93
|
-
if (errArray.length) throw new
|
|
93
|
+
if (errArray.length) throw new DescriptiveError(...errArray)
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
|
|
@@ -107,13 +107,13 @@ export function isValid(...paramsToValidate) {
|
|
|
107
107
|
*/
|
|
108
108
|
export function isType(value, type: BaseTypes) { return isValid({ name: 'Is type check', value, type }) }
|
|
109
109
|
|
|
110
|
-
export function validatorReturnErrArray(...paramsToValidate: ValidatorObject[]): [string?,
|
|
110
|
+
export function validatorReturnErrArray(...paramsToValidate: ValidatorObject[]): [string?, object?] {
|
|
111
111
|
let paramsFormatted: ValidatorObject[] = []
|
|
112
112
|
|
|
113
113
|
// support for multiple names with multiple values for one rule. Eg: {name: [{startDate:'20180101'}, {endDate:'20180101'}], type: 'dateInt8'}
|
|
114
114
|
paramsToValidate.forEach(param => {
|
|
115
115
|
if (typeof param !== 'object' || Array.isArray(param))
|
|
116
|
-
throw new
|
|
116
|
+
throw new DescriptiveError(`wrongTypeForDataValidatorArgument`, { code: 500, origin: 'Generic validator', expectedType: 'object', actualType: Array.isArray(param) ? 'array' : typeof param })
|
|
117
117
|
|
|
118
118
|
// parse => name: {myVar1: 'blah, myvar2: myvar2}
|
|
119
119
|
if (typeof param.name === 'object' && !Array.isArray(param.name))
|
|
@@ -127,12 +127,12 @@ export function validatorReturnErrArray(...paramsToValidate: ValidatorObject[]):
|
|
|
127
127
|
let optional = paramObj.optional || false
|
|
128
128
|
let emptyAllowed = optional || paramObj.emptyAllowed || false
|
|
129
129
|
if (paramObj.isset === false) paramObj.mustNotBeSet = true // ALIAS
|
|
130
|
-
const errMess = (msg, extraInfos = {}, errCode = 422): [string,
|
|
130
|
+
const errMess = (msg, extraInfos = {}, errCode = 422): [string, object] => [msg, { code: errCode, origin: 'Generic validator', varName: name, gotValue: isset(value) && isset(value.data) && isset(value.data.data) ? { ...value, data: 'Buffer' } : value, ...extraInfos }]
|
|
131
131
|
|
|
132
132
|
// accept syntax { 'myVar.var2': myVar.var2, ... }
|
|
133
133
|
if (!isset(name)) {
|
|
134
134
|
name = Object.keys(paramObj).find(param => !['in', 'eq', 'lte', 'gte', 'name', 'value', 'type', 'regexp', 'minLength', 'maxLength', 'optional', 'emptyAllowed', 'mustNotBeSet', 'includes', 'length'].includes(param))
|
|
135
|
-
if (isset(name)) value = paramObj[name]
|
|
135
|
+
if (isset(name)) value = paramObj[name]
|
|
136
136
|
}
|
|
137
137
|
// if nameString ends by $ sign it is optional
|
|
138
138
|
if (isset(name) && /.*\$$/.test(name)) {
|
|
@@ -183,7 +183,7 @@ export function validatorReturnErrArray(...paramsToValidate: ValidatorObject[]):
|
|
|
183
183
|
//...Object.keys(configFn().customTypes)
|
|
184
184
|
]
|
|
185
185
|
|
|
186
|
-
if (!allTypes.includes(type)) throw new
|
|
186
|
+
if (!allTypes.includes(type)) throw new DescriptiveError('typeDoNotExist', { code: 500, type })
|
|
187
187
|
|
|
188
188
|
const basicTypeCheck = {
|
|
189
189
|
objectId: val => /^[0-9a-fA-F-]{24,}$/.test(val), // "0c65940b-6b0c-4dd8-9c7a-7c5fe1ba907a"
|
package/tsconfig.json
CHANGED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export class dataValidationUtilErrorHandler extends Error {
|
|
2
|
-
fromDataValidation: boolean
|
|
3
|
-
code: number
|
|
4
|
-
extraInfos: object
|
|
5
|
-
msg: string
|
|
6
|
-
errorDescription: { [k: string]: any }
|
|
7
|
-
constructor(msg, code, extraInfos?) {
|
|
8
|
-
super(msg)
|
|
9
|
-
this.message = msg
|
|
10
|
-
this.msg = msg
|
|
11
|
-
this.name = `${code} ${msg}`
|
|
12
|
-
this.fromDataValidation = true // will be catched by express error handler
|
|
13
|
-
this.code = code
|
|
14
|
-
this.extraInfos = extraInfos
|
|
15
|
-
this.errorDescription = {
|
|
16
|
-
msg,
|
|
17
|
-
code,
|
|
18
|
-
...extraInfos,
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|