in src/modules/form-submissions/form-submissions.controller.ts [415:488]
public async validateField(
@Headers(ACCESS_TOKEN_NAME) accessToken: string,
@Param('formKey') formKey: string,
@Param('fieldKey') fieldKey: string,
@Body() body: FormFieldValidationDTO,
): Promise<{ isValid: boolean; message?: string }> {
this._logging.logger.info(`Loading schema [${formKey}]`);
const schema = await this._getSchema(accessToken, formKey);
try {
const result = this._validation.validateFileMeta(schema, fieldKey, body);
if (result) {
this._logging.logger.info(`File meta validation (schema [${formKey}])`, {
responseCode: 200,
});
return {
isValid: true,
};
} else {
this._logging.logger.error(`Unknown error while validating schema [${formKey}]`, {
responseCode: 500,
});
throw new InternalServerErrorException({
traceId: this._logging.traceId,
message: 'Unknown error while validating the form', // TODO: i18n
});
}
} catch (err) {
if (err instanceof FormFieldNotFoundError) {
this._logging.logger.error(`Field [${fieldKey}] is not found in the schema [${formKey}]`, {
responseCode: 404,
});
throw new NotFoundException({
traceId: this._logging.traceId,
message: err.message,
});
}
if (
err instanceof UnsupportedFileTypeError ||
err instanceof UnsupportedSizeDefinition ||
err instanceof FileMaxSizeError ||
err instanceof MissingFormComponentError
) {
this._logging.logger.error(`Field [${fieldKey}] submission data is not valid for the schema [${formKey}]`, {
responseCode: 422,
});
throw new HttpException(
{
traceId: this._logging.traceId,
code: VALIDATION_ERROR_CODE,
details: {
errors: [
{
field: fieldKey,
message: err.message,
},
],
},
},
422,
);
}
this._logging.logger.error(
`Unknown error while validating the field [${fieldKey}] submission data against the schema [${formKey}]`,
{
responseCode: 500,
},
);
throw new InternalServerErrorException({
traceId: this._logging.traceId,
message: 'Unknown error while validating the form', // TODO: i18n
});
}
}