@naturalcycles/js-lib 15.18.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.
@@ -1,6 +1,7 @@
1
1
  import { _isBackendErrorResponseObject, _isErrorLike, _isErrorObject } from '../error/error.util.js';
2
2
  import { _jsonParseIfPossible } from './json.util.js';
3
3
  import { _safeJsonStringify } from './safeJsonStringify.js';
4
+ import { _truncateMiddle } from './string.util.js';
4
5
  const supportsAggregateError = typeof globalThis.AggregateError === 'function';
5
6
  let globalStringifyFunction = _safeJsonStringify;
6
7
  /**
@@ -127,7 +128,7 @@ export function _stringify(obj, opt = {}) {
127
128
  // Handle maxLen
128
129
  const { maxLen = 10_000 } = opt;
129
130
  if (maxLen && s.length > maxLen) {
130
- s = s.slice(0, maxLen) + `... ${Math.ceil(s.length / 1024)} KB message truncated`;
131
+ return _truncateMiddle(s, maxLen, `\n... ${Math.ceil(s.length / 1024)} Kb message truncated ...\n`);
131
132
  }
132
133
  return s;
133
134
  }
@@ -1,30 +1,30 @@
1
1
  import type { AppError } from '../error/error.util.js';
2
- import type { ErrorDataTuple } from '../types.js';
3
2
  /**
4
- * Item to be validated.
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
- * Function returns a tuple of [err, item].
10
- * In case of error it will be [err, null].
11
- * In case of success it is [null, item].
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> = (item: T | null | undefined, opt?: ValidationFunctionOptions) => ErrorDataTuple<T, ERR>;
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
- itemName?: string;
24
+ inputName?: string;
25
25
  /**
26
26
  * E.g `12345678` (user id).
27
27
  * Used for error message printing.
28
28
  */
29
- itemId?: string;
29
+ inputId?: string;
30
30
  }
@@ -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
- export interface ZodErrorResult<T> {
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>(value: T, schema: ZodType<T>): ZodSuccessResult<T> | ZodErrorResult<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> {
@@ -5,21 +5,17 @@ export function zIsValid(value, schema) {
5
5
  return success;
6
6
  }
7
7
  export function zValidate(value, schema) {
8
- const r = zSafeValidate(value, schema);
9
- if (r.success) {
10
- return r.data;
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(value, schema) {
15
- const r = schema.safeParse(value);
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.18.0",
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": "19.11.0"
15
+ "@naturalcycles/dev-lib": "19.14.0"
16
16
  },
17
17
  "exports": {
18
18
  ".": "./dist/index.js",
@@ -2,6 +2,7 @@ import { _isBackendErrorResponseObject, _isErrorLike, _isErrorObject } from '../
2
2
  import type { Reviver } from '../types.js'
3
3
  import { _jsonParseIfPossible } from './json.util.js'
4
4
  import { _safeJsonStringify } from './safeJsonStringify.js'
5
+ import { _truncateMiddle } from './string.util.js'
5
6
 
6
7
  const supportsAggregateError = typeof globalThis.AggregateError === 'function'
7
8
 
@@ -179,7 +180,11 @@ export function _stringify(obj: any, opt: StringifyOptions = {}): string {
179
180
  // Handle maxLen
180
181
  const { maxLen = 10_000 } = opt
181
182
  if (maxLen && s.length > maxLen) {
182
- s = s.slice(0, maxLen) + `... ${Math.ceil(s.length / 1024)} KB message truncated`
183
+ return _truncateMiddle(
184
+ s,
185
+ maxLen,
186
+ `\n... ${Math.ceil(s.length / 1024)} Kb message truncated ...\n`,
187
+ )
183
188
  }
184
189
 
185
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
- * Item to be validated.
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
- * Function returns a tuple of [err, item].
11
- * In case of error it will be [err, null].
12
- * In case of success it is [null, item].
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
- item: T | null | undefined,
19
+ input: T,
21
20
  opt?: ValidationFunctionOptions,
22
- ) => ErrorDataTuple<T, ERR>
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
- itemName?: string
30
+ inputName?: string
30
31
  /**
31
32
  * E.g `12345678` (user id).
32
33
  * Used for error message printing.
33
34
  */
34
- itemId?: string
35
+ inputId?: string
35
36
  }
@@ -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 r = zSafeValidate(value, schema)
25
- if (r.success) {
26
- return r.data
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
- value: T,
19
+ input: T,
34
20
  schema: ZodType<T>,
35
21
  // objectName?: string,
36
- ): ZodSuccessResult<T> | ZodErrorResult<T> {
37
- const r = schema.safeParse(value)
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 {