Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity query validator #105

Merged
4 commits merged into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions api/test/users/custom-widgets/custom-widget-crud.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('Custom Widgets API', () => {
});

// TODO: Skipping this tests as filtering capabilities are not working, pending to fix the corresponding schema
it.skip('Should allow authenticated users to read their custom widgets using filters', async () => {
it('Should allow authenticated users to read their custom widgets using filters', async () => {
// Given
const customWidget1 = await entityMocks.createCustomWidget({
name: 'custom-widget1',
Expand All @@ -224,14 +224,17 @@ describe('Custom Widgets API', () => {
// When
const res = await testManager
.request()
.get(`/users/${testUser.id}/widgets?filter[name]=${customWidget1.name}`)
.get(
`/users/${testUser.id}/widgets?filter[name]=${customWidget1.name}&include[]=widget`,
)
.set('Authorization', `Bearer ${authToken}`);

// Then
expect(res.status).toBe(200);
const responseData = res.body.data;
expect(responseData).toHaveLength(1);
expect(responseData[0].name).toBe(customWidget1.name);
expect(responseData[0].widget.id).toBe(baseWidget.id);
});

it("Shouldn't allow authenticated users to read other user's custom widgets", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import { getAuthHeader } from "@/utils/auth-header";
import { selectedRowAtom } from "../../store";

import useColumns from "./columns";
import { SortQueryParam } from "@shared/schemas/query-param.schema";
import { CustomWidget } from "@shared/dto/widgets/custom-widget.entity";

const ROWS_PER_PAGE_OPTIONS = ["10", "25", "50", "100"];

Expand Down Expand Up @@ -73,7 +75,9 @@ const SavedVisualizationsTable: FC = () => {
"updatedAt",
],
sort: Object.keys(sorting).length
? sorting.map((sort) => `${sort.desc ? "" : "-"}${sort.id}`)
? (sorting.map(
(sort) => `${sort.desc ? "" : "-"}${sort.id}`,
) as SortQueryParam<CustomWidget>)
: ["-updatedAt"],
pageSize: pagination.size,
pageNumber: pagination.page,
Expand Down
4 changes: 2 additions & 2 deletions shared/contracts/sections.contract.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { JSONAPIError } from '@shared/dto/errors/json-api.error';
import { ApiPaginationResponse } from '@shared/dto/global/api-response.dto';
import { Section } from '@shared/dto/sections/section.entity';
import { generateEntityQuerySchema } from '@shared/schemas/query-param.schema';
import { initContract } from '@ts-rest/core';
import { FetchSpecificationSchema } from '@shared/schemas/query-param.schema';

const contract = initContract();
export const sectionContract = contract.router({
searchSections: {
method: 'GET',
path: '/sections',
query: FetchSpecificationSchema,
query: generateEntityQuerySchema(Section),
responses: {
200: contract.type<ApiPaginationResponse<Section>>(),
400: contract.type<JSONAPIError>(),
Expand Down
11 changes: 6 additions & 5 deletions shared/contracts/users.contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
CreateCustomWidgetSchema,
UpdateCustomWidgetSchema,
} from '@shared/schemas/widget.schemas';
import { FetchSpecificationSchema } from '@shared/schemas/query-param.schema';
import { generateEntityQuerySchema } from '@shared/schemas/query-param.schema';
import { User } from '@shared/dto/users/user.entity';

const contract = initContract();
export const usersContract = contract.router({
Expand All @@ -37,7 +38,7 @@ export const usersContract = contract.router({
400: contract.type<{ message: string }>(),
},
summary: 'Get all users',
query: FetchSpecificationSchema,
query: generateEntityQuerySchema(User),
},
findMe: {
method: 'GET',
Expand All @@ -46,7 +47,7 @@ export const usersContract = contract.router({
200: contract.type<ApiResponse<UserDto>>(),
401: contract.type<JSONAPIError>(),
},
query: FetchSpecificationSchema,
query: generateEntityQuerySchema(User),
},
updatePassword: {
method: 'PATCH',
Expand All @@ -70,7 +71,7 @@ export const usersContract = contract.router({
400: contract.type<JSONAPIError>(),
401: contract.type<JSONAPIError>(),
},
query: FetchSpecificationSchema,
query: generateEntityQuerySchema(User),
summary: 'Get a user by id',
},
updateUser: {
Expand Down Expand Up @@ -111,7 +112,7 @@ export const usersContract = contract.router({
method: 'GET',
path: '/users/:userId/widgets',
pathParams: z.object({ userId: z.string().uuid() }),
query: FetchSpecificationSchema,
query: generateEntityQuerySchema(CustomWidget),
responses: {
200: contract.type<ApiPaginationResponse<CustomWidget>>(),
400: contract.type<JSONAPIError>(),
Expand Down
135 changes: 125 additions & 10 deletions shared/schemas/query-param.schema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,127 @@
import { z } from 'zod';
import { getMetadataArgsStorage } from 'typeorm';

export const FetchSpecificationSchema = z.object({
pageSize: z.coerce.number().optional(),
pageNumber: z.coerce.number().optional(),
disablePagination: z.coerce.boolean().optional(),
fields: z.array(z.string()).optional(),
omitFields: z.array(z.string()).optional(),
include: z.array(z.string()).optional(),
sort: z.array(z.string()).optional(),
filter: z.record(z.unknown()).optional(),
});
const generateQuerySchema = <
FIELDS extends string,
INCLUDES extends string,
FILTERS extends string,
OMIT_FIELDS extends string,
SORT extends string,
>(config: {
fields?: readonly FIELDS[];
includes?: readonly INCLUDES[];
filter?: readonly FILTERS[];
omitFields?: readonly OMIT_FIELDS[];
sort?: readonly SORT[];
}) => {
const fields = z
.array(z.enum(config.fields as [FIELDS, ...FIELDS[]]))
.optional();
const filter = z
.record(
z.enum(config.filter as [FILTERS, ...FILTERS[]]),
z.union([
z.string().transform((value) => {
return value.split(',');
}),
z.array(z.string()),
]),
)
.optional();

const omitFields = z
.array(z.enum(config.omitFields as [OMIT_FIELDS, ...OMIT_FIELDS[]]))
.optional();

const include = z
.array(z.enum(config.includes as [INCLUDES, ...INCLUDES[]]))
.optional();

const sort = z.array(z.enum(config.sort as [SORT, ...SORT[]])).optional();

return z.object({
pageSize: z.coerce.number().optional(),
pageNumber: z.coerce.number().optional(),
disablePagination: z.coerce.boolean().optional(),
fields,
omitFields,
include,
sort,
filter,
});
};

type PropertyKeys<T> = {
// eslint-disable-next-line @typescript-eslint/ban-types
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T & string];

type ExtractProperties<T> = Pick<T, PropertyKeys<T>>;

type IsEntity<T> = unknown extends T // Exclude `any`
? never
: T extends Date // Exclude `Date`
? never
: T extends Array<infer U> // If `T` is an array, check the type of the elements
? IsEntity<U> extends never // If the elements are not entities, exclude the array
? never
: T // Include the array if the elements are valid entities
: T extends object // Check object types (non-arrays)
? T extends { constructor: Function }
? T // Include the object if it's a TypeORM entity
: never
: never;

// Utility type to extract only relationship keys (those referencing other entities that extend BaseWidget)
type RelationshipKeys<T> = {
[K in keyof T]: IsEntity<T[K]> extends never ? never : K;
}[keyof T & string];

// Extract only relationships from a class that extend BaseWidget
type ExtractRelationships<T> = Pick<T, RelationshipKeys<T>>;

export type FieldsQueryParam<T> = Array<keyof ExtractProperties<T>>;
export type IncludesQueryParam<T> = Array<keyof ExtractRelationships<T>>;
export type FilterQueryParam<T> = Array<keyof ExtractProperties<T>>;
export type OmitFieldsQueryParam<T> = Array<keyof ExtractProperties<T>>;
export type SortQueryParam<T> = Array<
keyof ExtractProperties<T> | `-${keyof ExtractProperties<T>}`
>;

export function generateEntityQuerySchema<T extends object>(
entityClass: {
new (): T;
},
config: {
fields?: FieldsQueryParam<T>;
includes?: IncludesQueryParam<T>;
filter?: FilterQueryParam<T>;
omitFields?: OmitFieldsQueryParam<T>;
sort?: SortQueryParam<T>;
} = {},
) {
const metadata = getMetadataArgsStorage();
const properties = metadata.columns
.filter((t) => t.target === entityClass)
.map((e) => e.propertyName) as Array<keyof ExtractProperties<T>>;

const mergedEntityConfig = {
fields: config.fields ?? properties,
includes:
config.includes ??
(metadata.relations
.filter((relation) => relation.target === entityClass)
.map((relation) => relation.propertyName) as Array<
keyof ExtractRelationships<T>
>),
filter: config.filter ?? properties,
omitFields: config.omitFields,
sort:
config.sort ??
([...properties, ...properties.map((p) => `-${p}`)] as Array<
keyof ExtractProperties<T> | `-${keyof ExtractProperties<T>}`
>),
} as const;

return generateQuerySchema(mergedEntityConfig);
}
Loading