Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Fix component validation
Browse files Browse the repository at this point in the history
  • Loading branch information
wpf500 committed Jan 25, 2024
1 parent 5dabc99 commit 5bcdba8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/api/dto/CalloutDto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CalloutFormSchema, ItemStatus } from "@beabee/beabee-common";
import { ItemStatus } from "@beabee/beabee-common";
import { Type } from "class-transformer";
import {
Equals,
Expand All @@ -8,7 +8,6 @@ import {
IsEnum,
IsIn,
IsNumber,
IsObject,
IsOptional,
IsString,
Max,
Expand All @@ -17,6 +16,7 @@ import {
} from "class-validator";

import { GetExportQuery, GetPaginatedQuery } from "@api/dto/BaseDto";
import { CalloutFormDto } from "@api/dto/CalloutFormDto";
import { LinkDto } from "@api/dto/LinkDto";
import IsSlug from "@api/validators/IsSlug";
import IsUrl from "@api/validators/IsUrl";
Expand All @@ -28,7 +28,6 @@ import { CalloutMapSchema, CalloutResponseViewSchema } from "@models/Callout";
import { CalloutAccess } from "@enums/callout-access";

import { CalloutData } from "@type/callout-data";
import { CalloutFormDto } from "./CalloutFormDto";

export enum GetCalloutWith {
Form = "form",
Expand Down Expand Up @@ -189,6 +188,7 @@ export class CreateCalloutDto extends BaseCalloutDto {
thanksText!: string;

@ValidateNested()
@Type(() => CalloutFormDto)
formSchema!: CalloutFormDto;
}

Expand Down Expand Up @@ -218,5 +218,6 @@ export class GetCalloutDto extends BaseCalloutDto {

@IsOptional()
@ValidateNested()
@Type(() => CalloutFormDto)
formSchema?: CalloutFormDto;
}
41 changes: 36 additions & 5 deletions src/api/dto/CalloutFormDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
IsIn,
IsOptional,
IsString,
ValidateNested
ValidateBy,
ValidateNested,
ValidationOptions,
buildMessage,
validate
} from "class-validator";

const inputTypes = [
Expand All @@ -37,7 +41,6 @@ abstract class BaseCalloutComponentDto implements BaseCalloutComponentSchema {
abstract type: string;
abstract input?: boolean;

// TODO
[key: string]: unknown;

@IsString()
Expand Down Expand Up @@ -143,10 +146,38 @@ function ComponentType() {
name: type
}))
]
}
},
keepDiscriminatorProperty: true
});
}

// This is hack to disable whitelist validation for components because
// the schema for a component has loads of properties and it would take
// a long time to list them all.
// TODO: validate properly!
function IsComponent(validationOptions?: ValidationOptions) {
return ValidateBy(
{
name: "isComponent",
validator: {
async validate(value: unknown) {
if (typeof value !== "object" || value === null) return false;
const error = await validate(value, {
whitelist: false,
forbidUnknownValues: true
});
return error.length === 0;
},
defaultMessage: buildMessage(
(eachPrefix) => eachPrefix + "$property must be a valid component",
validationOptions
)
}
},
validationOptions
);
}

type CalloutComponentDto =
| NestableCalloutComponentDto
| InputCalloutComponentDto
Expand All @@ -163,7 +194,7 @@ class NestableCalloutComponentDto
@Equals(false)
input!: false;

@ValidateNested({ each: true })
@IsComponent({ each: true })
@ComponentType()
components!: CalloutComponentDto[];
}
Expand All @@ -189,7 +220,7 @@ class CalloutSlideDto implements CalloutSlideSchema {
@IsString()
title!: string;

@ValidateNested({ each: true })
@IsComponent({ each: true })
@ComponentType()
components!: CalloutComponentDto[];

Expand Down
2 changes: 1 addition & 1 deletion src/api/interceptors/ValidateResponseInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class ValidateResponseInterceptor implements InterceptorInterface {
whitelist: true,
forbidUnknownValues: true,
forbidNonWhitelisted: true,
stopAtFirstError: true
validationError: { target: false }
});
if (errors.length > 0) {
log.error("Validation failed on response", { errors });
Expand Down

0 comments on commit 5bcdba8

Please sign in to comment.