@veloxts/validation 0.4.6 → 0.4.9
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 +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/schemas/serialization.d.ts +145 -0
- package/dist/schemas/serialization.d.ts.map +1 -0
- package/dist/schemas/serialization.js +119 -0
- package/dist/schemas/serialization.js.map +1 -0
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -33,4 +33,6 @@ export type { BaseEntity, IdParam, TimestampFields } from './schemas/common.js';
|
|
|
33
33
|
export { baseEntitySchema, booleanStringSchema, createIdSchema, datetimeSchema, emailSchema, idParamSchema, integerStringSchema, makePartial, nonEmptyStringSchema, numberStringSchema, omitFields, partialExcept, pickFields, timestampFieldsSchema, urlSchema, uuidSchema, } from './schemas/common.js';
|
|
34
34
|
export type { CursorPaginatedResponse, CursorPaginationInput, CursorPaginationMeta, PaginatedResponse, PaginationInput, PaginationMeta, } from './schemas/pagination.js';
|
|
35
35
|
export { calculateOffset, calculatePaginationMeta, createCursorPaginatedResponseSchema, createPaginatedResponse, createPaginatedResponseSchema, createPaginationSchema, cursorPaginationSchema, PAGINATION_DEFAULTS, paginationInputSchema, } from './schemas/pagination.js';
|
|
36
|
+
export type { InferWithTimestamps, OmitTimestamps, SerializedDates, WithOptional, } from './schemas/serialization.js';
|
|
37
|
+
export { dateToISOString, dateToISOStringNullable, dateToISOStringOptional, timestamps, timestampsWithSoftDelete, withTimestamps, } from './schemas/serialization.js';
|
|
36
38
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAMH,OAAO,EAAE,QAAQ,EAAE,KAAK,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AA6B,EAC7B,sBAAsB,EACtB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,yBAAyB,CAAC;AAMjC,YAAY,EACV,mBAAmB,EACnB,cAAc,EACd,eAAe,EACf,YAAY,GACb,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,uBAAuB,EACvB,UAAU,EACV,wBAAwB,EACxB,cAAc,GACf,MAAM,4BAA4B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -37,4 +37,5 @@ export { isSchema, isZodSchema, wrapSchema } from './types.js';
|
|
|
37
37
|
export { assertSchema, createTypeGuard, createValidator, formatZodErrors, parse, parseAll, safeParse, safeValidate, validate, validateAll, zodErrorToValidationError, } from './middleware.js';
|
|
38
38
|
export { baseEntitySchema, booleanStringSchema, createIdSchema, datetimeSchema, emailSchema, idParamSchema, integerStringSchema, makePartial, nonEmptyStringSchema, numberStringSchema, omitFields, partialExcept, pickFields, timestampFieldsSchema, urlSchema, uuidSchema, } from './schemas/common.js';
|
|
39
39
|
export { calculateOffset, calculatePaginationMeta, createCursorPaginatedResponseSchema, createPaginatedResponse, createPaginatedResponseSchema, createPaginationSchema, cursorPaginationSchema, PAGINATION_DEFAULTS, paginationInputSchema, } from './schemas/pagination.js';
|
|
40
|
+
export { dateToISOString, dateToISOStringNullable, dateToISOStringOptional, timestamps, timestampsWithSoftDelete, withTimestamps, } from './schemas/serialization.js';
|
|
40
41
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,gCAAgC;AAChC,yFAAyF;AACzF,OAAO,EAAE,QAAQ,EAAiC,CAAC,EAAE,MAAM,KAAK,CAAC;AAEjE,6CAA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GAAG,CAAC,CAAC;AAC/C,MAAM,WAAW,GAAG,OAAO,CAAC,iBAAiB,CAAwB,CAAC;AAEtE,iCAAiC;AACjC,MAAM,CAAC,MAAM,kBAAkB,GAAW,WAAW,CAAC,OAAO,IAAI,eAAe,CAAC;AAwBjF,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAW/D,wDAAwD;AACxD,iDAAiD;AACjD,OAAO,EACL,YAAY,EACZ,eAAe,EACf,eAAe,EACf,eAAe,EACf,KAAK,EACL,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,QAAQ,EACR,WAAW,EACX,yBAAyB,GAC1B,MAAM,iBAAiB,CAAC;AAOzB,OAAO,EACL,gBAAgB,EAChB,mBAAmB,EACnB,cAAc,EACd,cAAc,EACd,WAAW,EACX,aAAa,EACb,mBAAmB,EACnB,WAAW,EACX,oBAAoB,EACpB,kBAAkB,EAClB,UAAU,EACV,aAAa,EACb,UAAU,EACV,qBAAqB,EACrB,SAAS,EACT,UAAU,GACX,MAAM,qBAAqB,CAAC;AAc7B,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,mCAAmC,EACnC,uBAAuB,EACvB,6BAA6B,EAC7B,sBAAsB,EACtB,sBAAsB,EACtB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,yBAAyB,CAAC;AAYjC,OAAO,EACL,eAAe,EACf,uBAAuB,EACvB,uBAAuB,EACvB,UAAU,EACV,wBAAwB,EACxB,cAAc,GACf,MAAM,4BAA4B,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serialization Utilities for API Responses
|
|
3
|
+
*
|
|
4
|
+
* Provides type-safe Date → string transformation that works with
|
|
5
|
+
* Zod's output validation in the procedure system.
|
|
6
|
+
*
|
|
7
|
+
* @module schemas/serialization
|
|
8
|
+
*/
|
|
9
|
+
import { type ZodObject, type ZodRawShape, z } from 'zod';
|
|
10
|
+
/**
|
|
11
|
+
* Recursively transforms Date properties to string in a type
|
|
12
|
+
*
|
|
13
|
+
* This utility type mirrors the runtime transformation that happens
|
|
14
|
+
* when dates pass through Zod transforms.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import type { Prisma } from '@prisma/client';
|
|
19
|
+
*
|
|
20
|
+
* type DbUser = Prisma.UserGetPayload<{}>;
|
|
21
|
+
* // { id: string; name: string; createdAt: Date; updatedAt: Date }
|
|
22
|
+
*
|
|
23
|
+
* type ApiUser = SerializedDates<DbUser>;
|
|
24
|
+
* // { id: string; name: string; createdAt: string; updatedAt: string }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export type SerializedDates<T> = T extends Date ? string : T extends Array<infer U> ? Array<SerializedDates<U>> : T extends object ? {
|
|
28
|
+
[K in keyof T]: SerializedDates<T[K]>;
|
|
29
|
+
} : T;
|
|
30
|
+
/**
|
|
31
|
+
* Makes specified keys optional (useful for create inputs)
|
|
32
|
+
*/
|
|
33
|
+
export type WithOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
34
|
+
/**
|
|
35
|
+
* Omits timestamp fields from a type (useful for create/update inputs)
|
|
36
|
+
*/
|
|
37
|
+
export type OmitTimestamps<T> = Omit<T, 'createdAt' | 'updatedAt' | 'deletedAt'>;
|
|
38
|
+
/**
|
|
39
|
+
* Creates a date field that serializes to ISO string
|
|
40
|
+
*
|
|
41
|
+
* Use in output schemas. Accepts Date objects from Prisma
|
|
42
|
+
* and transforms them to ISO strings for JSON responses.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const UserSchema = z.object({
|
|
47
|
+
* id: z.string().uuid(),
|
|
48
|
+
* createdAt: dateToISOString(),
|
|
49
|
+
* updatedAt: dateToISOString(),
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export declare function dateToISOString(): z.ZodEffects<z.ZodDate, string, Date>;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a nullable date field that serializes to ISO string or null
|
|
56
|
+
*/
|
|
57
|
+
export declare function dateToISOStringNullable(): z.ZodEffects<z.ZodNullable<z.ZodDate>, string | null, Date | null>;
|
|
58
|
+
/**
|
|
59
|
+
* Creates an optional date field that serializes to ISO string or undefined
|
|
60
|
+
*/
|
|
61
|
+
export declare function dateToISOStringOptional(): z.ZodEffects<z.ZodOptional<z.ZodDate>, string | undefined, Date | undefined>;
|
|
62
|
+
/**
|
|
63
|
+
* Standard timestamp fields for entities
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const UserSchema = z.object({
|
|
68
|
+
* id: z.string().uuid(),
|
|
69
|
+
* name: z.string(),
|
|
70
|
+
* }).merge(timestamps);
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare const timestamps: ZodObject<{
|
|
74
|
+
createdAt: z.ZodEffects<z.ZodDate, string, Date>;
|
|
75
|
+
updatedAt: z.ZodEffects<z.ZodDate, string, Date>;
|
|
76
|
+
}, "strip", z.ZodTypeAny, {
|
|
77
|
+
createdAt: string;
|
|
78
|
+
updatedAt: string;
|
|
79
|
+
}, {
|
|
80
|
+
createdAt: Date;
|
|
81
|
+
updatedAt: Date;
|
|
82
|
+
}>;
|
|
83
|
+
/**
|
|
84
|
+
* Timestamp fields with soft delete support
|
|
85
|
+
*/
|
|
86
|
+
export declare const timestampsWithSoftDelete: ZodObject<{
|
|
87
|
+
createdAt: z.ZodEffects<z.ZodDate, string, Date>;
|
|
88
|
+
updatedAt: z.ZodEffects<z.ZodDate, string, Date>;
|
|
89
|
+
deletedAt: z.ZodEffects<z.ZodNullable<z.ZodDate>, string | null, Date | null>;
|
|
90
|
+
}, "strip", z.ZodTypeAny, {
|
|
91
|
+
createdAt: string;
|
|
92
|
+
updatedAt: string;
|
|
93
|
+
deletedAt: string | null;
|
|
94
|
+
}, {
|
|
95
|
+
createdAt: Date;
|
|
96
|
+
updatedAt: Date;
|
|
97
|
+
deletedAt: Date | null;
|
|
98
|
+
}>;
|
|
99
|
+
/**
|
|
100
|
+
* Configuration for withTimestamps
|
|
101
|
+
*/
|
|
102
|
+
interface TimestampConfig {
|
|
103
|
+
/** Include createdAt (default: true) */
|
|
104
|
+
createdAt?: boolean;
|
|
105
|
+
/** Include updatedAt (default: true) */
|
|
106
|
+
updatedAt?: boolean;
|
|
107
|
+
/** Include deletedAt for soft deletes (default: false) */
|
|
108
|
+
deletedAt?: boolean;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Extends a Zod object schema with timestamp fields
|
|
112
|
+
*
|
|
113
|
+
* This is the primary way to add serialized timestamps to entity schemas.
|
|
114
|
+
* Timestamps are automatically transformed from Date to ISO string.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* // Define business fields
|
|
119
|
+
* const UserFields = z.object({
|
|
120
|
+
* id: z.string().uuid(),
|
|
121
|
+
* name: z.string(),
|
|
122
|
+
* email: z.string().email(),
|
|
123
|
+
* });
|
|
124
|
+
*
|
|
125
|
+
* // Add timestamps (createdAt, updatedAt as strings)
|
|
126
|
+
* export const UserSchema = withTimestamps(UserFields);
|
|
127
|
+
*
|
|
128
|
+
* // With soft delete
|
|
129
|
+
* export const UserSchema = withTimestamps(UserFields, { deletedAt: true });
|
|
130
|
+
*
|
|
131
|
+
* // Without updatedAt
|
|
132
|
+
* export const UserSchema = withTimestamps(UserFields, { updatedAt: false });
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export declare function withTimestamps<T extends ZodRawShape>(schema: ZodObject<T>, config?: TimestampConfig): ZodObject<T & typeof timestampShape>;
|
|
136
|
+
declare const timestampShape: {
|
|
137
|
+
createdAt: z.ZodEffects<z.ZodDate, string, Date>;
|
|
138
|
+
updatedAt: z.ZodEffects<z.ZodDate, string, Date>;
|
|
139
|
+
};
|
|
140
|
+
/**
|
|
141
|
+
* Type helper to infer the output type of a schema with timestamps
|
|
142
|
+
*/
|
|
143
|
+
export type InferWithTimestamps<T extends ZodObject<ZodRawShape>> = z.infer<ReturnType<typeof withTimestamps<T['shape']>>>;
|
|
144
|
+
export {};
|
|
145
|
+
//# sourceMappingURL=serialization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialization.d.ts","sourceRoot":"","sources":["../../src/schemas/serialization.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,WAAW,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAM1D;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,CAAC,SAAS,IAAI,GAC3C,MAAM,GACN,CAAC,SAAS,KAAK,CAAC,MAAM,CAAC,CAAC,GACtB,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,GACzB,CAAC,SAAS,MAAM,GACd;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GACzC,CAAC,CAAC;AAEV;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAElF;;GAEG;AACH,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,WAAW,GAAG,WAAW,GAAG,WAAW,CAAC,CAAC;AAMjF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,eAAe,0CAE9B;AAED;;GAEG;AACH,wBAAgB,uBAAuB,uEAKtC;AAED;;GAEG;AACH,wBAAgB,uBAAuB,iFAKtC;AAMD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,UAAU;;;;;;;;;EAGrB,CAAC;AAEH;;GAEG;AACH,eAAO,MAAM,wBAAwB;;;;;;;;;;;;EAInC,CAAC;AAMH;;GAEG;AACH,UAAU,eAAe;IACvB,wCAAwC;IACxC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,wCAAwC;IACxC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,0DAA0D;IAC1D,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,WAAW,EAClD,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,MAAM,GAAE,eAAoB,GAC3B,SAAS,CAAC,CAAC,GAAG,OAAO,cAAc,CAAC,CAgBtC;AAGD,QAAA,MAAM,cAAc;;;CAGnB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,mBAAmB,CAAC,CAAC,SAAS,SAAS,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,KAAK,CACzE,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAC9C,CAAC"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serialization Utilities for API Responses
|
|
3
|
+
*
|
|
4
|
+
* Provides type-safe Date → string transformation that works with
|
|
5
|
+
* Zod's output validation in the procedure system.
|
|
6
|
+
*
|
|
7
|
+
* @module schemas/serialization
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
// ============================================================================
|
|
11
|
+
// Date Field Helpers
|
|
12
|
+
// ============================================================================
|
|
13
|
+
/**
|
|
14
|
+
* Creates a date field that serializes to ISO string
|
|
15
|
+
*
|
|
16
|
+
* Use in output schemas. Accepts Date objects from Prisma
|
|
17
|
+
* and transforms them to ISO strings for JSON responses.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* const UserSchema = z.object({
|
|
22
|
+
* id: z.string().uuid(),
|
|
23
|
+
* createdAt: dateToISOString(),
|
|
24
|
+
* updatedAt: dateToISOString(),
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function dateToISOString() {
|
|
29
|
+
return z.coerce.date().transform((date) => date.toISOString());
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Creates a nullable date field that serializes to ISO string or null
|
|
33
|
+
*/
|
|
34
|
+
export function dateToISOStringNullable() {
|
|
35
|
+
return z.coerce
|
|
36
|
+
.date()
|
|
37
|
+
.nullable()
|
|
38
|
+
.transform((date) => (date ? date.toISOString() : null));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Creates an optional date field that serializes to ISO string or undefined
|
|
42
|
+
*/
|
|
43
|
+
export function dateToISOStringOptional() {
|
|
44
|
+
return z.coerce
|
|
45
|
+
.date()
|
|
46
|
+
.optional()
|
|
47
|
+
.transform((date) => (date ? date.toISOString() : undefined));
|
|
48
|
+
}
|
|
49
|
+
// ============================================================================
|
|
50
|
+
// Timestamp Schema Presets
|
|
51
|
+
// ============================================================================
|
|
52
|
+
/**
|
|
53
|
+
* Standard timestamp fields for entities
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* const UserSchema = z.object({
|
|
58
|
+
* id: z.string().uuid(),
|
|
59
|
+
* name: z.string(),
|
|
60
|
+
* }).merge(timestamps);
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export const timestamps = z.object({
|
|
64
|
+
createdAt: dateToISOString(),
|
|
65
|
+
updatedAt: dateToISOString(),
|
|
66
|
+
});
|
|
67
|
+
/**
|
|
68
|
+
* Timestamp fields with soft delete support
|
|
69
|
+
*/
|
|
70
|
+
export const timestampsWithSoftDelete = z.object({
|
|
71
|
+
createdAt: dateToISOString(),
|
|
72
|
+
updatedAt: dateToISOString(),
|
|
73
|
+
deletedAt: dateToISOStringNullable(),
|
|
74
|
+
});
|
|
75
|
+
/**
|
|
76
|
+
* Extends a Zod object schema with timestamp fields
|
|
77
|
+
*
|
|
78
|
+
* This is the primary way to add serialized timestamps to entity schemas.
|
|
79
|
+
* Timestamps are automatically transformed from Date to ISO string.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // Define business fields
|
|
84
|
+
* const UserFields = z.object({
|
|
85
|
+
* id: z.string().uuid(),
|
|
86
|
+
* name: z.string(),
|
|
87
|
+
* email: z.string().email(),
|
|
88
|
+
* });
|
|
89
|
+
*
|
|
90
|
+
* // Add timestamps (createdAt, updatedAt as strings)
|
|
91
|
+
* export const UserSchema = withTimestamps(UserFields);
|
|
92
|
+
*
|
|
93
|
+
* // With soft delete
|
|
94
|
+
* export const UserSchema = withTimestamps(UserFields, { deletedAt: true });
|
|
95
|
+
*
|
|
96
|
+
* // Without updatedAt
|
|
97
|
+
* export const UserSchema = withTimestamps(UserFields, { updatedAt: false });
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export function withTimestamps(schema, config = {}) {
|
|
101
|
+
const { createdAt = true, updatedAt = true, deletedAt = false } = config;
|
|
102
|
+
const extensions = {};
|
|
103
|
+
if (createdAt) {
|
|
104
|
+
extensions.createdAt = dateToISOString();
|
|
105
|
+
}
|
|
106
|
+
if (updatedAt) {
|
|
107
|
+
extensions.updatedAt = dateToISOString();
|
|
108
|
+
}
|
|
109
|
+
if (deletedAt) {
|
|
110
|
+
extensions.deletedAt = dateToISOStringNullable();
|
|
111
|
+
}
|
|
112
|
+
return schema.extend(extensions);
|
|
113
|
+
}
|
|
114
|
+
// Type helper for withTimestamps return type inference
|
|
115
|
+
const timestampShape = {
|
|
116
|
+
createdAt: dateToISOString(),
|
|
117
|
+
updatedAt: dateToISOString(),
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=serialization.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serialization.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B,SAAS,EAAE,uBAAuB,EAAE;CACrC,CAAC,CAAC;AAkBH;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,cAAc,CAC5B,MAAoB,EACpB,SAA0B,EAAE;IAE5B,MAAM,EAAE,SAAS,GAAG,IAAI,EAAE,SAAS,GAAG,IAAI,EAAE,SAAS,GAAG,KAAK,EAAE,GAAG,MAAM,CAAC;IAEzE,MAAM,UAAU,GAAgB,EAAE,CAAC;IAEnC,IAAI,SAAS,EAAE,CAAC;QACd,UAAU,CAAC,SAAS,GAAG,eAAe,EAAE,CAAC;IAC3C,CAAC;IACD,IAAI,SAAS,EAAE,CAAC;QACd,UAAU,CAAC,SAAS,GAAG,eAAe,EAAE,CAAC;IAC3C,CAAC;IACD,IAAI,SAAS,EAAE,CAAC;QACd,UAAU,CAAC,SAAS,GAAG,uBAAuB,EAAE,CAAC;IACnD,CAAC;IAED,OAAO,MAAM,CAAC,MAAM,CAAC,UAAU,CAAyC,CAAC;AAC3E,CAAC;AAED,uDAAuD;AACvD,MAAM,cAAc,GAAG;IACrB,SAAS,EAAE,eAAe,EAAE;IAC5B,SAAS,EAAE,eAAe,EAAE;CAC7B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veloxts/validation",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.9",
|
|
4
4
|
"description": "Zod integration and validation middleware for VeloxTS framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"zod": "3.24.4",
|
|
26
|
-
"@veloxts/core": "0.4.
|
|
26
|
+
"@veloxts/core": "0.4.9"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@vitest/coverage-v8": "4.0.15",
|