Skip to content

Commit

Permalink
feat: add uuid dependency and setup validation
Browse files Browse the repository at this point in the history
  • Loading branch information
typeWolffo committed Jul 9, 2024
1 parent 69c28c6 commit d763c93
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
4 changes: 3 additions & 1 deletion examples/common_nestjs_remix/apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"passport-local": "^1.0.0",
"postgres": "^3.4.4",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"uuid": "^10.0.0"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand All @@ -61,6 +62,7 @@
"@types/passport-jwt": "^4.0.1",
"@types/passport-local": "^1.0.38",
"@types/supertest": "^6.0.0",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.42.0",
Expand Down
13 changes: 9 additions & 4 deletions examples/common_nestjs_remix/apps/api/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TSchema, Type } from "@sinclair/typebox";
import { TSchema, Type, FormatRegistry } from "@sinclair/typebox";
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
import * as schema from "src/storage/schema";
import { validate as uuidValidate } from "uuid";

export type DatabasePg = PostgresJsDatabase<typeof schema>;

Expand All @@ -15,17 +16,21 @@ export class BaseResponse<T> {
export const UUIDSchema = Type.String({ format: "uuid" });

export function baseResponse(data: TSchema) {
if (data.$type === "array") {
if (data.type === "array") {
console.log(data);
return Type.Object({
data: Type.Array(data.items),
});
}

return Type.Object({
data: data,
data,
});
}

export function nullResponse() {
return Type.Null();
}

export function setupValidation() {
FormatRegistry.Set("uuid", (value) => uuidValidate(value));
}
3 changes: 3 additions & 0 deletions examples/common_nestjs_remix/apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import { patchNestJsSwagger, applyFormats } from "nestjs-typebox";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { exportSchemaToFile } from "./utils/save-swagger-to-file";
import * as cookieParser from "cookie-parser";
import { setupValidation } from "./common";

patchNestJsSwagger();
applyFormats();

async function bootstrap() {
const app = await NestFactory.create(AppModule);

setupValidation();

app.use(cookieParser());

const config = new DocumentBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ export class UsersController {
response: nullResponse(),
request: [{ type: "param", name: "id", schema: UUIDSchema }],
})
async deleteUser(@Param("id") id: string): Promise<null> {
async deleteUser(
@Param("id") id: string,
@CurrentUser() currentUser: { userId: string },
): Promise<null> {
if (currentUser.userId !== id) {
throw new ForbiddenException("You can only delete your own account");
}

await this.usersService.deleteUser(id);

return null;
Expand Down
15 changes: 15 additions & 0 deletions examples/common_nestjs_remix/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d763c93

Please sign in to comment.