Skip to content

Throw if template is invalid because of missing data #171

@romain-sen

Description

@romain-sen

Hello everyone, I am new to postmark so maybe there is something I didn't understand about template validation.

I use the NodeJS SDK of postmark and this is what I would like to achieve :

async validateTemplate(templateId: number, templateModel: any) { // any for the example
  try {
    const template = await this.postmarkClient.getTemplate(templateId);
    const result = await this.postmarkClient.validateTemplate({
      TemplateType: template.TemplateType,
      HtmlBody: template.HtmlBody || "",
      TextBody: template.TextBody || "",
      Subject: template.Subject,
      TestRenderModel: templateModel,
    });
    if (!result.AllContentIsValid) {
      this.logger.error(...);
      return false;
    }
    return true;
  } catch (err) {
    this.logger.error(...);
    return false;
  }
}

The idea is that, even if I put some validation on the data I want to inject in the template, if the template is changed on Postmark web interface, I want my code to break and throw/log something. But most importantly, I don't want to send an invalid email with missing data.

However, when I use some invalid data, no error is trigger. AllContentIsValid is true and the email is sent with the template incomplete because my variables are missing.

const invalidTemplateModel = {
   invalid: "invalid",
   // missing fields
};

Is there a way to not send email and throw instead if at least one variable is missing in the TemplateModel ?

My workaround would be to use the result of const template = await this.postmarkClient.getTemplate(templateId), and then with regex find all the {{variable}} to check I have them all defined. But I would be surprised if there is no better way to do it with the SDK.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions