@naturalcycles/nodejs-lib 13.1.3 → 13.2.0
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/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/log/log.util.js +1 -1
- package/dist/script/runScript.js +2 -2
- package/dist/slack/slack.service.js +2 -2
- package/dist/string/{inspectAny.d.ts → inspect.d.ts} +9 -5
- package/dist/string/{inspectAny.js → inspect.js} +11 -7
- package/dist/validation/ajv/ajvSchema.js +1 -1
- package/dist/validation/joi/joi.validation.util.d.ts +6 -6
- package/dist/validation/joi/joi.validation.util.js +19 -21
- package/package.json +1 -1
- package/src/index.ts +1 -1
- package/src/log/log.util.ts +2 -2
- package/src/script/runScript.ts +2 -2
- package/src/slack/slack.service.ts +3 -3
- package/src/string/{inspectAny.ts → inspect.ts} +11 -6
- package/src/validation/ajv/ajvSchema.ts +2 -2
- package/src/validation/joi/joi.validation.util.ts +20 -21
package/dist/index.d.ts
CHANGED
|
@@ -58,7 +58,7 @@ export * from './stream/writable/writableVoid';
|
|
|
58
58
|
export * from './csv/csvWriter';
|
|
59
59
|
export * from './csv/csvReader';
|
|
60
60
|
export * from './csv/transformToCSV';
|
|
61
|
-
export * from './string/
|
|
61
|
+
export * from './string/inspect';
|
|
62
62
|
export * from './util/env.util';
|
|
63
63
|
export * from './util/lruMemoCache';
|
|
64
64
|
export * from './util/zip.util';
|
package/dist/index.js
CHANGED
|
@@ -62,7 +62,7 @@ tslib_1.__exportStar(require("./stream/writable/writableVoid"), exports);
|
|
|
62
62
|
tslib_1.__exportStar(require("./csv/csvWriter"), exports);
|
|
63
63
|
tslib_1.__exportStar(require("./csv/csvReader"), exports);
|
|
64
64
|
tslib_1.__exportStar(require("./csv/transformToCSV"), exports);
|
|
65
|
-
tslib_1.__exportStar(require("./string/
|
|
65
|
+
tslib_1.__exportStar(require("./string/inspect"), exports);
|
|
66
66
|
tslib_1.__exportStar(require("./util/env.util"), exports);
|
|
67
67
|
tslib_1.__exportStar(require("./util/lruMemoCache"), exports);
|
|
68
68
|
tslib_1.__exportStar(require("./util/zip.util"), exports);
|
package/dist/log/log.util.js
CHANGED
|
@@ -7,5 +7,5 @@ const index_1 = require("../index");
|
|
|
7
7
|
* CommonLogger that logs to process.stdout directly (bypassing console.log).
|
|
8
8
|
*/
|
|
9
9
|
exports.stdoutLogger = (0, js_lib_1.commonLoggerCreate)((_level, args) => {
|
|
10
|
-
process.stdout.write(args.map(a => (0, index_1.
|
|
10
|
+
process.stdout.write(args.map(a => (0, index_1._inspect)(a)).join(' ') + '\n');
|
|
11
11
|
});
|
package/dist/script/runScript.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.runScript = void 0;
|
|
4
4
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
const
|
|
5
|
+
const inspect_1 = require("../string/inspect");
|
|
6
6
|
const { DEBUG_RUN_SCRIPT } = process.env;
|
|
7
7
|
/**
|
|
8
8
|
* Use it in your top-level scripts like this:
|
|
@@ -23,7 +23,7 @@ const { DEBUG_RUN_SCRIPT } = process.env;
|
|
|
23
23
|
* Set env DEBUG_RUN_SCRIPT for extra debugging.
|
|
24
24
|
*/
|
|
25
25
|
function runScript(fn, opt = {}) {
|
|
26
|
-
(0, js_lib_1.setGlobalStringifyFunction)(
|
|
26
|
+
(0, js_lib_1.setGlobalStringifyFunction)(inspect_1.inspectStringifyFn);
|
|
27
27
|
const { logger = console, noExit } = opt;
|
|
28
28
|
process.on('uncaughtException', err => {
|
|
29
29
|
logger.error('uncaughtException:', err);
|
|
@@ -69,10 +69,10 @@ class SlackService {
|
|
|
69
69
|
let text;
|
|
70
70
|
// Array has a special treatment here
|
|
71
71
|
if (Array.isArray(msg.items)) {
|
|
72
|
-
text = msg.items.map(t => (0, __1.
|
|
72
|
+
text = msg.items.map(t => (0, __1._inspect)(t, inspectOptions)).join('\n');
|
|
73
73
|
}
|
|
74
74
|
else {
|
|
75
|
-
text = (0, __1.
|
|
75
|
+
text = (0, __1._inspect)(msg.items, inspectOptions);
|
|
76
76
|
}
|
|
77
77
|
// Wrap in markdown-text-block if it's anything but plain String
|
|
78
78
|
if (typeof msg.items !== 'string') {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { InspectOptions } from 'node:util';
|
|
3
|
-
import {
|
|
4
|
-
export interface InspectAnyOptions extends
|
|
3
|
+
import { StringifyOptions, JsonStringifyFunction } from '@naturalcycles/js-lib';
|
|
4
|
+
export interface InspectAnyOptions extends StringifyOptions, InspectOptions {
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
7
|
* Just a convenience export of a const that fulfills the JsonStringifyFunction interface.
|
|
8
8
|
*/
|
|
9
|
-
export declare const
|
|
9
|
+
export declare const inspectStringifyFn: JsonStringifyFunction;
|
|
10
10
|
/**
|
|
11
11
|
* Transforms ANY to human-readable string (via util.inspect mainly).
|
|
12
12
|
* Safe (no error throwing).
|
|
@@ -22,6 +22,10 @@ export declare const inspectAnyStringifyFn: JsonStringifyFunction;
|
|
|
22
22
|
* Returns 'empty_string' if empty string is passed.
|
|
23
23
|
* Returns 'undefined' if undefined is passed (default util.inspect behavior).
|
|
24
24
|
*
|
|
25
|
-
* Based on `
|
|
25
|
+
* Based on `_stringify` from `js-lib`, just replaced `JSON.stringify` with `util.inspect`.
|
|
26
26
|
*/
|
|
27
|
-
export declare function
|
|
27
|
+
export declare function _inspect(obj: any, opt?: InspectAnyOptions): string;
|
|
28
|
+
/**
|
|
29
|
+
* @deprecated renamed to _inspect
|
|
30
|
+
*/
|
|
31
|
+
export declare const inspectAny: typeof _inspect;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.inspectAny = exports.
|
|
3
|
+
exports.inspectAny = exports._inspect = exports.inspectStringifyFn = void 0;
|
|
4
4
|
const node_util_1 = require("node:util");
|
|
5
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
6
|
const INSPECT_OPT = {
|
|
@@ -10,8 +10,8 @@ const INSPECT_OPT = {
|
|
|
10
10
|
/**
|
|
11
11
|
* Just a convenience export of a const that fulfills the JsonStringifyFunction interface.
|
|
12
12
|
*/
|
|
13
|
-
const
|
|
14
|
-
exports.
|
|
13
|
+
const inspectStringifyFn = obj => (0, node_util_1.inspect)(obj, INSPECT_OPT);
|
|
14
|
+
exports.inspectStringifyFn = inspectStringifyFn;
|
|
15
15
|
/**
|
|
16
16
|
* Transforms ANY to human-readable string (via util.inspect mainly).
|
|
17
17
|
* Safe (no error throwing).
|
|
@@ -27,9 +27,9 @@ exports.inspectAnyStringifyFn = inspectAnyStringifyFn;
|
|
|
27
27
|
* Returns 'empty_string' if empty string is passed.
|
|
28
28
|
* Returns 'undefined' if undefined is passed (default util.inspect behavior).
|
|
29
29
|
*
|
|
30
|
-
* Based on `
|
|
30
|
+
* Based on `_stringify` from `js-lib`, just replaced `JSON.stringify` with `util.inspect`.
|
|
31
31
|
*/
|
|
32
|
-
function
|
|
32
|
+
function _inspect(obj, opt = {}) {
|
|
33
33
|
// Inspect handles functions better
|
|
34
34
|
if (typeof obj === 'function') {
|
|
35
35
|
return (0, node_util_1.inspect)(obj, {
|
|
@@ -37,7 +37,7 @@ function inspectAny(obj, opt = {}) {
|
|
|
37
37
|
...opt,
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
|
-
return (0, js_lib_1.
|
|
40
|
+
return (0, js_lib_1._stringify)(obj, {
|
|
41
41
|
...opt,
|
|
42
42
|
stringifyFn: obj => (0, node_util_1.inspect)(obj, {
|
|
43
43
|
...INSPECT_OPT,
|
|
@@ -45,4 +45,8 @@ function inspectAny(obj, opt = {}) {
|
|
|
45
45
|
}),
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
|
-
exports.
|
|
48
|
+
exports._inspect = _inspect;
|
|
49
|
+
/**
|
|
50
|
+
* @deprecated renamed to _inspect
|
|
51
|
+
*/
|
|
52
|
+
exports.inspectAny = _inspect;
|
|
@@ -86,7 +86,7 @@ class AjvSchema {
|
|
|
86
86
|
dataVar: name,
|
|
87
87
|
separator,
|
|
88
88
|
});
|
|
89
|
-
const strValue = (0, index_1.
|
|
89
|
+
const strValue = (0, index_1._inspect)(obj, { maxLen: 1000 });
|
|
90
90
|
message = [message, 'Input: ' + strValue].join(separator);
|
|
91
91
|
if (logErrors) {
|
|
92
92
|
this.cfg.logger.error(errors);
|
|
@@ -11,7 +11,7 @@ export interface JoiValidationResult<T = any> {
|
|
|
11
11
|
*
|
|
12
12
|
* If `schema` is undefined - returns value as is.
|
|
13
13
|
*/
|
|
14
|
-
export declare function validate<T>(
|
|
14
|
+
export declare function validate<T>(input: any, schema?: AnySchema<T>, objectName?: string, opt?: ValidationOptions): T;
|
|
15
15
|
/**
|
|
16
16
|
* Validates with Joi.
|
|
17
17
|
* Returns JoiValidationResult with converted value and error (if any).
|
|
@@ -19,15 +19,15 @@ export declare function validate<T>(value: T, schema?: AnySchema<T>, objectName?
|
|
|
19
19
|
*
|
|
20
20
|
* If `schema` is undefined - returns value as is.
|
|
21
21
|
*/
|
|
22
|
-
export declare function getValidationResult<T>(
|
|
22
|
+
export declare function getValidationResult<T>(input: any, schema?: AnySchema<T>, objectName?: string, options?: ValidationOptions): JoiValidationResult<T>;
|
|
23
23
|
/**
|
|
24
24
|
* Convenience function that returns true if !error.
|
|
25
25
|
*/
|
|
26
|
-
export declare function isValid<T>(
|
|
27
|
-
export declare function undefinedIfInvalid<T>(
|
|
26
|
+
export declare function isValid<T>(input: T, schema?: AnySchema<T>): boolean;
|
|
27
|
+
export declare function undefinedIfInvalid<T>(input: any, schema?: AnySchema<T>): T | undefined;
|
|
28
28
|
/**
|
|
29
|
-
* Will do joi-
|
|
29
|
+
* Will do joi-conversion, regardless of error/validity of value.
|
|
30
30
|
*
|
|
31
31
|
* @returns converted value
|
|
32
32
|
*/
|
|
33
|
-
export declare function convert<T>(
|
|
33
|
+
export declare function convert<T>(input: any, schema?: AnySchema<T>): T;
|
|
@@ -40,8 +40,8 @@ const defaultOptions = {
|
|
|
40
40
|
*
|
|
41
41
|
* If `schema` is undefined - returns value as is.
|
|
42
42
|
*/
|
|
43
|
-
function validate(
|
|
44
|
-
const { value: returnValue, error } = getValidationResult(
|
|
43
|
+
function validate(input, schema, objectName, opt = {}) {
|
|
44
|
+
const { value: returnValue, error } = getValidationResult(input, schema, objectName, opt);
|
|
45
45
|
if (error) {
|
|
46
46
|
throw error;
|
|
47
47
|
}
|
|
@@ -55,18 +55,18 @@ exports.validate = validate;
|
|
|
55
55
|
*
|
|
56
56
|
* If `schema` is undefined - returns value as is.
|
|
57
57
|
*/
|
|
58
|
-
function getValidationResult(
|
|
58
|
+
function getValidationResult(input, schema, objectName, options = {}) {
|
|
59
59
|
if (!schema)
|
|
60
|
-
return { value };
|
|
61
|
-
const { value
|
|
60
|
+
return { value: input };
|
|
61
|
+
const { value, error } = schema.validate(input, {
|
|
62
62
|
...defaultOptions,
|
|
63
63
|
...options,
|
|
64
64
|
});
|
|
65
65
|
const vr = {
|
|
66
|
-
value
|
|
66
|
+
value,
|
|
67
67
|
};
|
|
68
68
|
if (error) {
|
|
69
|
-
vr.error = createError(
|
|
69
|
+
vr.error = createError(input, error, objectName);
|
|
70
70
|
}
|
|
71
71
|
return vr;
|
|
72
72
|
}
|
|
@@ -74,35 +74,33 @@ exports.getValidationResult = getValidationResult;
|
|
|
74
74
|
/**
|
|
75
75
|
* Convenience function that returns true if !error.
|
|
76
76
|
*/
|
|
77
|
-
function isValid(
|
|
77
|
+
function isValid(input, schema) {
|
|
78
78
|
if (!schema)
|
|
79
|
-
return
|
|
80
|
-
const { error } = schema.validate(
|
|
79
|
+
return true;
|
|
80
|
+
const { error } = schema.validate(input, defaultOptions);
|
|
81
81
|
return !error;
|
|
82
82
|
}
|
|
83
83
|
exports.isValid = isValid;
|
|
84
|
-
function undefinedIfInvalid(
|
|
84
|
+
function undefinedIfInvalid(input, schema) {
|
|
85
85
|
if (!schema)
|
|
86
|
-
return
|
|
87
|
-
const { value
|
|
88
|
-
return error ? undefined :
|
|
86
|
+
return input;
|
|
87
|
+
const { value, error } = schema.validate(input, defaultOptions);
|
|
88
|
+
return error ? undefined : value;
|
|
89
89
|
}
|
|
90
90
|
exports.undefinedIfInvalid = undefinedIfInvalid;
|
|
91
91
|
/**
|
|
92
|
-
* Will do joi-
|
|
92
|
+
* Will do joi-conversion, regardless of error/validity of value.
|
|
93
93
|
*
|
|
94
94
|
* @returns converted value
|
|
95
95
|
*/
|
|
96
|
-
function convert(
|
|
96
|
+
function convert(input, schema) {
|
|
97
97
|
if (!schema)
|
|
98
|
-
return
|
|
99
|
-
const { value
|
|
100
|
-
return
|
|
98
|
+
return input;
|
|
99
|
+
const { value } = schema.validate(input, defaultOptions);
|
|
100
|
+
return value;
|
|
101
101
|
}
|
|
102
102
|
exports.convert = convert;
|
|
103
103
|
function createError(value, err, objectName) {
|
|
104
|
-
if (!err)
|
|
105
|
-
return undefined;
|
|
106
104
|
const tokens = [];
|
|
107
105
|
const objectId = (0, js_lib_1._isObject)(value) ? value['id'] : undefined;
|
|
108
106
|
if (objectId || objectName) {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -68,7 +68,7 @@ export * from './stream/writable/writableVoid'
|
|
|
68
68
|
export * from './csv/csvWriter'
|
|
69
69
|
export * from './csv/csvReader'
|
|
70
70
|
export * from './csv/transformToCSV'
|
|
71
|
-
export * from './string/
|
|
71
|
+
export * from './string/inspect'
|
|
72
72
|
export * from './util/env.util'
|
|
73
73
|
export * from './util/lruMemoCache'
|
|
74
74
|
export * from './util/zip.util'
|
package/src/log/log.util.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { commonLoggerCreate } from '@naturalcycles/js-lib'
|
|
2
|
-
import {
|
|
2
|
+
import { _inspect } from '../index'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* CommonLogger that logs to process.stdout directly (bypassing console.log).
|
|
6
6
|
*/
|
|
7
7
|
export const stdoutLogger = commonLoggerCreate((_level, args) => {
|
|
8
|
-
process.stdout.write(args.map(a =>
|
|
8
|
+
process.stdout.write(args.map(a => _inspect(a)).join(' ') + '\n')
|
|
9
9
|
})
|
package/src/script/runScript.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { setGlobalStringifyFunction } from '@naturalcycles/js-lib'
|
|
2
2
|
import type { CommonLogger } from '@naturalcycles/js-lib'
|
|
3
|
-
import {
|
|
3
|
+
import { inspectStringifyFn } from '../string/inspect'
|
|
4
4
|
|
|
5
5
|
export interface RunScriptOptions {
|
|
6
6
|
/**
|
|
@@ -37,7 +37,7 @@ const { DEBUG_RUN_SCRIPT } = process.env
|
|
|
37
37
|
* Set env DEBUG_RUN_SCRIPT for extra debugging.
|
|
38
38
|
*/
|
|
39
39
|
export function runScript(fn: (...args: any[]) => any, opt: RunScriptOptions = {}): void {
|
|
40
|
-
setGlobalStringifyFunction(
|
|
40
|
+
setGlobalStringifyFunction(inspectStringifyFn)
|
|
41
41
|
|
|
42
42
|
const { logger = console, noExit } = opt
|
|
43
43
|
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
localTimeNow,
|
|
10
10
|
PQueue,
|
|
11
11
|
} from '@naturalcycles/js-lib'
|
|
12
|
-
import {
|
|
12
|
+
import { _inspect, InspectAnyOptions } from '..'
|
|
13
13
|
import {
|
|
14
14
|
SlackApiBody,
|
|
15
15
|
SlackAttachmentField,
|
|
@@ -99,9 +99,9 @@ export class SlackService<CTX = any> {
|
|
|
99
99
|
|
|
100
100
|
// Array has a special treatment here
|
|
101
101
|
if (Array.isArray(msg.items)) {
|
|
102
|
-
text = msg.items.map(t =>
|
|
102
|
+
text = msg.items.map(t => _inspect(t, inspectOptions)).join('\n')
|
|
103
103
|
} else {
|
|
104
|
-
text =
|
|
104
|
+
text = _inspect(msg.items, inspectOptions)
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
// Wrap in markdown-text-block if it's anything but plain String
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { inspect, InspectOptions } from 'node:util'
|
|
2
|
-
import {
|
|
2
|
+
import { StringifyOptions, _stringify, JsonStringifyFunction } from '@naturalcycles/js-lib'
|
|
3
3
|
|
|
4
|
-
export interface InspectAnyOptions extends
|
|
4
|
+
export interface InspectAnyOptions extends StringifyOptions, InspectOptions {}
|
|
5
5
|
|
|
6
6
|
const INSPECT_OPT: InspectOptions = {
|
|
7
7
|
breakLength: 80, // default: ??
|
|
@@ -11,7 +11,7 @@ const INSPECT_OPT: InspectOptions = {
|
|
|
11
11
|
/**
|
|
12
12
|
* Just a convenience export of a const that fulfills the JsonStringifyFunction interface.
|
|
13
13
|
*/
|
|
14
|
-
export const
|
|
14
|
+
export const inspectStringifyFn: JsonStringifyFunction = obj => inspect(obj, INSPECT_OPT)
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Transforms ANY to human-readable string (via util.inspect mainly).
|
|
@@ -28,9 +28,9 @@ export const inspectAnyStringifyFn: JsonStringifyFunction = obj => inspect(obj,
|
|
|
28
28
|
* Returns 'empty_string' if empty string is passed.
|
|
29
29
|
* Returns 'undefined' if undefined is passed (default util.inspect behavior).
|
|
30
30
|
*
|
|
31
|
-
* Based on `
|
|
31
|
+
* Based on `_stringify` from `js-lib`, just replaced `JSON.stringify` with `util.inspect`.
|
|
32
32
|
*/
|
|
33
|
-
export function
|
|
33
|
+
export function _inspect(obj: any, opt: InspectAnyOptions = {}): string {
|
|
34
34
|
// Inspect handles functions better
|
|
35
35
|
if (typeof obj === 'function') {
|
|
36
36
|
return inspect(obj, {
|
|
@@ -39,7 +39,7 @@ export function inspectAny(obj: any, opt: InspectAnyOptions = {}): string {
|
|
|
39
39
|
})
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
return
|
|
42
|
+
return _stringify(obj, {
|
|
43
43
|
...opt,
|
|
44
44
|
stringifyFn: obj =>
|
|
45
45
|
inspect(obj, {
|
|
@@ -48,3 +48,8 @@ export function inspectAny(obj: any, opt: InspectAnyOptions = {}): string {
|
|
|
48
48
|
}),
|
|
49
49
|
})
|
|
50
50
|
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @deprecated renamed to _inspect
|
|
54
|
+
*/
|
|
55
|
+
export const inspectAny = _inspect
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
CommonLogger,
|
|
9
9
|
} from '@naturalcycles/js-lib'
|
|
10
10
|
import Ajv, { ValidateFunction } from 'ajv'
|
|
11
|
-
import { _readJsonSync,
|
|
11
|
+
import { _readJsonSync, _inspect, requireFileToExist } from '../../index'
|
|
12
12
|
import { AjvValidationError } from './ajvValidationError'
|
|
13
13
|
import { getAjv } from './getAjv'
|
|
14
14
|
|
|
@@ -173,7 +173,7 @@ export class AjvSchema<T = unknown> {
|
|
|
173
173
|
separator,
|
|
174
174
|
})
|
|
175
175
|
|
|
176
|
-
const strValue =
|
|
176
|
+
const strValue = _inspect(obj, { maxLen: 1000 })
|
|
177
177
|
message = [message, 'Input: ' + strValue].join(separator)
|
|
178
178
|
|
|
179
179
|
if (logErrors) {
|
|
@@ -49,12 +49,12 @@ const defaultOptions: ValidationOptions = {
|
|
|
49
49
|
* If `schema` is undefined - returns value as is.
|
|
50
50
|
*/
|
|
51
51
|
export function validate<T>(
|
|
52
|
-
|
|
52
|
+
input: any,
|
|
53
53
|
schema?: AnySchema<T>,
|
|
54
54
|
objectName?: string,
|
|
55
|
-
|
|
55
|
+
opt: ValidationOptions = {},
|
|
56
56
|
): T {
|
|
57
|
-
const { value: returnValue, error } = getValidationResult(
|
|
57
|
+
const { value: returnValue, error } = getValidationResult(input, schema, objectName, opt)
|
|
58
58
|
|
|
59
59
|
if (error) {
|
|
60
60
|
throw error
|
|
@@ -71,24 +71,24 @@ export function validate<T>(
|
|
|
71
71
|
* If `schema` is undefined - returns value as is.
|
|
72
72
|
*/
|
|
73
73
|
export function getValidationResult<T>(
|
|
74
|
-
|
|
74
|
+
input: any,
|
|
75
75
|
schema?: AnySchema<T>,
|
|
76
76
|
objectName?: string,
|
|
77
77
|
options: ValidationOptions = {},
|
|
78
78
|
): JoiValidationResult<T> {
|
|
79
|
-
if (!schema) return { value }
|
|
79
|
+
if (!schema) return { value: input }
|
|
80
80
|
|
|
81
|
-
const { value
|
|
81
|
+
const { value, error } = schema.validate(input, {
|
|
82
82
|
...defaultOptions,
|
|
83
83
|
...options,
|
|
84
84
|
})
|
|
85
85
|
|
|
86
86
|
const vr: JoiValidationResult<T> = {
|
|
87
|
-
value
|
|
87
|
+
value,
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
if (error) {
|
|
91
|
-
vr.error = createError(
|
|
91
|
+
vr.error = createError(input, error, objectName)
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
return vr
|
|
@@ -97,34 +97,33 @@ export function getValidationResult<T>(
|
|
|
97
97
|
/**
|
|
98
98
|
* Convenience function that returns true if !error.
|
|
99
99
|
*/
|
|
100
|
-
export function isValid<T>(
|
|
101
|
-
if (!schema) return
|
|
100
|
+
export function isValid<T>(input: T, schema?: AnySchema<T>): boolean {
|
|
101
|
+
if (!schema) return true
|
|
102
102
|
|
|
103
|
-
const { error } = schema.validate(
|
|
103
|
+
const { error } = schema.validate(input, defaultOptions)
|
|
104
104
|
return !error
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
export function undefinedIfInvalid<T>(
|
|
108
|
-
if (!schema) return
|
|
107
|
+
export function undefinedIfInvalid<T>(input: any, schema?: AnySchema<T>): T | undefined {
|
|
108
|
+
if (!schema) return input
|
|
109
109
|
|
|
110
|
-
const { value
|
|
110
|
+
const { value, error } = schema.validate(input, defaultOptions)
|
|
111
111
|
|
|
112
|
-
return error ? undefined :
|
|
112
|
+
return error ? undefined : value
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
/**
|
|
116
|
-
* Will do joi-
|
|
116
|
+
* Will do joi-conversion, regardless of error/validity of value.
|
|
117
117
|
*
|
|
118
118
|
* @returns converted value
|
|
119
119
|
*/
|
|
120
|
-
export function convert<T>(
|
|
121
|
-
if (!schema) return
|
|
122
|
-
const { value
|
|
123
|
-
return
|
|
120
|
+
export function convert<T>(input: any, schema?: AnySchema<T>): T {
|
|
121
|
+
if (!schema) return input
|
|
122
|
+
const { value } = schema.validate(input, defaultOptions)
|
|
123
|
+
return value
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
function createError(value: any, err: ValidationError, objectName?: string): JoiValidationError {
|
|
127
|
-
if (!err) return undefined as any
|
|
128
127
|
const tokens: string[] = []
|
|
129
128
|
|
|
130
129
|
const objectId = _isObject(value) ? (value['id'] as string) : undefined
|