@lyxa.ai/types 1.1.52 → 1.1.53
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
CHANGED
package/package.json
CHANGED
|
@@ -18,11 +18,11 @@ export type PopulateObject<TPopulate extends ReadonlyNonEmptyArray<string>, TSel
|
|
|
18
18
|
};
|
|
19
19
|
populate?: PopulateObject<TPopulate, TSelect> | PopulateObject<TPopulate, TSelect>[];
|
|
20
20
|
};
|
|
21
|
-
export type FilterDTO<TSelect extends ReadonlyNonEmptyArray<string>, TPopulate extends ReadonlyNonEmptyArray<string>, TSearch extends ReadonlyNonEmptyArray<string>> = {
|
|
21
|
+
export type FilterDTO<TSelect extends ReadonlyNonEmptyArray<string>, TPopulate extends ReadonlyNonEmptyArray<string>, TSearch extends ReadonlyNonEmptyArray<string>, TQuery extends Record<string, any> = Record<string, any>> = {
|
|
22
22
|
size?: number;
|
|
23
23
|
page?: number;
|
|
24
24
|
sort?: Record<string, 1 | -1>;
|
|
25
|
-
query?:
|
|
25
|
+
query?: TQuery;
|
|
26
26
|
select?: Partial<Record<TSelect[number], 0 | 1>>;
|
|
27
27
|
populate?: PopulateObject<TPopulate, TSelect> | PopulateObject<TPopulate, TSelect>[];
|
|
28
28
|
search?: {
|
|
@@ -30,5 +30,5 @@ export type FilterDTO<TSelect extends ReadonlyNonEmptyArray<string>, TPopulate e
|
|
|
30
30
|
searchFields?: TSearch[number][];
|
|
31
31
|
};
|
|
32
32
|
};
|
|
33
|
-
export declare function createFilterSchema<TSelect extends ReadonlyNonEmptyArray<string>, TPopulate extends ReadonlyNonEmptyArray<string>, TSearch extends ReadonlyNonEmptyArray<string>>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch>): z.ZodType<FilterDTO<TSelect, TPopulate, TSearch>>;
|
|
33
|
+
export declare function createFilterSchema<TSelect extends ReadonlyNonEmptyArray<string>, TPopulate extends ReadonlyNonEmptyArray<string>, TSearch extends ReadonlyNonEmptyArray<string>, TQuery extends Record<string, any> = Record<string, any>>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch>): z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery>>;
|
|
34
34
|
export {};
|
|
@@ -28,7 +28,7 @@ function createFilterSchema(config) {
|
|
|
28
28
|
size: zod_1.z.number().min(1).max(maxSize).optional().default(defaultSize),
|
|
29
29
|
page: zod_1.z.number().min(1).optional().default(1),
|
|
30
30
|
sort: sortSchema.optional(),
|
|
31
|
-
query:
|
|
31
|
+
query: zod_1.z.any().optional(),
|
|
32
32
|
select: selectSchema.optional(),
|
|
33
33
|
populate: zod_1.z.union([populateSchema, zod_1.z.array(populateSchema)]).optional(),
|
|
34
34
|
search: searchSchema.optional(),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filter-schema.factory.js","sourceRoot":"/","sources":["utilities/validation/filter-schema.factory.ts"],"names":[],"mappings":";;AA4CA,
|
|
1
|
+
{"version":3,"file":"filter-schema.factory.js","sourceRoot":"/","sources":["utilities/validation/filter-schema.factory.ts"],"names":[],"mappings":";;AA4CA,gDA2CC;AAvFD,6BAAwB;AAExB,MAAM,UAAU,GAAG,OAAC,CAAC,MAAM,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,OAAC,CAAC,KAAK,CAAC,CAAC,OAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAChF,MAAM,gBAAgB,GAAG,OAAC,CAAC,MAAM,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,OAAC,CAAC,GAAG,EAAE,CAAC,CAAC;AAyCvD,SAAgB,kBAAkB,CAKhC,MAAuD;IACxD,MAAM,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,OAAO,GAAG,GAAG,EAAE,WAAW,GAAG,EAAE,EAAE,GAAG,MAAM,CAAC;IAE1G,MAAM,YAAY,GAAG,O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,OAAC,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,CAAC,QAAQ,EAAE;KAC1D,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG,OAAC,CAAC,MAAM,CAAC;QACvB,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC;QACpE,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;QAC7C,IAAI,EAAE,UAAU,CAAC,QAAQ,EAAE;QAC3B,KAAK,EAAE,OAAC,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE;QACzB,MAAM,EAAE,YAAY,CAAC,QAAQ,EAAE;QAC/B,QAAQ,EAAE,OAAC,CAAC,KAAK,CAAC,CAAC,cAAc,EAAE,OAAC,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE;QACvE,MAAM,EAAE,YAAY,CAAC,QAAQ,EAAE;KAC/B,CAAC,CAAC;IAEH,OAAO,MAAmE,CAAC;AAC5E,CAAC","sourcesContent":["import { z } from 'zod';\n\nconst sortSchema = z.record(z.string(), z.union([z.literal(1), z.literal(-1)]));\nconst MongoQuerySchema = z.record(z.string(), z.any());\n\ntype ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];\n\nexport interface FilterSchemaConfig<\n\tTSelect extends ReadonlyNonEmptyArray<string>,\n\tTPopulate extends ReadonlyNonEmptyArray<string>,\n\tTSearch extends ReadonlyNonEmptyArray<string>,\n> {\n\tselectableFields: TSelect;\n\tpopulatableFields: TPopulate;\n\tsearchableFields: TSearch;\n\tmaxSize?: number;\n\tdefaultSize?: number;\n}\n\nexport type PopulateObject<\n\tTPopulate extends ReadonlyNonEmptyArray<string>,\n\tTSelect extends ReadonlyNonEmptyArray<string>,\n> = {\n\tpath: TPopulate[number];\n\tselect?: Partial<Record<TSelect[number], 0 | 1>>;\n\tmatch?: Record<string, any>;\n\toptions?: { sort?: Record<string, 1 | -1>; limit?: number; skip?: number };\n\tpopulate?: PopulateObject<TPopulate, TSelect> | PopulateObject<TPopulate, TSelect>[];\n};\nexport type FilterDTO<\n\tTSelect extends ReadonlyNonEmptyArray<string>,\n\tTPopulate extends ReadonlyNonEmptyArray<string>,\n\tTSearch extends ReadonlyNonEmptyArray<string>,\n\tTQuery extends Record<string, any> = Record<string, any>,\n> = {\n\tsize?: number;\n\tpage?: number;\n\tsort?: Record<string, 1 | -1>;\n\tquery?: TQuery;\n\tselect?: Partial<Record<TSelect[number], 0 | 1>>;\n\tpopulate?: PopulateObject<TPopulate, TSelect> | PopulateObject<TPopulate, TSelect>[];\n\tsearch?: { searchKey: string; searchFields?: TSearch[number][] };\n};\n\nexport function createFilterSchema<\n\tTSelect extends ReadonlyNonEmptyArray<string>,\n\tTPopulate extends ReadonlyNonEmptyArray<string>,\n\tTSearch extends ReadonlyNonEmptyArray<string>,\n\tTQuery extends Record<string, any> = Record<string, any>,\n>(config: FilterSchemaConfig<TSelect, TPopulate, TSearch>) {\n\tconst { selectableFields, populatableFields, searchableFields, maxSize = 100, defaultSize = 10 } = config;\n\n\tconst selectSchema = z.record(z.enum(selectableFields), z.union([z.literal(0), z.literal(1)]));\n\n\tconst populateSchema: z.ZodType<PopulateObject<TPopulate, TSelect>> = z.lazy(\n\t\t() =>\n\t\t\tz.object({\n\t\t\t\tpath: z.enum(populatableFields),\n\t\t\t\tselect: z.record(z.enum(selectableFields), z.union([z.literal(0), z.literal(1)])).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\tpopulate: z.union([populateSchema, z.array(populateSchema)]).optional(),\n\t\t\t}) as z.ZodType<PopulateObject<TPopulate, TSelect>>\n\t);\n\n\tconst searchSchema = z.object({\n\t\tsearchKey: z.string(),\n\t\tsearchFields: z.array(z.enum(searchableFields)).optional(),\n\t});\n\n\tconst schema = z.object({\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\tquery: z.any().optional(),\n\t\tselect: selectSchema.optional(),\n\t\tpopulate: z.union([populateSchema, z.array(populateSchema)]).optional(),\n\t\tsearch: searchSchema.optional(),\n\t});\n\n\treturn schema as z.ZodType<FilterDTO<TSelect, TPopulate, TSearch, TQuery>>;\n}\n\n/**\n * @Example\n *\n * const UserFilterSchema = createFilterSchema({\n * \tselectableFields: ['_id', 'name', 'email', 'role', 'createdAt', 'updatedAt'] as const,\n * \tpopulatableFields: ['organization', 'permissions', 'addresses'] as const,\n * \tsearchableFields: ['name', 'email', 'role'] as const,\n * \t{ role?: string; active?: boolean }\n * });\n *\n * type UserFilterDTO = z.infer<typeof UserFilterSchema>;\n *\n * const correctFilter: UserFilterDTO = {\n * \tselect: { name: 1, email: 1 },\n * \tquery: { role: 'admin', active: true },\n * \tpopulate: [{ path: 'organization', select: { name: 1 } }],\n * \tsearch: { searchKey: 'john', searchFields: ['name', 'email'] },\n * \tpage: 1,\n * \tsize: 20,\n * };\n *\n * const wrongFilter: UserFilterDTO = {\n * \tselect: { name: 1, email: 1, password: 1 }, ❌ Password is not in selectableFields\n * \tquery: { role: 'admin', status: true }, ❌ Status is not in the query type\n * \tpopulate: [{ path: 'shops', select: { name: 1 } }], ❌ Shops is not in populatableFields\n * \tsearch: { searchKey: 'john', searchFields: ['name', 'email', 'price'] }, ❌ Price is not in searchableFields\n * \tpage: 1,\n * \tsize: 20,\n * };\n */"]}
|