Skip to content

Commit ecc1531

Browse files
committed
Lenght checks for Template's Header and Body texts
1 parent d41db98 commit ecc1531

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

types/template.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class HeaderComponent {
193193
*/
194194
constructor(...parameters) {
195195
this.type = "header";
196-
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e));
196+
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e, "header"));
197197
}
198198
}
199199

@@ -211,7 +211,7 @@ class BodyComponent {
211211
*/
212212
constructor(...parameters) {
213213
this.type = "body";
214-
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e));
214+
if (parameters) this.parameters = parameters.map(e => e instanceof Parameter ? e : new Parameter(e, "body"));
215215
}
216216
}
217217

@@ -233,14 +233,22 @@ class Parameter {
233233
* For Document parameter, only PDF documents are supported for document-based message templates.
234234
*
235235
* @param {(Text|Currency|DateTime|Image|Document|Video)} parameter The parameter to be used in the template
236+
* @param {String} [whoami] The parent component, used to check if a Text object is too long. Can be either 'header' or 'body'
236237
* @throws {Error} If parameter is not provided
238+
* @throws {Error} If parameter is a Text and the parent component (whoami) is "header" and the text over 60 characters
239+
* @throws {Error} If parameter is a Text and the parent component (whoami) is "body" and the text over 1024 characters
237240
*/
238-
constructor(parameter) {
239-
if (!parameter) throw new Error("Parameter must have a parameter");
241+
constructor(parameter, whoami) {
242+
if (!parameter) throw new Error("Parameter object must have a parameter parameter");
240243
this.type = parameter._;
241244
delete parameter._;
245+
242246
// Text type can go to hell
243-
if (this.type === "text") this.text = parameter.body; else this[this.type] = parameter;
247+
if (this.type === "text") {
248+
if (whoami === "header" && object.body > 60) throw new Error("Header text must be 60 characters or less");
249+
if (whoami === "body" && object.body > 1024) throw new Error("Body text must be 1024 characters or less");
250+
this[this.type] = object.body;
251+
} else this[this.type] = object;
244252
}
245253
}
246254

0 commit comments

Comments
 (0)