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

Don't coerce numerical strings of CampaignActivities #1658

Merged
merged 1 commit into from
Jun 17, 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export function campaignActivityBuilder(): IBuilder<CampaignActivity> {
.with('holder', getAddress(faker.finance.ethereumAddress()))
.with('startDate', faker.date.recent())
.with('endDate', faker.date.future())
.with('boost', faker.number.float())
.with('totalPoints', faker.number.float())
.with('totalBoostedPoints', faker.number.float());
.with('boost', faker.string.numeric())
.with('totalPoints', faker.string.numeric())
.with('totalBoostedPoints', faker.string.numeric());
}

export function toJson(campaignActivity: CampaignActivity): unknown {
Expand Down
7 changes: 4 additions & 3 deletions src/domain/community/entities/campaign-activity.entity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { buildPageSchema } from '@/domain/entities/schemas/page.schema.factory';
import { AddressSchema } from '@/validation/entities/schemas/address.schema';
import { NumericStringSchema } from '@/validation/entities/schemas/numeric-string.schema';
import { z } from 'zod';

export const CampaignActivitySchema = z.object({
startDate: z.coerce.date(),
endDate: z.coerce.date(),
holder: AddressSchema,
boost: z.coerce.number(),
totalPoints: z.coerce.number(),
totalBoostedPoints: z.coerce.number(),
boost: NumericStringSchema,
totalPoints: NumericStringSchema,
totalBoostedPoints: NumericStringSchema,
});

export const CampaignActivityPageSchema = buildPageSchema(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('CampaignActivitySchema', () => {
'totalBoostedPoints' as const,
])(`should validate a decimal %s`, (field) => {
const campaignActivity = campaignActivityBuilder()
.with(field, faker.number.float())
.with(field, faker.number.int().toString())
.build();

const result = CampaignActivitySchema.safeParse(campaignActivity);
Expand All @@ -58,7 +58,7 @@ describe('CampaignActivitySchema', () => {
'totalBoostedPoints' as const,
])(`should validate a float %s`, (field) => {
const campaignActivity = campaignActivityBuilder()
.with(field, faker.number.float())
.with(field, faker.number.float().toString())
.build();

const result = CampaignActivitySchema.safeParse(campaignActivity);
Expand Down Expand Up @@ -92,24 +92,24 @@ describe('CampaignActivitySchema', () => {
},
{
code: 'invalid_type',
expected: 'number',
received: 'nan',
expected: 'string',
received: 'undefined',
path: ['boost'],
message: 'Expected number, received nan',
message: 'Required',
},
{
code: 'invalid_type',
expected: 'number',
received: 'nan',
expected: 'string',
received: 'undefined',
path: ['totalPoints'],
message: 'Expected number, received nan',
message: 'Required',
},
{
code: 'invalid_type',
expected: 'number',
received: 'nan',
expected: 'string',
received: 'undefined',
path: ['totalBoostedPoints'],
message: 'Expected number, received nan',
message: 'Required',
},
]),
);
Expand Down
6 changes: 3 additions & 3 deletions src/routes/community/entities/campaign-activity.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export class CampaignActivity implements DomainCampaignActivity {
@ApiProperty({ type: String })
endDate!: Date;
@ApiProperty()
boost!: number;
boost!: string;
@ApiProperty()
totalPoints!: number;
totalPoints!: string;
@ApiProperty()
totalBoostedPoints!: number;
totalBoostedPoints!: string;
}