@lyxa.ai/types 1.1.67 → 1.1.68
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/README.md +1 -1
- package/package.json +1 -1
- package/utilities/validation/common-validation.d.ts +78 -2
- package/utilities/validation/filter-schema.factory.d.ts +5 -4
- package/utilities/validation/filter-schema.factory.js +11 -11
- package/utilities/validation/filter-schema.factory.js.map +1 -1
- package/utilities/validation/global-validation.d.ts +38 -14
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -646,7 +646,7 @@ export declare const createResponseSchema: <T>(schema: z.ZodSchema<T>) => z.ZodO
|
|
|
646
646
|
totalPages: number;
|
|
647
647
|
} | undefined;
|
|
648
648
|
}>, z.ZodType<T, z.ZodTypeDef, T>]>>;
|
|
649
|
-
}>, any> extends infer T_1 ? { [k in keyof T_1]:
|
|
649
|
+
}>, any> extends infer T_1 ? { [k in keyof T_1]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<{
|
|
650
650
|
success: z.ZodBoolean;
|
|
651
651
|
message: z.ZodString | z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, string, string>;
|
|
652
652
|
data: z.ZodOptional<z.ZodUnion<[z.ZodObject<{
|
|
@@ -684,7 +684,83 @@ export declare const createResponseSchema: <T>(schema: z.ZodSchema<T>) => z.ZodO
|
|
|
684
684
|
totalPages: number;
|
|
685
685
|
} | undefined;
|
|
686
686
|
}>, z.ZodType<T, z.ZodTypeDef, T>]>>;
|
|
687
|
-
}>
|
|
687
|
+
}>, any>[k]; } : never, z.baseObjectInputType<{
|
|
688
|
+
success: z.ZodBoolean;
|
|
689
|
+
message: z.ZodString | z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, string, string>;
|
|
690
|
+
data: z.ZodOptional<z.ZodUnion<[z.ZodObject<{
|
|
691
|
+
metadata: z.ZodOptional<z.ZodObject<{
|
|
692
|
+
page: z.ZodNumber;
|
|
693
|
+
size: z.ZodNumber;
|
|
694
|
+
totalElements: z.ZodNumber;
|
|
695
|
+
totalPages: z.ZodNumber;
|
|
696
|
+
}, "strip", z.ZodTypeAny, {
|
|
697
|
+
size: number;
|
|
698
|
+
page: number;
|
|
699
|
+
totalElements: number;
|
|
700
|
+
totalPages: number;
|
|
701
|
+
}, {
|
|
702
|
+
size: number;
|
|
703
|
+
page: number;
|
|
704
|
+
totalElements: number;
|
|
705
|
+
totalPages: number;
|
|
706
|
+
}>>;
|
|
707
|
+
documents: z.ZodArray<z.ZodType<T, z.ZodTypeDef, T>, "many">;
|
|
708
|
+
}, "strip", z.ZodTypeAny, {
|
|
709
|
+
documents: T[];
|
|
710
|
+
metadata?: {
|
|
711
|
+
size: number;
|
|
712
|
+
page: number;
|
|
713
|
+
totalElements: number;
|
|
714
|
+
totalPages: number;
|
|
715
|
+
} | undefined;
|
|
716
|
+
}, {
|
|
717
|
+
documents: T[];
|
|
718
|
+
metadata?: {
|
|
719
|
+
size: number;
|
|
720
|
+
page: number;
|
|
721
|
+
totalElements: number;
|
|
722
|
+
totalPages: number;
|
|
723
|
+
} | undefined;
|
|
724
|
+
}>, z.ZodType<T, z.ZodTypeDef, T>]>>;
|
|
725
|
+
}> extends infer T_2 ? { [k_1 in keyof T_2]: z.baseObjectInputType<{
|
|
726
|
+
success: z.ZodBoolean;
|
|
727
|
+
message: z.ZodString | z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, string, string>;
|
|
728
|
+
data: z.ZodOptional<z.ZodUnion<[z.ZodObject<{
|
|
729
|
+
metadata: z.ZodOptional<z.ZodObject<{
|
|
730
|
+
page: z.ZodNumber;
|
|
731
|
+
size: z.ZodNumber;
|
|
732
|
+
totalElements: z.ZodNumber;
|
|
733
|
+
totalPages: z.ZodNumber;
|
|
734
|
+
}, "strip", z.ZodTypeAny, {
|
|
735
|
+
size: number;
|
|
736
|
+
page: number;
|
|
737
|
+
totalElements: number;
|
|
738
|
+
totalPages: number;
|
|
739
|
+
}, {
|
|
740
|
+
size: number;
|
|
741
|
+
page: number;
|
|
742
|
+
totalElements: number;
|
|
743
|
+
totalPages: number;
|
|
744
|
+
}>>;
|
|
745
|
+
documents: z.ZodArray<z.ZodType<T, z.ZodTypeDef, T>, "many">;
|
|
746
|
+
}, "strip", z.ZodTypeAny, {
|
|
747
|
+
documents: T[];
|
|
748
|
+
metadata?: {
|
|
749
|
+
size: number;
|
|
750
|
+
page: number;
|
|
751
|
+
totalElements: number;
|
|
752
|
+
totalPages: number;
|
|
753
|
+
} | undefined;
|
|
754
|
+
}, {
|
|
755
|
+
documents: T[];
|
|
756
|
+
metadata?: {
|
|
757
|
+
size: number;
|
|
758
|
+
page: number;
|
|
759
|
+
totalElements: number;
|
|
760
|
+
totalPages: number;
|
|
761
|
+
} | undefined;
|
|
762
|
+
}>, z.ZodType<T, z.ZodTypeDef, T>]>>;
|
|
763
|
+
}>[k_1]; } : never>;
|
|
688
764
|
export type PaginatedDTO<T> = {
|
|
689
765
|
metadata: DTO<typeof metadataSchema>;
|
|
690
766
|
documents: T[];
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { z, ZodTypeAny } from 'zod';
|
|
2
2
|
type ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];
|
|
3
|
-
export interface FilterSchemaConfig<TSelect extends ReadonlyNonEmptyArray<string> | [] = [], TPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {}, TSearch extends ReadonlyNonEmptyArray<string> | [] = [], TQuery extends Record<string, ZodTypeAny> = {}> {
|
|
3
|
+
export interface FilterSchemaConfig<TSelect extends ReadonlyNonEmptyArray<string> | [] = [], TPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {}, TSearch extends ReadonlyNonEmptyArray<string> | [] = [], TQuery extends Record<string, ZodTypeAny> = {}, TSort extends ReadonlyNonEmptyArray<string> | [] = []> {
|
|
4
4
|
selectableFields?: TSelect;
|
|
5
5
|
populatableFields?: TPopulate;
|
|
6
6
|
searchableFields?: TSearch;
|
|
7
7
|
queryableFields?: TQuery;
|
|
8
|
+
sortableFields?: TSort;
|
|
8
9
|
maxSize?: number;
|
|
9
10
|
defaultSize?: number;
|
|
10
11
|
}
|
|
@@ -21,10 +22,10 @@ export type PopulateObject<TPath extends string, TSelectFields extends ReadonlyN
|
|
|
21
22
|
type PopulateUnion<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {
|
|
22
23
|
[K in keyof TPopulate]: PopulateObject<K & string, TPopulate[K]>;
|
|
23
24
|
}[keyof TPopulate];
|
|
24
|
-
export type FilterDTO<TSelect extends ReadonlyNonEmptyArray<string> | [] = [], TPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {}, TSearch extends ReadonlyNonEmptyArray<string> | [] = [], TQuery extends Record<string, ZodTypeAny> = {}> = {
|
|
25
|
+
export type FilterDTO<TSelect extends ReadonlyNonEmptyArray<string> | [] = [], TPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {}, TSearch extends ReadonlyNonEmptyArray<string> | [] = [], TQuery extends Record<string, ZodTypeAny> = {}, TSort extends ReadonlyNonEmptyArray<string> | [] = []> = {
|
|
25
26
|
size?: number;
|
|
26
27
|
page?: number;
|
|
27
|
-
sort?: Record<
|
|
28
|
+
sort?: TSort extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSort[number], 1 | -1>> : never;
|
|
28
29
|
select?: TSelect extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSelect[number], 0 | 1>> : never;
|
|
29
30
|
populate?: PopulateUnion<TPopulate>[];
|
|
30
31
|
search?: TSearch extends ReadonlyNonEmptyArray<string> ? {
|
|
@@ -35,5 +36,5 @@ export type FilterDTO<TSelect extends ReadonlyNonEmptyArray<string> | [] = [], T
|
|
|
35
36
|
[K in keyof TQuery]?: z.infer<TQuery[K]>;
|
|
36
37
|
};
|
|
37
38
|
};
|
|
38
|
-
export declare function createFilterSchema<TSelect extends ReadonlyNonEmptyArray<string> | [] = [], TPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {}, TSearch extends ReadonlyNonEmptyArray<string> | [] = [], TQuery extends Record<string, ZodTypeAny> = {}>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch, TQuery>): z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery>>;
|
|
39
|
+
export declare function createFilterSchema<TSelect extends ReadonlyNonEmptyArray<string> | [] = [], TPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {}, TSearch extends ReadonlyNonEmptyArray<string> | [] = [], TQuery extends Record<string, ZodTypeAny> = {}, TSort extends ReadonlyNonEmptyArray<string> | [] = []>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch, TQuery, TSort>): z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery, TSort>>;
|
|
39
40
|
export {};
|
|
@@ -4,13 +4,17 @@ exports.createFilterSchema = createFilterSchema;
|
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const sortSchema = zod_1.z.record(zod_1.z.string(), zod_1.z.union([zod_1.z.literal(1), zod_1.z.literal(-1)]));
|
|
6
6
|
function createFilterSchema(config) {
|
|
7
|
-
const { selectableFields, populatableFields, searchableFields, queryableFields, maxSize = 100, defaultSize = 10, } = config;
|
|
7
|
+
const { selectableFields, populatableFields, searchableFields, queryableFields, sortableFields, maxSize = 100, defaultSize = 10, } = config;
|
|
8
8
|
const populateKeys = populatableFields ? Object.keys(populatableFields) : [];
|
|
9
9
|
const schemaShape = {
|
|
10
10
|
size: zod_1.z.number().min(1).max(maxSize).optional().default(defaultSize),
|
|
11
11
|
page: zod_1.z.number().min(1).optional().default(1),
|
|
12
|
-
sort: sortSchema.optional(),
|
|
13
12
|
};
|
|
13
|
+
if (sortableFields?.length) {
|
|
14
|
+
schemaShape.sort = zod_1.z
|
|
15
|
+
.record(zod_1.z.enum([...sortableFields]), zod_1.z.union([zod_1.z.literal(1), zod_1.z.literal(-1)]))
|
|
16
|
+
.optional();
|
|
17
|
+
}
|
|
14
18
|
if (selectableFields?.length) {
|
|
15
19
|
schemaShape.select = zod_1.z
|
|
16
20
|
.record(zod_1.z.enum([...selectableFields]), zod_1.z.union([zod_1.z.literal(0), zod_1.z.literal(1)]))
|
|
@@ -59,14 +63,7 @@ function createFilterSchema(config) {
|
|
|
59
63
|
return zod_1.z.object(schemaShape);
|
|
60
64
|
}
|
|
61
65
|
const TicketFilterSchema = createFilterSchema({
|
|
62
|
-
selectableFields: [
|
|
63
|
-
'_id',
|
|
64
|
-
'ticketId',
|
|
65
|
-
'type',
|
|
66
|
-
'status',
|
|
67
|
-
'createdAt',
|
|
68
|
-
'updatedAt',
|
|
69
|
-
],
|
|
66
|
+
selectableFields: ['_id', 'ticketId', 'type', 'status', 'createdAt', 'updatedAt'],
|
|
70
67
|
populatableFields: {
|
|
71
68
|
chatroom: ['_id', 'lastMessage', 'participants', 'unreadCount'],
|
|
72
69
|
client: ['_id', 'name', 'profilePhoto', 'type'],
|
|
@@ -74,6 +71,7 @@ const TicketFilterSchema = createFilterSchema({
|
|
|
74
71
|
order: ['_id', 'orderId', 'shop', 'total'],
|
|
75
72
|
},
|
|
76
73
|
searchableFields: ['ticketId'],
|
|
74
|
+
sortableFields: ['createdAt', 'updatedAt', 'ticketId', 'status'],
|
|
77
75
|
queryableFields: {
|
|
78
76
|
status: zod_1.z.enum(['OPEN', 'CLOSED', 'PENDING']),
|
|
79
77
|
type: zod_1.z.enum(['ORDER', 'GENERAL', 'COMPLAINT']),
|
|
@@ -81,5 +79,7 @@ const TicketFilterSchema = createFilterSchema({
|
|
|
81
79
|
clientType: zod_1.z.enum(['USER', 'SHOP']),
|
|
82
80
|
},
|
|
83
81
|
});
|
|
84
|
-
const ticket = {
|
|
82
|
+
const ticket = {
|
|
83
|
+
sort: { createdAt: -1, status: 1 },
|
|
84
|
+
};
|
|
85
85
|
//# sourceMappingURL=filter-schema.factory.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter-schema.factory.js","sourceRoot":"/","sources":["utilities/validation/filter-schema.factory.ts"],"names":[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gB,EAAE,CAAC,UAAU,CAAU;IAEvC,eAAe,EAAE;QAChB,MAAM,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;QAC7C,IAAI,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,SAAS,EAAE,WAAW,CAAC,CAAC;QAC/C,MAAM,EAAE,OAAC,CAAC,MAAM,EAAE;QAClB,UAAU,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KACpC;CACD,CAAC,CAAC;AAIH,MAAM,MAAM,GAAoB,EAE/B,CAAA","sourcesContent":["import { z, ZodTypeAny } from 'zod';\n\ntype ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];\n\nexport interface FilterSchemaConfig<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> {\n\tselectableFields?: TSelect;\n\tpopulatableFields?: TPopulate;\n\tsearchableFields?: TSearch;\n\tqueryableFields?: TQuery;\n\tmaxSize?: number;\n\tdefaultSize?: number;\n}\n\n// Populate object type\nexport type PopulateObject<\n\tTPath extends string,\n\tTSelectFields extends ReadonlyNonEmptyArray<string>\n> = {\n\tpath: TPath;\n\tselect?: Partial<Record<TSelectFields[number], 0 | 1>>;\n\tmatch?: Record<string, any>;\n\toptions?: { sort?: Record<string, 1 | -1>; limit?: number; skip?: number };\n};\n\n// Union of all possible populate objects based on populatableFields\ntype PopulateUnion<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {\n\t[K in keyof TPopulate]: PopulateObject<K & string, TPopulate[K]>;\n}[keyof TPopulate];\n\n// DTO type\nexport type FilterDTO<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n> = {\n\tsize?: number;\n\tpage?: number;\n\tsort?: Record<string, 1 | -1>;\n\tselect?: TSelect extends ReadonlyNonEmptyArray<string>\n\t\t? Partial<Record<TSelect[number], 0 | 1>>\n\t\t: never;\n\tpopulate?: PopulateUnion<TPopulate>[];\n\tsearch?: TSearch extends ReadonlyNonEmptyArray<string>\n\t\t? { searchKey: string; searchFields?: TSearch[number][] }\n\t\t: never;\n\tquery?: { [K in keyof TQuery]?: z.infer<TQuery[K]> };\n};\n\nconst sortSchema = z.record(z.string(), z.union([z.literal(1), z.literal(-1)]));\n\nexport function createFilterSchema<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch, TQuery>) {\n\tconst {\n\t\tselectableFields,\n\t\tpopulatableFields,\n\t\tsearchableFields,\n\t\tqueryableFields,\n\t\tmaxSize = 100,\n\t\tdefaultSize = 10,\n\t} = config;\n\n\tconst populateKeys = populatableFields ? (Object.keys(populatableFields) as (keyof TPopulate)[]) : [];\n\n\tconst schemaShape: Record<string, any> = {\n\t\tsize: z.number().min(1).max(maxSize).optional().default(defaultSize),\n\t\tpage: z.number().min(1).optional().default(1),\n\t\tsort: sortSchema.optional(),\n\t};\n\n\t// Top-level select\n\tif (selectableFields?.length) {\n\t\tschemaShape.select = z\n\t\t\t.record(\n\t\t\t\tz.enum([...selectableFields] as [string, ...string[]]),\n\t\t\t\tz.union([z.literal(0), z.literal(1)]),\n\t\t\t)\n\t\t\t.optional();\n\t}\n\n\t// Populate as array of objects with strict typing per path\n\tif (populateKeys.length) {\n\t\tconst populateSchemas: ZodTypeAny[] = [];\n\n\t\tfor (const path of populateKeys) {\n\t\t\tconst allowedSelects = populatableFields![path] as ReadonlyNonEmptyArray<string>;\n\n\t\t\tconst populateSchema = z.object({\n\t\t\t\tpath: z.literal(path as string),\n\t\t\t\tselect: z\n\t\t\t\t\t.record(z.enum([...allowedSelects] as [string, ...string[]]), z.union([z.literal(0), z.literal(1)]))\n\t\t\t\t\t.optional(),\n\t\t\t\tmatch: z.record(z.string(), z.any()).optional(),\n\t\t\t\toptions: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tsort: sortSchema.optional(),\n\t\t\t\t\t\tlimit: z.number().optional(),\n\t\t\t\t\t\tskip: z.number().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t});\n\n\t\t\tpopulateSchemas.push(populateSchema);\n\t\t}\n\n\t\tconst unionSchema =\n\t\t\tpopulateSchemas.length === 1\n\t\t\t\t? populateSchemas[0]\n\t\t\t\t: z.union(populateSchemas as [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]);\n\n\t\tschemaShape.populate = z.array(unionSchema).optional();\n\t}\n\n\t// Search\n\tif (searchableFields?.length) {\n\t\tschemaShape.search = z\n\t\t\t.object({\n\t\t\t\tsearchKey: z.string(),\n\t\t\t\tsearchFields: z.array(z.enum([...searchableFields] as [string, ...string[]])).optional(),\n\t\t\t})\n\t\t\t.optional();\n\t}\n\n\t// Query\n\tif (queryableFields && Object.keys(queryableFields).length) {\n\t\tconst queryShape: Record<string, ZodTypeAny> = {};\n\t\tfor (const key in queryableFields) {\n\t\t\tqueryShape[key] = queryableFields[key].optional();\n\t\t}\n\t\tschemaShape.query = z.object(queryShape).optional();\n\t}\n\n\treturn z.object(schemaShape) as z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery>>;\n}\n\n\nconst TicketFilterSchema = createFilterSchema({\n\t// Top-level document fields that can be selected\n\tselectableFields: [\n\t\t'_id',\n\t\t'ticketId',\n\t\t'type',\n\t\t'status',\n\t\t'createdAt',\n\t\t'updatedAt',\n\t] as const,\n\n\t// Each populate path has its own allowed select fields\n\tpopulatableFields: {\n\t\tchatroom: ['_id', 'lastMessage', 'participants', 'unreadCount'] as const,\n\t\tclient: ['_id', 'name', 'profilePhoto', 'type'] as const,\n\t\tadmin: ['_id', 'name', 'profilePhoto'] as const,\n\t\torder: ['_id', 'orderId', 'shop', 'total'] as const,\n\t} as const,\n\n\tsearchableFields: ['ticketId'] as const,\n\n\tqueryableFields: {\n\t\tstatus: z.enum(['OPEN', 'CLOSED', 'PENDING']),\n\t\ttype: z.enum(['ORDER', 'GENERAL', 'COMPLAINT']),\n\t\tclient: z.string(),\n\t\tclientType: z.enum(['USER', 'SHOP']),\n\t},\n});\n\ntype TicketFilterDTO = z.infer<typeof TicketFilterSchema>;\n\nconst ticket: TicketFilterDTO = {\n\n}\n\n\n/**\n * EXAMPLE USAGE\n *\n * const TicketFilterSchema = createFilterSchema({\n * // Top-level document fields that can be selected\n * selectableFields: [\n * '_id',\n * 'ticketId',\n * 'type',\n * 'status',\n * 'createdAt',\n * 'updatedAt',\n * ] as const,\n *\n * // Each populate path has its own allowed select fields\n * populatableFields: {\n * chatroom: ['_id', 'lastMessage', 'participants', 'unreadCount'] as const,\n * client: ['_id', 'name', 'profilePhoto', 'type'] as const,\n * admin: ['_id', 'name', 'profilePhoto'] as const,\n * order: ['_id', 'orderId', 'shop', 'total'] as const,\n * } as const,\n *\n * searchableFields: ['ticketId'] as const,\n *\n * queryableFields: {\n * status: z.enum(['OPEN', 'CLOSED', 'PENDING']),\n * type: z.enum(['ORDER', 'GENERAL', 'COMPLAINT']),\n * client: z.string(),\n * clientType: z.enum(['USER', 'SHOP']),\n * },\n * });\n *\n * type TicketFilterDTO = z.infer<typeof TicketFilterSchema>;\n *\n * // ✅ VALID EXAMPLES\n *\n * // Example 1: Select only top-level fields\n * const filter1: TicketFilterDTO = {\n * select: { ticketId: 1, status: 1, createdAt: 1 },\n * query: { status: 'OPEN' },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 2: Populate client with specific fields\n * const filter2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { name: 1, profilePhoto: 1 }, // Uses client's allowed fields\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 3: Populate multiple relations\n * const filter3: TicketFilterDTO = {\n * populate: [\n * { path: 'client', select: { name: 1, profilePhoto: 1 } },\n * { path: 'order', select: { orderId: 1, total: 1 } },\n * { path: 'chatroom', select: { lastMessage: 1, unreadCount: 1 } },\n * ],\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 4: Complex query with search\n * const filter4: TicketFilterDTO = {\n * query: { status: 'OPEN', type: 'ORDER' },\n * search: { searchKey: 'TKT-123', searchFields: ['ticketId'] },\n * populate: { path: 'client', select: { name: 1 } },\n * sort: { createdAt: -1 },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 5: Populate with match and options\n * const filter5: TicketFilterDTO = {\n * populate: {\n * path: 'order',\n * select: { orderId: 1, shop: 1 },\n * match: { status: 'DELIVERED' },\n * options: { sort: { createdAt: -1 }, limit: 10 },\n * },\n * page: 1,\n * size: 20,\n * };\n *\n * // ❌ INVALID EXAMPLES (TypeScript will reject)\n *\n * const invalid1: TicketFilterDTO = {\n * select: { invalidField: 1 }, // ❌ not in selectableFields\n * };\n *\n * const invalid2: TicketFilterDTO = {\n * populate: {\n * path: 'client',\n * select: { email: 1 }, // ❌ email not in client's populatableFields\n * },\n * };\n *\n * const invalid3: TicketFilterDTO = {\n * populate: {\n * path: 'invalidPath', // ❌ path doesn't exist\n * select: { name: 1 },\n * },\n * };\n *\n * const invalid4: TicketFilterDTO = {\n * query: { status: 'INVALID_STATUS' }, // ❌ not in enum\n * }\n */"]}
|
|
1
|
+
{"version":3,"file":"filter-schema.factory.js","sourceRoot":"/","sources":["utilities/validation/filter-schema.factory.ts"],"names":[],"mappings":";;AAsDA,gDA2FC;AAjJD,6BAAoC;AAoDpC,MAAM,UAAU,GAAG,OAAC,CAAC,MAAM,CAAC,OAAC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CAAC,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,CAAU;QACxD,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,cAAc,CAAU;QAC/C,KAAK,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,CAAU;KAC1C;IAEV,gBAAgB,EAAE,CAAC,UAAU,CAAU;IAEvC,cAAc,EAAE,CAAC,WAAW,EAAE,WAAW,EAAE,UAAU,EAAE,QAAQ,CAAU;IAEzE,eAAe,EAAE;QAChB,MAAM,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC;QAC7C,IAAI,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,SAAS,EAAE,WAAW,CAAC,CAAC;QAC/C,MAAM,EAAE,OAAC,CAAC,MAAM,EAAE;QAClB,UAAU,EAAE,OAAC,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;KACpC;CACD,CAAC,CAAC;AAIH,MAAM,MAAM,GAAoB;IAC/B,IAAI,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE;CAClC,CAAC","sourcesContent":["import { z, ZodTypeAny } from 'zod';\n\ntype ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];\n\nexport interface FilterSchemaConfig<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n\tTSort extends ReadonlyNonEmptyArray<string> | [] = [],\n> {\n\tselectableFields?: TSelect;\n\tpopulatableFields?: TPopulate;\n\tsearchableFields?: TSearch;\n\tqueryableFields?: TQuery;\n\tsortableFields?: TSort;\n\tmaxSize?: number;\n\tdefaultSize?: number;\n}\n\n// Populate object type\nexport type PopulateObject<TPath extends string, TSelectFields extends ReadonlyNonEmptyArray<string>> = {\n\tpath: TPath;\n\tselect?: Partial<Record<TSelectFields[number], 0 | 1>>;\n\tmatch?: Record<string, any>;\n\toptions?: { sort?: Record<string, 1 | -1>; limit?: number; skip?: number };\n};\n\n// Union of all possible populate objects based on populatableFields\ntype PopulateUnion<TPopulate extends Record<string, ReadonlyNonEmptyArray<string>>> = {\n\t[K in keyof TPopulate]: PopulateObject<K & string, TPopulate[K]>;\n}[keyof TPopulate];\n\n// DTO type\nexport type FilterDTO<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n\tTSort extends ReadonlyNonEmptyArray<string> | [] = [],\n> = {\n\tsize?: number;\n\tpage?: number;\n\tsort?: TSort extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSort[number], 1 | -1>> : never;\n\tselect?: TSelect extends ReadonlyNonEmptyArray<string> ? Partial<Record<TSelect[number], 0 | 1>> : never;\n\tpopulate?: PopulateUnion<TPopulate>[];\n\tsearch?: TSearch extends ReadonlyNonEmptyArray<string>\n\t\t? { searchKey: string; searchFields?: TSearch[number][] }\n\t\t: never;\n\tquery?: { [K in keyof TQuery]?: z.infer<TQuery[K]> };\n};\n\nconst sortSchema = z.record(z.string(), z.union([z.literal(1), z.literal(-1)]));\n\nexport function createFilterSchema<\n\tTSelect extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTPopulate extends Record<string, ReadonlyNonEmptyArray<string>> | {} = {},\n\tTSearch extends ReadonlyNonEmptyArray<string> | [] = [],\n\tTQuery extends Record<string, ZodTypeAny> = {},\n\tTSort extends ReadonlyNonEmptyArray<string> | [] = [],\n>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch, TQuery, TSort>) {\n\tconst {\n\t\tselectableFields,\n\t\tpopulatableFields,\n\t\tsearchableFields,\n\t\tqueryableFields,\n\t\tsortableFields,\n\t\tmaxSize = 100,\n\t\tdefaultSize = 10,\n\t} = config;\n\n\tconst populateKeys = populatableFields ? (Object.keys(populatableFields) as (keyof TPopulate)[]) : [];\n\n\tconst schemaShape: Record<string, any> = {\n\t\tsize: z.number().min(1).max(maxSize).optional().default(defaultSize),\n\t\tpage: z.number().min(1).optional().default(1),\n\t};\n\n\t// Sort with allowed fields\n\tif (sortableFields?.length) {\n\t\tschemaShape.sort = z\n\t\t\t.record(z.enum([...sortableFields] as [string, ...string[]]), z.union([z.literal(1), z.literal(-1)]))\n\t\t\t.optional();\n\t}\n\n\t// Top-level select\n\tif (selectableFields?.length) {\n\t\tschemaShape.select = z\n\t\t\t.record(z.enum([...selectableFields] as [string, ...string[]]), z.union([z.literal(0), z.literal(1)]))\n\t\t\t.optional();\n\t}\n\n\t// Populate as array of objects with strict typing per path\n\tif (populateKeys.length) {\n\t\tconst populateSchemas: ZodTypeAny[] = [];\n\n\t\tfor (const path of populateKeys) {\n\t\t\tconst allowedSelects = populatableFields![path] as ReadonlyNonEmptyArray<string>;\n\n\t\t\tconst populateSchema = z.object({\n\t\t\t\tpath: z.literal(path as string),\n\t\t\t\tselect: z\n\t\t\t\t\t.record(z.enum([...allowedSelects] as [string, ...string[]]), z.union([z.literal(0), z.literal(1)]))\n\t\t\t\t\t.optional(),\n\t\t\t\tmatch: z.record(z.string(), z.any()).optional(),\n\t\t\t\toptions: z\n\t\t\t\t\t.object({\n\t\t\t\t\t\tsort: sortSchema.optional(),\n\t\t\t\t\t\tlimit: z.number().optional(),\n\t\t\t\t\t\tskip: z.number().optional(),\n\t\t\t\t\t})\n\t\t\t\t\t.optional(),\n\t\t\t});\n\n\t\t\tpopulateSchemas.push(populateSchema);\n\t\t}\n\n\t\tconst unionSchema =\n\t\t\tpopulateSchemas.length === 1\n\t\t\t\t? populateSchemas[0]\n\t\t\t\t: z.union(populateSchemas as [ZodTypeAny, ZodTypeAny, ...ZodTypeAny[]]);\n\n\t\tschemaShape.populate = z.array(unionSchema).optional();\n\t}\n\n\t// Search\n\tif (searchableFields?.length) {\n\t\tschemaShape.search = z\n\t\t\t.object({\n\t\t\t\tsearchKey: z.string(),\n\t\t\t\tsearchFields: z.array(z.enum([...searchableFields] as [string, ...string[]])).optional(),\n\t\t\t})\n\t\t\t.optional();\n\t}\n\n\t// Query\n\tif (queryableFields && Object.keys(queryableFields).length) {\n\t\tconst queryShape: Record<string, ZodTypeAny> = {};\n\t\tfor (const key in queryableFields) {\n\t\t\tqueryShape[key] = queryableFields[key].optional();\n\t\t}\n\t\tschemaShape.query = z.object(queryShape).optional();\n\t}\n\n\treturn z.object(schemaShape) as z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery, TSort>>;\n}\n\nconst TicketFilterSchema = createFilterSchema({\n\t// Top-level document fields that can be selected\n\tselectableFields: ['_id', 'ticketId', 'type', 'status', 'createdAt', 'updatedAt'] as const,\n\n\t// Each populate path has its own allowed select fields\n\tpopulatableFields: {\n\t\tchatroom: ['_id', 'lastMessage', 'participants', 'unreadCount'] as const,\n\t\tclient: ['_id', 'name', 'profilePhoto', 'type'] as const,\n\t\tadmin: ['_id', 'name', 'profilePhoto'] as const,\n\t\torder: ['_id', 'orderId', 'shop', 'total'] as const,\n\t} as const,\n\n\tsearchableFields: ['ticketId'] as const,\n\n\tsortableFields: ['createdAt', 'updatedAt', 'ticketId', 'status'] as const,\n\n\tqueryableFields: {\n\t\tstatus: z.enum(['OPEN', 'CLOSED', 'PENDING']),\n\t\ttype: z.enum(['ORDER', 'GENERAL', 'COMPLAINT']),\n\t\tclient: z.string(),\n\t\tclientType: z.enum(['USER', 'SHOP']),\n\t},\n});\n\ntype TicketFilterDTO = z.infer<typeof TicketFilterSchema>;\n\nconst ticket: TicketFilterDTO = {\n\tsort: { createdAt: -1, status: 1 }, // ✅ Only allowed fields with 1 or -1\n};\n\n/**\n * EXAMPLE USAGE\n *\n * const TicketFilterSchema = createFilterSchema({\n * selectableFields: [\n * '_id',\n * 'ticketId',\n * 'type',\n * 'status',\n * 'createdAt',\n * 'updatedAt',\n * ] as const,\n *\n * populatableFields: {\n * chatroom: ['_id', 'lastMessage', 'participants', 'unreadCount'] as const,\n * client: ['_id', 'name', 'profilePhoto', 'type'] as const,\n * admin: ['_id', 'name', 'profilePhoto'] as const,\n * order: ['_id', 'orderId', 'shop', 'total'] as const,\n * } as const,\n *\n * searchableFields: ['ticketId'] as const,\n *\n * sortableFields: ['createdAt', 'updatedAt', 'ticketId', 'status'] as const,\n *\n * queryableFields: {\n * status: z.enum(['OPEN', 'CLOSED', 'PENDING']),\n * type: z.enum(['ORDER', 'GENERAL', 'COMPLAINT']),\n * client: z.string(),\n * clientType: z.enum(['USER', 'SHOP']),\n * },\n * });\n *\n * type TicketFilterDTO = z.infer<typeof TicketFilterSchema>;\n *\n * // ✅ VALID EXAMPLES\n *\n * // Example 1: Sort by allowed fields\n * const filter1: TicketFilterDTO = {\n * sort: { createdAt: -1, status: 1 },\n * query: { status: 'OPEN' },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 2: Populate client with specific fields\n * const filter2: TicketFilterDTO = {\n * populate: [\n * { path: 'client', select: { name: 1, profilePhoto: 1 } }\n * ],\n * sort: { updatedAt: -1 },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 3: Populate multiple relations with sort\n * const filter3: TicketFilterDTO = {\n * populate: [\n * { path: 'client', select: { name: 1, profilePhoto: 1 } },\n * { path: 'order', select: { orderId: 1, total: 1 } },\n * { path: 'chatroom', select: { lastMessage: 1, unreadCount: 1 } },\n * ],\n * sort: { createdAt: -1 },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 4: Complex query with search and sort\n * const filter4: TicketFilterDTO = {\n * query: { status: 'OPEN', type: 'ORDER' },\n * search: { searchKey: 'TKT-123', searchFields: ['ticketId'] },\n * populate: [{ path: 'client', select: { name: 1 } }],\n * sort: { createdAt: -1, ticketId: 1 },\n * page: 1,\n * size: 20,\n * };\n *\n * // Example 5: Populate with match and options\n * const filter5: TicketFilterDTO = {\n * populate: [\n * {\n * path: 'order',\n * select: { orderId: 1, shop: 1 },\n * match: { status: 'DELIVERED' },\n * options: { sort: { createdAt: -1 }, limit: 10 },\n * }\n * ],\n * sort: { updatedAt: -1 },\n * page: 1,\n * size: 20,\n * };\n *\n * // ❌ INVALID EXAMPLES (TypeScript will reject)\n *\n * const invalid1: TicketFilterDTO = {\n * sort: { invalidField: 1 }, // ❌ not in sortableFields\n * };\n *\n * const invalid2: TicketFilterDTO = {\n * sort: { createdAt: 2 }, // ❌ must be 1 or -1\n * };\n *\n * const invalid3: TicketFilterDTO = {\n * populate: [\n * {\n * path: 'client',\n * select: { email: 1 }, // ❌ email not in client's populatableFields\n * }\n * ],\n * };\n *\n * const invalid4: TicketFilterDTO = {\n * query: { status: 'INVALID_STATUS' }, // ❌ not in enum\n * }\n */\n"]}
|
|
@@ -76,22 +76,34 @@ export declare class SchemaBuilder<T extends z.ZodRawShape> {
|
|
|
76
76
|
private baseSchema;
|
|
77
77
|
constructor(baseFields: T, includeTimestamps?: boolean, includeTracking?: boolean);
|
|
78
78
|
private createBaseSchema;
|
|
79
|
-
getBaseSchema(): z.ZodObject<T, "strict", z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<T>, any> extends infer T_1 ? { [k in keyof T_1]:
|
|
79
|
+
getBaseSchema(): z.ZodObject<T, "strict", z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<T>, any> extends infer T_1 ? { [k in keyof T_1]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<T>, any>[k]; } : never, z.baseObjectInputType<T> extends infer T_2 ? { [k_1 in keyof T_2]: z.baseObjectInputType<T>[k_1]; } : never>;
|
|
80
80
|
getEntitySchema(): z.ZodObject<z.objectUtil.extendShape<T, {
|
|
81
81
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
82
|
-
}> extends infer T_1 extends z.ZodRawShape ? { [k in keyof T_1]: z.ZodOptional<
|
|
82
|
+
}> extends infer T_1 extends z.ZodRawShape ? { [k in keyof T_1]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
83
83
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
84
|
-
}>
|
|
84
|
+
}>[k]>; } : never, "passthrough", z.ZodTypeAny, z.objectOutputType<z.objectUtil.extendShape<T, {
|
|
85
85
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
86
|
-
}> extends infer
|
|
86
|
+
}> extends infer T_2 extends z.ZodRawShape ? { [k in keyof T_2]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
87
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
88
|
+
}>[k]>; } : never, z.ZodTypeAny, "passthrough">, z.objectInputType<z.objectUtil.extendShape<T, {
|
|
89
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
90
|
+
}> extends infer T_3 extends z.ZodRawShape ? { [k in keyof T_3]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
91
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
92
|
+
}>[k]>; } : never, z.ZodTypeAny, "passthrough">>;
|
|
87
93
|
getIdSchema(): z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
88
94
|
getUpdateSchema(excludeFields?: ExcludeFromUpdate): z.ZodObject<z.objectUtil.extendShape<T, {
|
|
89
95
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
90
|
-
}> extends infer T_1 extends z.ZodRawShape ? { [k in keyof T_1]: z.ZodOptional<
|
|
96
|
+
}> extends infer T_1 extends z.ZodRawShape ? { [k in keyof T_1]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
97
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
98
|
+
}>[k]>; } : never, "passthrough", z.ZodTypeAny, z.objectOutputType<z.objectUtil.extendShape<T, {
|
|
99
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
100
|
+
}> extends infer T_2 extends z.ZodRawShape ? { [k in keyof T_2]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
101
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
102
|
+
}>[k]>; } : never, z.ZodTypeAny, "passthrough">, z.objectInputType<z.objectUtil.extendShape<T, {
|
|
91
103
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
92
|
-
}> extends infer
|
|
104
|
+
}> extends infer T_3 extends z.ZodRawShape ? { [k in keyof T_3]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
93
105
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
94
|
-
}>
|
|
106
|
+
}>[k]>; } : never, z.ZodTypeAny, "passthrough">> | z.ZodObject<{
|
|
95
107
|
[x: string]: z.ZodOptional<z.ZodTypeAny>;
|
|
96
108
|
}, "strict", z.ZodTypeAny, {
|
|
97
109
|
[x: string]: any;
|
|
@@ -114,22 +126,34 @@ export declare class SchemaBuilder<T extends z.ZodRawShape> {
|
|
|
114
126
|
[x: string]: any;
|
|
115
127
|
}>;
|
|
116
128
|
getAllSchemas(excludeFromUpdate?: ExcludeFromUpdate): {
|
|
117
|
-
BaseSchema: z.ZodObject<T, "strict", z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<T>, any> extends infer T_1 ? { [k in keyof T_1]:
|
|
129
|
+
BaseSchema: z.ZodObject<T, "strict", z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<T>, any> extends infer T_1 ? { [k in keyof T_1]: z.objectUtil.addQuestionMarks<z.baseObjectOutputType<T>, any>[k]; } : never, z.baseObjectInputType<T> extends infer T_2 ? { [k_1 in keyof T_2]: z.baseObjectInputType<T>[k_1]; } : never>;
|
|
118
130
|
EntitySchema: z.ZodObject<z.objectUtil.extendShape<T, {
|
|
119
131
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
120
|
-
}> extends infer T_3 extends z.ZodRawShape ? { [k_2 in keyof T_3]: z.ZodOptional<
|
|
132
|
+
}> extends infer T_3 extends z.ZodRawShape ? { [k_2 in keyof T_3]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
121
133
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
122
|
-
}>
|
|
134
|
+
}>[k_2]>; } : never, "passthrough", z.ZodTypeAny, z.objectOutputType<z.objectUtil.extendShape<T, {
|
|
123
135
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
124
|
-
}> extends infer
|
|
136
|
+
}> extends infer T_4 extends z.ZodRawShape ? { [k_2 in keyof T_4]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
137
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
138
|
+
}>[k_2]>; } : never, z.ZodTypeAny, "passthrough">, z.objectInputType<z.objectUtil.extendShape<T, {
|
|
139
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
140
|
+
}> extends infer T_5 extends z.ZodRawShape ? { [k_2 in keyof T_5]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
141
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
142
|
+
}>[k_2]>; } : never, z.ZodTypeAny, "passthrough">>;
|
|
125
143
|
IdSchema: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
126
144
|
UpdateSchema: z.ZodObject<z.objectUtil.extendShape<T, {
|
|
127
145
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
128
|
-
}> extends infer T_6 extends z.ZodRawShape ? { [k_2 in keyof T_6]: z.ZodOptional<
|
|
146
|
+
}> extends infer T_6 extends z.ZodRawShape ? { [k_2 in keyof T_6]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
147
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
148
|
+
}>[k_2]>; } : never, "passthrough", z.ZodTypeAny, z.objectOutputType<z.objectUtil.extendShape<T, {
|
|
149
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
150
|
+
}> extends infer T_7 extends z.ZodRawShape ? { [k_2 in keyof T_7]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
151
|
+
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
152
|
+
}>[k_2]>; } : never, z.ZodTypeAny, "passthrough">, z.objectInputType<z.objectUtil.extendShape<T, {
|
|
129
153
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
130
|
-
}> extends infer
|
|
154
|
+
}> extends infer T_8 extends z.ZodRawShape ? { [k_2 in keyof T_8]: z.ZodOptional<z.objectUtil.extendShape<T, {
|
|
131
155
|
_id: z.ZodUnion<[z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mongoose.Types.ObjectId, string>, z.ZodType<mongoose.Types.ObjectId, z.ZodTypeDef, mongoose.Types.ObjectId>]>;
|
|
132
|
-
}>
|
|
156
|
+
}>[k_2]>; } : never, z.ZodTypeAny, "passthrough">> | z.ZodObject<{
|
|
133
157
|
[x: string]: z.ZodOptional<z.ZodTypeAny>;
|
|
134
158
|
}, "strict", z.ZodTypeAny, {
|
|
135
159
|
[x: string]: any;
|