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

Add validation for SafeApp['socialProfiles'][number]['platform'] #2094

Merged
merged 2 commits into from
Nov 6, 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
@@ -1,10 +1,11 @@
import { faker } from '@faker-js/faker';
import type { IBuilder } from '@/__tests__/builder';
import { Builder } from '@/__tests__/builder';
import { SafeAppSocialProfilePlatforms } from '@/domain/safe-apps/entities/schemas/safe-app.schema';
import type { SafeAppSocialProfile } from '@/domain/safe-apps/entities/safe-app-social-profile.entity';

export function safeAppSocialProfileBuilder(): IBuilder<SafeAppSocialProfile> {
return new Builder<SafeAppSocialProfile>()
.with('platform', faker.word.sample())
.with('platform', faker.helpers.objectValue(SafeAppSocialProfilePlatforms))
PooyaRaki marked this conversation as resolved.
Show resolved Hide resolved
.with('url', faker.internet.url({ appendSlash: false }));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export interface SafeAppSocialProfile {
platform: string;
import type { z } from 'zod';
import type {
SafeAppSocialProfilePlatforms,
SafeAppSocialProfileSchema,
} from '@/domain/safe-apps/entities/schemas/safe-app.schema';

export interface SafeAppSocialProfile
extends z.infer<typeof SafeAppSocialProfileSchema> {
platform: SafeAppSocialProfilePlatforms;
url: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { safeAppProviderBuilder } from '@/domain/safe-apps/entities/__tests__/sa
import { safeAppSocialProfileBuilder } from '@/domain/safe-apps/entities/__tests__/safe-app-social-profile.builder';
import { safeAppBuilder } from '@/domain/safe-apps/entities/__tests__/safe-app.builder';
import { SafeAppAccessControlPolicies } from '@/domain/safe-apps/entities/safe-app-access-control.entity';
import type { SafeAppSocialProfile } from '@/domain/safe-apps/entities/safe-app-social-profile.entity';
import { SafeAppSchema } from '@/domain/safe-apps/entities/schemas/safe-app.schema';
import { faker } from '@faker-js/faker';
import { ZodError } from 'zod';
Expand Down Expand Up @@ -64,14 +65,31 @@ describe('SafeAppSchema', () => {
);
});

it('should validate nested socialProfile and provider urls', () => {
it('should throw if a socialProfile has an invalid url', () => {
const safeApp = safeAppBuilder()
.with('socialProfiles', [
safeAppSocialProfileBuilder().build(),
safeAppSocialProfileBuilder()
.with('url', faker.string.alphanumeric())
.build(),
])
.build();

const result = SafeAppSchema.safeParse(safeApp);

expect(!result.success && result.error).toStrictEqual(
new ZodError([
{
validation: 'url',
code: 'invalid_string',
message: 'Invalid url',
path: ['socialProfiles', 0, 'url'],
},
]),
);
});

it('should throw if a profile has an invalid url', () => {
const safeApp = safeAppBuilder()
.with(
'provider',
safeAppProviderBuilder()
Expand All @@ -84,12 +102,6 @@ describe('SafeAppSchema', () => {

expect(!result.success && result.error).toStrictEqual(
new ZodError([
{
validation: 'url',
code: 'invalid_string',
message: 'Invalid url',
path: ['socialProfiles', 1, 'url'],
},
{
validation: 'url',
code: 'invalid_string',
Expand All @@ -100,6 +112,22 @@ describe('SafeAppSchema', () => {
);
});

it('should fallback to UNKNOWN nested socialProfile', () => {
const safeApp = safeAppBuilder()
.with('socialProfiles', [
safeAppSocialProfileBuilder()
.with('platform', 'invalid' as SafeAppSocialProfile['platform'])
PooyaRaki marked this conversation as resolved.
Show resolved Hide resolved
.build(),
])
.build();

const result = SafeAppSchema.safeParse(safeApp);
PooyaRaki marked this conversation as resolved.
Show resolved Hide resolved

expect(result.success && result.data.socialProfiles[0].platform).toBe(
'UNKNOWN',
);
});

it('should validate accessControl field', () => {
const safeApp = safeAppBuilder()
.with(
Expand Down
11 changes: 10 additions & 1 deletion src/domain/safe-apps/entities/schemas/safe-app.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ export const SafeAppAccessControlSchema = z.discriminatedUnion('type', [
}),
]);

export enum SafeAppSocialProfilePlatforms {
Discord = 'DISCORD',
GitHub = 'GITHUB',
Twitter = 'TWITTER',
Unknown = 'UNKNOWN',
}

export const SafeAppSocialProfileSchema = z.object({
platform: z.string(),
platform: z
.nativeEnum(SafeAppSocialProfilePlatforms)
.catch(SafeAppSocialProfilePlatforms.Unknown),
url: z.string().url(),
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';
import { SafeAppSocialProfile as DomainSafeAppSocialProfile } from '@/domain/safe-apps/entities/safe-app-social-profile.entity';
import { SafeAppSocialProfilePlatforms } from '@/domain/safe-apps/entities/schemas/safe-app.schema';

export class SafeAppSocialProfile implements DomainSafeAppSocialProfile {
@ApiProperty()
platform!: string;
@ApiProperty({ enum: SafeAppSocialProfilePlatforms })
platform!: SafeAppSocialProfilePlatforms;
@ApiProperty()
url!: string;
}