@naturalcycles/js-lib 15.3.0 → 15.5.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,6 @@
1
1
  export * from './zod.shared.schemas.js';
2
2
  export * from './zod.util.js';
3
- import type { ZodIssue } from 'zod';
4
- import { z, ZodError, ZodSchema } from 'zod';
5
- export { z, ZodError, ZodSchema };
6
- export type { ZodIssue };
3
+ import type { ZodJSONSchema } from 'zod/v4';
4
+ import { z, ZodType } from 'zod/v4';
5
+ export { z, ZodType };
6
+ export type { ZodJSONSchema };
package/dist/zod/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './zod.shared.schemas.js';
2
2
  export * from './zod.util.js';
3
- import { z, ZodError, ZodSchema } from 'zod';
4
- export { z, ZodError, ZodSchema };
3
+ import { z, ZodType } from 'zod/v4';
4
+ export { z, ZodType };
@@ -1,4 +1,4 @@
1
- import { z } from 'zod';
1
+ import { z } from 'zod/v4';
2
2
  export declare const TS_2500 = 16725225600;
3
3
  export declare const TS_2000 = 946684800;
4
4
  export declare const zUnixTimestamp: z.ZodNumber;
@@ -6,8 +6,9 @@ export declare const zUnixTimestamp2000: z.ZodNumber;
6
6
  export declare const zUnixTimestampMillis: z.ZodNumber;
7
7
  export declare const zUnixTimestampMillis2000: z.ZodNumber;
8
8
  export declare const zSemVer: z.ZodString;
9
- export declare const zIsoDateString: z.ZodEffects<z.ZodString, string, string>;
10
- export declare const zEmail: z.ZodEffects<z.ZodString, string, string>;
9
+ export declare const zIsoDate: z.ZodString;
10
+ export declare const zEmail: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
11
+ export declare const zEmailNoLowercase: z.ZodString;
11
12
  export declare const BASE62_REGEX: RegExp;
12
13
  export declare const BASE64_REGEX: RegExp;
13
14
  export declare const BASE64URL_REGEX: RegExp;
@@ -28,12 +29,4 @@ export declare const zBaseDBEntity: z.ZodObject<{
28
29
  id: z.ZodString;
29
30
  created: z.ZodOptional<z.ZodNumber>;
30
31
  updated: z.ZodOptional<z.ZodNumber>;
31
- }, "strip", z.ZodTypeAny, {
32
- id: string;
33
- created?: number | undefined;
34
- updated?: number | undefined;
35
- }, {
36
- id: string;
37
- created?: number | undefined;
38
- updated?: number | undefined;
39
- }>;
32
+ }, z.core.$strip>;
@@ -1,4 +1,4 @@
1
- import { z } from 'zod';
1
+ import { z } from 'zod/v4';
2
2
  export const TS_2500 = 16725225600; // 2500-01-01
3
3
  export const TS_2000 = 946684800; // 2000-01-01
4
4
  export const zUnixTimestamp = z
@@ -6,40 +6,51 @@ export const zUnixTimestamp = z
6
6
  .int()
7
7
  .min(0)
8
8
  .max(TS_2500, 'Must be a UnixTimestamp number')
9
+ // .transform(v => v as UnixTimestamp) // breaks jsonSchema
9
10
  .describe('UnixTimestamp');
10
11
  export const zUnixTimestamp2000 = z
11
12
  .number()
12
13
  .int()
13
14
  .min(TS_2000)
14
15
  .max(TS_2500, 'Must be a UnixTimestamp number after 2000-01-01')
16
+ // .transform(v => v as UnixTimestamp)
15
17
  .describe('UnixTimestamp2000');
16
18
  export const zUnixTimestampMillis = z
17
19
  .number()
18
20
  .int()
19
21
  .min(0)
20
22
  .max(TS_2500 * 1000, 'Must be a UnixTimestampMillis number')
23
+ // .transform(v => v as UnixTimestampMillis)
21
24
  .describe('UnixTimestampMillis');
22
25
  export const zUnixTimestampMillis2000 = z
23
26
  .number()
