@naturalcycles/js-lib 15.19.0 → 15.20.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/validation/validation.d.ts +11 -11
- package/dist/zod/zod.util.d.ts +2 -11
- package/dist/zod/zod.util.js +8 -12
- package/package.json +2 -2
- package/src/string/stringify.ts +5 -1
- package/src/validation/validation.ts +13 -12
- package/src/zod/zod.util.ts +9 -26
|
@@ -1,30 +1,30 @@
|
|
|
1
1
|
import type { AppError } from '../error/error.util.js';
|
|
2
|
-
import type { ErrorDataTuple } from '../types.js';
|
|
3
2
|
/**
|
|
4
|
-
*
|
|
5
|
-
* Can be null or undefined, which allows validation function to produce an error,
|
|
6
|
-
* if undefined/null are not accepted. But they might be accepted too, it depends
|
|
7
|
-
* on the schema (implementation detail of the ValidationFunction).
|
|
3
|
+
* Function returns a tuple of [err, output].
|
|
8
4
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
5
|
+
* If ERR is returned, it indicates that validation has failed.
|
|
6
|
+
* If ERR is null - validation has succeeded.
|
|
7
|
+
*
|
|
8
|
+
* Regardless of the Error, ValidationFunction always returns the output item.
|
|
9
|
+
*
|
|
10
|
+
* Output item may be transformed or not, depending on the implementation.
|
|
12
11
|
*
|
|
13
12
|
* ValidationFunction may mutate the input item or not,
|
|
14
13
|
* depending on the implementation.
|
|
15
14
|
*
|
|
16
15
|
* @experimental
|
|
17
16
|
*/
|
|
18
|
-
export type ValidationFunction<T, ERR extends AppError> = (
|
|
17
|
+
export type ValidationFunction<T, ERR extends AppError> = (input: T, opt?: ValidationFunctionOptions) => ValidationFunctionResult<T, ERR>;
|
|
18
|
+
export type ValidationFunctionResult<T, ERR extends AppError> = [err: ERR | null, output: T];
|
|
19
19
|
export interface ValidationFunctionOptions {
|
|
20
20
|
/**
|
|
21
21
|
* E.g User
|
|
22
22
|
* Used for error message printing.
|
|
23
23
|
*/
|
|
24
|
-
|
|
24
|
+
inputName?: string;
|
|
25
25
|
/**
|
|
26
26
|
* E.g `12345678` (user id).
|
|
27
27
|
* Used for error message printing.
|
|
28
28
|
*/
|
|
29
|
-
|
|
29
|
+
inputId?: string;
|
|
30
30
|
}
|
package/dist/zod/zod.util.d.ts
CHANGED
|
@@ -1,19 +1,10 @@
|
|
|
1
1
|
import type { ZodError, ZodType } from 'zod';
|
|
2
2
|
import type { ErrorData } from '../error/error.model.js';
|
|
3
3
|
import { AppError } from '../error/error.util.js';
|
|
4
|
-
|
|
5
|
-
success: false;
|
|
6
|
-
data?: T;
|
|
7
|
-
error: ZodValidationError;
|
|
8
|
-
}
|
|
9
|
-
export interface ZodSuccessResult<T> {
|
|
10
|
-
success: true;
|
|
11
|
-
data: T;
|
|
12
|
-
error?: ZodValidationError;
|
|
13
|
-
}
|
|
4
|
+
import type { ValidationFunctionResult } from '../validation/validation.js';
|
|
14
5
|
export declare function zIsValid<T>(value: T, schema: ZodType<T>): boolean;
|
|
15
6
|
export declare function zValidate<T>(value: T, schema: ZodType<T>): T;
|
|
16
|
-
export declare function zSafeValidate<T>(
|
|
7
|
+
export declare function zSafeValidate<T>(input: T, schema: ZodType<T>): ValidationFunctionResult<T, ZodValidationError>;
|
|
17
8
|
export interface ZodValidationErrorData extends ErrorData {
|
|
18
9
|
}
|
|
19
10
|
export declare class ZodValidationError extends AppError<ZodValidationErrorData> {
|
package/dist/zod/zod.util.js
CHANGED
|
@@ -5,21 +5,17 @@ export function zIsValid(value, schema) {
|
|
|
5
5
|
return success;
|
|
6
6
|
}
|
|
7
7
|
export function zValidate(value, schema) {
|
|
8
|
-
const
|
|
9
|
-
if (
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
throw r.error;
|
|
8
|
+
const [err, data] = zSafeValidate(value, schema);
|
|
9
|
+
if (err)
|
|
10
|
+
throw err;
|
|
11
|
+
return data;
|
|
13
12
|
}
|
|
14
|
-
export function zSafeValidate(
|
|
15
|
-
const r = schema.safeParse(
|
|
13
|
+
export function zSafeValidate(input, schema) {
|
|
14
|
+
const r = schema.safeParse(input);
|
|
16
15
|
if (r.success) {
|
|
17
|
-
return r;
|
|
16
|
+
return [null, r.data];
|
|
18
17
|
}
|
|
19
|
-
return
|
|
20
|
-
success: false,
|
|
21
|
-
error: new ZodValidationError(r.error, value, schema),
|
|
22
|
-
};
|
|
18
|
+
return [new ZodValidationError(r.error, input, schema), r.data ?? input];
|
|
23
19
|
}
|
|
24
20
|
export class ZodValidationError extends AppError {
|
|
25
21
|
constructor(zodError, value, schema) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "15.
|
|
4
|
+
"version": "15.20.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "^2",
|
|
7
7
|
"zod": "^4"
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"@types/semver": "^7",
|
|
13
13
|
"crypto-js": "^4",
|
|
14
14
|
"dayjs": "^1",
|
|
15
|
-
"@naturalcycles/dev-lib": "
|
|
15
|
+
"@naturalcycles/dev-lib": "19.14.0"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
18
|
".": "./dist/index.js",
|
package/src/string/stringify.ts
CHANGED
|
@@ -180,7 +180,11 @@ export function _stringify(obj: any, opt: StringifyOptions = {}): string {
|
|
|
180
180
|
// Handle maxLen
|
|
181
181
|
const { maxLen = 10_000 } = opt
|
|
182
182
|
if (maxLen && s.length > maxLen) {
|
|
183
|
-
return _truncateMiddle(
|
|
183
|
+
return _truncateMiddle(
|
|
184
|
+
s,
|
|
185
|
+
maxLen,
|
|
186
|
+
`\n... ${Math.ceil(s.length / 1024)} Kb message truncated ...\n`,
|
|
187
|
+
)
|
|
184
188
|
}
|
|
185
189
|
|
|
186
190
|
return s
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import type { AppError } from '../error/error.util.js'
|
|
2
|
-
import type { ErrorDataTuple } from '../types.js'
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
|
-
*
|
|
6
|
-
* Can be null or undefined, which allows validation function to produce an error,
|
|
7
|
-
* if undefined/null are not accepted. But they might be accepted too, it depends
|
|
8
|
-
* on the schema (implementation detail of the ValidationFunction).
|
|
4
|
+
* Function returns a tuple of [err, output].
|
|
9
5
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
6
|
+
* If ERR is returned, it indicates that validation has failed.
|
|
7
|
+
* If ERR is null - validation has succeeded.
|
|
8
|
+
*
|
|
9
|
+
* Regardless of the Error, ValidationFunction always returns the output item.
|
|
10
|
+
*
|
|
11
|
+
* Output item may be transformed or not, depending on the implementation.
|
|
13
12
|
*
|
|
14
13
|
* ValidationFunction may mutate the input item or not,
|
|
15
14
|
* depending on the implementation.
|
|
@@ -17,19 +16,21 @@ import type { ErrorDataTuple } from '../types.js'
|
|
|
17
16
|
* @experimental
|
|
18
17
|
*/
|
|
19
18
|
export type ValidationFunction<T, ERR extends AppError> = (
|
|
20
|
-
|
|
19
|
+
input: T,
|
|
21
20
|
opt?: ValidationFunctionOptions,
|
|
22
|
-
) =>
|
|
21
|
+
) => ValidationFunctionResult<T, ERR>
|
|
22
|
+
|
|
23
|
+
export type ValidationFunctionResult<T, ERR extends AppError> = [err: ERR | null, output: T]
|
|
23
24
|
|
|
24
25
|
export interface ValidationFunctionOptions {
|
|
25
26
|
/**
|
|
26
27
|
* E.g User
|
|
27
28
|
* Used for error message printing.
|
|
28
29
|
*/
|
|
29
|
-
|
|
30
|
+
inputName?: string
|
|
30
31
|
/**
|
|
31
32
|
* E.g `12345678` (user id).
|
|
32
33
|
* Used for error message printing.
|
|
33
34
|
*/
|
|
34
|
-
|
|
35
|
+
inputId?: string
|
|
35
36
|
}
|
package/src/zod/zod.util.ts
CHANGED
|
@@ -2,18 +2,7 @@ import type { ZodError, ZodType } from 'zod'
|
|
|
2
2
|
import type { ErrorData } from '../error/error.model.js'
|
|
3
3
|
import { AppError } from '../error/error.util.js'
|
|
4
4
|
import { _stringify } from '../string/stringify.js'
|
|
5
|
-
|
|
6
|
-
export interface ZodErrorResult<T> {
|
|
7
|
-
success: false
|
|
8
|
-
data?: T
|
|
9
|
-
error: ZodValidationError
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface ZodSuccessResult<T> {
|
|
13
|
-
success: true
|
|
14
|
-
data: T
|
|
15
|
-
error?: ZodValidationError
|
|
16
|
-
}
|
|
5
|
+
import type { ValidationFunctionResult } from '../validation/validation.js'
|
|
17
6
|
|
|
18
7
|
export function zIsValid<T>(value: T, schema: ZodType<T>): boolean {
|
|
19
8
|
const { success } = schema.safeParse(value)
|
|
@@ -21,28 +10,22 @@ export function zIsValid<T>(value: T, schema: ZodType<T>): boolean {
|
|
|
21
10
|
}
|
|
22
11
|
|
|
23
12
|
export function zValidate<T>(value: T, schema: ZodType<T>): T {
|
|
24
|
-
const
|
|
25
|
-
if (
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
throw r.error
|
|
13
|
+
const [err, data] = zSafeValidate(value, schema)
|
|
14
|
+
if (err) throw err
|
|
15
|
+
return data
|
|
30
16
|
}
|
|
31
17
|
|
|
32
18
|
export function zSafeValidate<T>(
|
|
33
|
-
|
|
19
|
+
input: T,
|
|
34
20
|
schema: ZodType<T>,
|
|
35
21
|
// objectName?: string,
|
|
36
|
-
):
|
|
37
|
-
const r = schema.safeParse(
|
|
22
|
+
): ValidationFunctionResult<T, ZodValidationError> {
|
|
23
|
+
const r = schema.safeParse(input)
|
|
38
24
|
if (r.success) {
|
|
39
|
-
return r
|
|
25
|
+
return [null, r.data]
|
|
40
26
|
}
|
|
41
27
|
|
|
42
|
-
return
|
|
43
|
-
success: false,
|
|
44
|
-
error: new ZodValidationError(r.error, value, schema),
|
|
45
|
-
}
|
|
28
|
+
return [new ZodValidationError(r.error, input, schema), r.data ?? input]
|
|
46
29
|
}
|
|
47
30
|
|
|
48
31
|
export interface ZodValidationErrorData extends ErrorData {
|