public async validate()

in src/modules/form-submissions/form-submissions.controller.ts [207:287]


  public async validate(
    @Headers(ACCESS_TOKEN_NAME) accessToken: string,
    @Param('formKey') formKey: string,
    @Body() body: FormSchemaDTO,
  ): Promise<{ data: FormSubmission['data'] }> {
    this._logging.logger.info(`Loading schema [${formKey}]`);
    const schema = await this._getSchema(accessToken, formKey);

    try {
      this._logging.logger.info(`Validating against schema [${formKey}]`);
      const data = await this._validation.validate(schema, body);
      this._logging.logger.info(`Submission data for schema [${formKey}] seems to be valid ...`, {
        responseCode: 200,
      });

      const fileComponents = findComponents(
        schema.components,
        (component) => !!(isFileComponent(component.type) && component.resourceValidation),
      );

      const { processInstanceId, data: bodyFormData } = body;

      if (fileComponents && processInstanceId) {
        const results = fileComponents?.map(async (component) => {
          const bodyData = (bodyFormData[component.key] as unknown as FileMetadata[])[0];
          return await this._dataFactoryService.validationCsvFile({
            processInstanceId,
            accessToken,
            entity: component.resourceValidation as string,
            body: {
              id: bodyData.id,
              checksum: bodyData.checksum,
            },
          });
        });

        await Promise.all(results);
      }

      // TODO: normalize response format
      return {
        data,
      };
    } catch (err) {
      if (err instanceof InvalidCsvFileError) {
        this._logging.logger.error(`csv file is invalid`, {
          responseCode: 422,
        });

        throw new HttpException(JSON.parse(err.message), 422);
      }

      if (err instanceof FormValidationError) {
        this._logging.logger.error(`Submission for schema [${formKey}] is invalid!`, {
          responseCode: 422,
        });
        throw new HttpException(
          {
            traceId: this._logging.traceId,
            code: VALIDATION_ERROR_CODE,
            details: {
              errors: err.details.map((item) => ({
                value: JSON.stringify(item.context.value),
                field: item.context.key,
                message: item.message,
              })),
            },
          },
          422,
        );
      }

      this._logging.logger.error(`Unknown error while validating schema [${formKey}]`, {
        responseCode: 500,
      });
      throw new InternalServerErrorException({
        traceId: this._logging.traceId,
        message: 'Unknown validation error', // TODO: i18n
      });
    }
  }