24
27
  .int()
25
28
  .min(TS_2000 * 1000)
26
29
  .max(TS_2500 * 1000, 'Must be a UnixTimestampMillis number after 2000-01-01')
30
+ // .transform(v => v as UnixTimestampMillis)
27
31
  .describe('UnixTimestampMillis2000');
28
32
  export const zSemVer = z
29
33
  .string()
30
34
  .regex(/^[0-9]+\.[0-9]+\.[0-9]+$/, 'Must be a SemVer string')
31
35
  .describe('SemVer');
32
- export const zIsoDateString = z
36
+ export const zIsoDate = z
33
37
  .string()
34
38
  .refine(v => {
35
39
  return /^\d{4}-\d{2}-\d{2}$/.test(v);
36
40
  }, 'Must be an IsoDateString')
41
+ // .transform(v => v as IsoDate)
37
42
  .describe('IsoDateString');
38
43
  export const zEmail = z
39
44
  .string()
40
45
  .trim()
41
- .email()
42
- .transform(s => s.toLowerCase())
46
+ .email() // keeping as-is, so trim happens before email validation
47
+ .transform(s => s.toLowerCase()) // breaks toJsonSchema
48
+ .describe('Email');
49
+ export const zEmailNoLowercase = z
50
+ .string()
51
+ .trim()
52
+ .email() // keeping as-is, so trim happens before email validation
53
+ // .transform(s => s.toLowerCase()) // breaks toJsonSchema
43
54
  .describe('Email');
44
55
  export const BASE62_REGEX = /^[a-zA-Z0-9]+$/;
45
56
  export const BASE64_REGEX = /^[A-Za-z0-9+/]+={0,2}$/;
@@ -60,7 +71,7 @@ export const JWT_REGEX = /^[\w-]+\.[\w-]+\.[\w-]+$/;
60
71
  export const zJwt = z.string().regex(JWT_REGEX, 'Must be a JWT string').describe('JWTString');
61
72
  export const zId = z
62
73
  .string()
63
- .regex(/^[a-zA-Z0-9_]{6,64}$/, 'Must be an id string')
74
+ .regex(/^[a-zA-Z0-9_]{6,64}$/, 'Must be an id string (6 to 64 chars long)')
64
75
  .describe('IdString');
65
76
  export const zIdBase62 = z
66
77
  .string()
@@ -1,21 +1,21 @@
1
- import { ZodError, type ZodIssue, type ZodSchema } from 'zod';
1
+ import type { ZodError, ZodType } from 'zod/v4';
2
+ import type { ErrorData } from '../error/error.model.js';
3
+ import { AppError } from '../error/error.util.js';
2
4
  export interface ZodErrorResult<T> {
3
5
  success: false;
4
6
  data?: T;
5
- error: ZodValidationError<T>;
7
+ error: ZodValidationError;
6
8
  }
7
9
  export interface ZodSuccessResult<T> {
8
10
  success: true;
9
11
  data: T;
10
- error?: ZodValidationError<T>;
12
+ error?: ZodValidationError;
11
13
  }
