Skip to content

Commit c6c28c9

Browse files
committed
refactor(song): update song entity and DTO to enforce maximum length constraints
- Introduced maximum length constraints for title, originalAuthor, and description fields in the Song entity using UPLOAD_CONSTANTS. - Simplified the songPreviewFromSongDocumentWithUser function to default the description to an empty string if not provided. - Updated the SongPreview DTO to reflect the new maximum length validations for title, originalAuthor, and description fields.
1 parent 53a7f93 commit c6c28c9

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

apps/backend/src/song/song.util.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,14 @@ export function uploadSongDtoFromSongDocument(song: SongEntity): UploadSongDto {
106106
export function songPreviewFromSongDocumentWithUser(
107107
song: SongPreviewSource,
108108
): SongPreviewDto {
109-
const description =
110-
song.description?.trim() && song.description.trim().length > 0
111-
? song.description
112-
: ' ';
113-
114109
return {
115110
publicId: song.publicId,
116111
uploader: {
117112
username: song.uploader.username,
118113
profileImage: song.uploader.profileImage,
119114
},
120115
title: song.title,
121-
description,
116+
description: song.description ?? '',
122117
originalAuthor: song.originalAuthor ?? '',
123118
duration: song.stats.duration,
124119
noteCount: song.stats.noteCount,

packages/database/src/song/song.entity.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
22
import { Document, Types } from 'mongoose';
33

4+
import { UPLOAD_CONSTANTS } from '@nbw/config';
45
import type {
56
CategoryType,
67
LicenseType,
@@ -73,13 +74,25 @@ export class Song {
7374
@Prop({ type: Boolean, required: true, default: true })
7475
allowDownload: boolean;
7576

76-
@Prop({ type: String, required: true })
77+
@Prop({
78+
type: String,
79+
required: true,
80+
maxlength: UPLOAD_CONSTANTS.title.maxLength,
81+
})
7782
title: string;
7883

79-
@Prop({ type: String, required: false })
84+
@Prop({
85+
type: String,
86+
required: false,
87+
maxlength: UPLOAD_CONSTANTS.originalAuthor.maxLength,
88+
})
8089
originalAuthor: string;
8190

82-
@Prop({ type: String, required: false })
91+
@Prop({
92+
type: String,
93+
required: false,
94+
maxlength: UPLOAD_CONSTANTS.description.maxLength,
95+
})
8396
description: string;
8497

8598
// SONG FILE ATTRIBUTES (Populated from NBS file - immutable)

packages/validation/src/song/SongPreview.dto.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { z } from 'zod';
22

3+
import { UPLOAD_CONSTANTS } from '@nbw/config';
4+
35
import type { VisibilityType } from './uploadMeta.js';
46

57
const songPreviewUploaderSchema = z.object({
@@ -10,9 +12,9 @@ const songPreviewUploaderSchema = z.object({
1012
export const songPreviewDtoSchema = z.object({
1113
publicId: z.string().min(1),
1214
uploader: songPreviewUploaderSchema,
13-
title: z.string().min(1).max(128),
14-
description: z.string().min(1),
15-
originalAuthor: z.string().min(1).max(64),
15+
title: z.string().min(1).max(UPLOAD_CONSTANTS.title.maxLength),
16+
description: z.string().max(UPLOAD_CONSTANTS.description.maxLength),
17+
originalAuthor: z.string().max(UPLOAD_CONSTANTS.originalAuthor.maxLength),
1618
duration: z.number().min(0),
1719
noteCount: z.number().int().min(0),
1820
thumbnailUrl: z.url(),

0 commit comments

Comments
 (0)