12
- export declare function zIsValid<T>(value: T, schema: ZodSchema<T>): boolean;
13
- export declare function zValidate<T>(value: T, schema: ZodSchema<T>): T;
14
- export declare function zSafeValidate<T>(value: T, schema: ZodSchema<T>): ZodSuccessResult<T> | ZodErrorResult<T>;
15
- export declare class ZodValidationError<T> extends ZodError<T> {
16
- value: T;
17
- schema: ZodSchema<T>;
18
- constructor(issues: ZodIssue[], value: T, schema: ZodSchema<T>);
19
- get message(): string;
20
- annotate(): string;
14
+ export declare function zIsValid<T>(value: T, schema: ZodType<T>): boolean;
15
+ 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>;
17
+ export interface ZodValidationErrorData extends ErrorData {
18
+ }
19
+ export declare class ZodValidationError extends AppError<ZodValidationErrorData> {
20
+ constructor(zodError: ZodError, value: any, schema: ZodType);
21
21
  }
@@ -1,4 +1,4 @@
1
- import { ZodError } from 'zod';
1
+ import { AppError } from '../error/error.util.js';
2
2
  import { _stringify } from '../string/stringify.js';
3
3
  export function zIsValid(value, schema) {
4
4
  const { success } = schema.safeParse(value);
@@ -18,37 +18,32 @@ export function zSafeValidate(value, schema) {
18
18
  }
19
19
  return {
20
20
  success: false,
21
- error: new ZodValidationError(r.error.issues, value, schema),
21
+ error: new ZodValidationError(r.error, value, schema),
22
22
  };
23
23
  }
24
- export class ZodValidationError extends ZodError {
25
- value;
26
- schema;
27
- constructor(issues, value, schema) {
28
- super(issues);
29
- this.value = value;
30
- this.schema = schema;
24
+ export class ZodValidationError extends AppError {
25
+ constructor(zodError, value, schema) {
26
+ const message = createZodErrorMessage(zodError, schema, value);
27
+ // const message = z.prettifyError(zodError) // todo: consider adopting it instead
28
+ super(message, {}, { name: 'ZodValidationError' });
31
29
  }
32
- get message() {
33
- return this.annotate();
34
- }
35
- annotate() {
36
- let objectTitle = this.schema.description;
37
- if (typeof this.value === 'object' && this.value) {
38
- const objectName = this.schema.description || this.value.constructor?.name;
39
- const objectId = this.value['id'];
40
- objectTitle = [objectName, objectId].filter(Boolean).join('.');
41
- }
42
- objectTitle ||= 'data';
43
- return [
44
- `Invalid ${objectTitle}`,
45
- '',
46
- 'Input:',
47
- _stringify(this.value),
48
- this.issues.length > 1 ? `\n${this.issues.length} issues:` : '',
49
- ...this.issues.slice(0, 100).map(i => {
50
- return [i.path.join('.'), i.message].filter(Boolean).join(': ');
51
- }),
52
- ].join('\n');
30
+ }
31
+ function createZodErrorMessage(err, schema, value) {
32
+ let objectTitle = schema.description;
33
+ if (typeof value === 'object' && value) {
34
+ const objectName = schema.description || value.constructor?.name;
35
+ const objectId = value['id'];
36
+ objectTitle = [objectName, objectId].filter(Boolean).join('.');
53
37
  }
38
+ objectTitle ||= 'data';
39
+ return [
40
+ `Invalid ${objectTitle}`,
41
+ '',
42
+ 'Input:',
43
+ _stringify(value),
44
+ err.issues.length > 1 ? `\n${err.issues.length} issues:` : '',
45
+ ...err.issues.slice(0, 100).map(i => {
46
+ return [i.path.join('.'), i.message].filter(Boolean).join(': ');
47
+ }),
48
+ ].join('\n');
54
49
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.3.0",
4
+ "version": "15.5.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "zod": "^3"
package/src/zod/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export * from './zod.shared.schemas.js'
2
2
  export * from './zod.util.js'
3
- import type { ZodIssue } from 'zod'
4
- import { z, ZodError, ZodSchema } from 'zod'
3
+ import type { ZodJSONSchema } from 'zod/v4'
4
+ import { z, ZodType } from 'zod/v4'
5
5
 
6
- export { z, ZodError, ZodSchema }
7
- export type { ZodIssue }
6
+ export { z, ZodType }
7
+ export type { ZodJSONSchema }
@@ -1,4 +1,4 @@
1
- import { z } from 'zod'
1
+ import { z } from 'zod/v4'
2
2
 
3
3
  export const TS_2500 = 16725225600 // 2500-01-01
4
4
  export const TS_2000 = 946684800 // 2000-01-01
@@ -8,24 +8,28 @@ export const zUnixTimestamp = z
8
8
  .int()
9
9
  .min(0)
10
10
  .max(TS_2500, 'Must be a UnixTimestamp number')
11
+ // .transform(v => v as UnixTimestamp) // breaks jsonSchema
11
12
  .describe('UnixTimestamp')
12
13
  export const zUnixTimestamp2000 = z
13
14
  .number()
14
15
  .int()
15
16
  .min(TS_2000)
16
17
  .max(TS_2500, 'Must be a UnixTimestamp number after 2000-01-01')
18
+ // .transform(v => v as UnixTimestamp)
17
19
  .describe('UnixTimestamp2000')
18
20
  export const zUnixTimestampMillis = z
19
21
  .number()
20
22
  .int()
21
23
  .min(0)
22
24
  .max(TS_2500 * 1000, 'Must be a UnixTimestampMillis number')
25
+ // .transform(v => v as UnixTimestampMillis)
23
26
  .describe('UnixTimestampMillis')
24
27
  export const zUnixTimestampMillis2000 = z
25
28
  .number()
26
29
  .int()
27
30
  .min(TS_2000 * 1000)
28
31
  .max(TS_2500 * 1000, 'Must be a UnixTimestampMillis number after 2000-01-01')
32
+ // .transform(v => v as UnixTimestampMillis)
29
33
  .describe('UnixTimestampMillis2000')
30
34
 
31
35
  export const zSemVer = z
@@ -33,18 +37,26 @@ export const zSemVer = z
33
37
  .regex(/^[0-9]+\.[0-9]+\.[0-9]+$/, 'Must be a SemVer string')
34
38
  .describe('SemVer')
35
39
 
36
- export const zIsoDateString = z
40
+ export const zIsoDate = z
37
41
  .string()
38
42
  .refine(v => {
39
43
  return /^\d{4}-\d{2}-\d{2}$/.test(v)
40
44
  }, 'Must be an IsoDateString')
45
+ // .transform(v => v as IsoDate)
41
46
  .describe('IsoDateString')
42
47
 
43
48
  export const zEmail = z
44
49
  .string()
45
50
  .trim()
46
- .email()
47
- .transform(s => s.toLowerCase())
51
+ .email() // keeping as-is, so trim happens before email validation
52
+ .transform(s => s.toLowerCase()) // breaks toJsonSchema
53
+ .describe('Email')
54
+
55
+ export const zEmailNoLowercase = z
56
+ .string()
57
+ .trim()
58
+ .email() // keeping as-is, so trim happens before email validation
59
+ // .transform(s => s.toLowerCase()) // breaks toJsonSchema
48
60
  .describe('Email')
49
61
 
50
62
  export const BASE62_REGEX = /^[a-zA-Z0-9]+$/
@@ -68,7 +80,7 @@ export const zJwt = z.string().regex(JWT_REGEX, 'Must be a JWT string').describe
68
80
 
69
81
  export const zId = z
70
82
  .string()
71
- .regex(/^[a-zA-Z0-9_]{6,64}$/, 'Must be an id string')
83
+ .regex(/^[a-zA-Z0-9_]{6,64}$/, 'Must be an id string (6 to 64 chars long)')
72
84
  .describe('IdString')
73
85
  export const zIdBase62 = z
74
86
  .string()
@@ -1,24 +1,26 @@
1
- import { ZodError, type ZodIssue, type ZodSchema } from 'zod'
1
+ import type { ZodError, ZodType } from 'zod/v4'
2
+ import type { ErrorData } from '../error/error.model.js'
3
+ import { AppError } from '../error/error.util.js'
2
4
  import { _stringify } from '../string/stringify.js'
3
5
 
4
6
  export interface ZodErrorResult<T> {
5
7
  success: false
6
8
  data?: T
7
- error: ZodValidationError<T>
9
+ error: ZodValidationError
8
10
  }
9
11
 
10
12
  export interface ZodSuccessResult<T> {
11
13
  success: true
12
14
  data: T
13
- error?: ZodValidationError<T>
15
+ error?: ZodValidationError
14
16
  }
15
17
 
16
- export function zIsValid<T>(value: T, schema: ZodSchema<T>): boolean {
18
+ export function zIsValid<T>(value: T, schema: ZodType<T>): boolean {
17
19
  const { success } = schema.safeParse(value)
18
20
  return success
19
21
  }
20
22
 
21
- export function zValidate<T>(value: T, schema: ZodSchema<T>): T {
23
+ export function zValidate<T>(value: T, schema: ZodType<T>): T {
22
24
  const r = zSafeValidate(value, schema)
23
25
  if (r.success) {
24
26
  return r.data
@@ -29,7 +31,7 @@ export function zValidate<T>(value: T, schema: ZodSchema<T>): T {
29
31
 
30
32
  export function zSafeValidate<T>(
31
33
  value: T,
32
- schema: ZodSchema<T>,
34
+ schema: ZodType<T>,
33
35
  // objectName?: string,
34
36
  ): ZodSuccessResult<T> | ZodErrorResult<T> {
35
37
  const r = schema.safeParse(value)
@@ -39,43 +41,51 @@ export function zSafeValidate<T>(
39
41
 
40
42
  return {
41
43
  success: false,
42
- error: new ZodValidationError<T>(r.error.issues, value, schema),
44
+ error: new ZodValidationError(r.error, value, schema),
43
45
  }
44
46
  }
45
47
 
46
- export class ZodValidationError<T> extends ZodError<T> {
47
- constructor(
48
- issues: ZodIssue[],
49
- public value: T,
50
- public schema: ZodSchema<T>,
51
- ) {
52
- super(issues)
53
- }
48
+ export interface ZodValidationErrorData extends ErrorData {
49
+ // issues: $ZodIssue[]
50
+ // joiValidationObjectName?: string
51
+ // joiValidationObjectId?: string
52
+ /**
53
+ * Error "annotation" is stripped in Error.message.
54
+ * This field contains the "full" annotation.
55
+ *
56
+ * This field is non-enumerable, won't be printed or included in JSON by default,
57
+ * but still accessible programmatically (via `err.data.annotation`) when needed!
58
+ */
59
+ // annotation?: string
60
+ }
54
61
 
55
- override get message(): string {
56
- return this.annotate()
62
+ export class ZodValidationError extends AppError<ZodValidationErrorData> {
63
+ constructor(zodError: ZodError, value: any, schema: ZodType) {
64
+ const message = createZodErrorMessage(zodError, schema, value)
65
+ // const message = z.prettifyError(zodError) // todo: consider adopting it instead
66
+ super(message, {}, { name: 'ZodValidationError' })
57
67
  }
68
+ }
58
69
 
59
- annotate(): string {
60
- let objectTitle = this.schema.description
70
+ function createZodErrorMessage<T>(err: ZodError<T>, schema: ZodType<T>, value: T): string {
71
+ let objectTitle = schema.description
61
72
 
62
- if (typeof this.value === 'object' && this.value) {
63
- const objectName = this.schema.description || this.value.constructor?.name
64
- const objectId = (this.value as any)['id'] as string
65
- objectTitle = [objectName, objectId].filter(Boolean).join('.')
66
- }
73
+ if (typeof value === 'object' && value) {
74
+ const objectName = schema.description || value.constructor?.name
75
+ const objectId = (value as any)['id'] as string
76
+ objectTitle = [objectName, objectId].filter(Boolean).join('.')
77
+ }
67
78
 
68
- objectTitle ||= 'data'
79
+ objectTitle ||= 'data'
69
80
 
70
- return [
71
- `Invalid ${objectTitle}`,
72
- '',
73
- 'Input:',
74
- _stringify(this.value),
75
- this.issues.length > 1 ? `\n${this.issues.length} issues:` : '',
76
- ...this.issues.slice(0, 100).map(i => {
77
- return [i.path.join('.'), i.message].filter(Boolean).join(': ')
78
- }),
79
- ].join('\n')
80
- }
81
+ return [
82
+ `Invalid ${objectTitle}`,
83
+ '',
84
+ 'Input:',
85
+ _stringify(value),
86
+ err.issues.length > 1 ? `\n${err.issues.length} issues:` : '',
87
+ ...err.issues.slice(0, 100).map(i => {
88
+ return [i.path.join('.'), i.message].filter(Boolean).join(': ')
89
+ }),
90
+ ].join('\n')
81
91
  }