Skip to content

Commit 49ef3a8

Browse files
authored
fix(EmbedBuilder): allow empty name and value on fields (#10747)
1 parent bffe8a4 commit 49ef3a8

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

packages/builders/__tests__/messages/embed.test.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,16 @@ describe('Embed', () => {
324324
test('GIVEN an embed using Embed#addFields THEN returns valid toJSON data', () => {
325325
const embed = new EmbedBuilder();
326326
embed.addFields({ name: 'foo', value: 'bar' });
327-
embed.addFields([{ name: 'foo', value: 'bar' }]);
327+
embed.addFields([
328+
{ name: 'foo', value: 'bar' },
329+
{ name: '', value: '' },
330+
]);
328331

329332
expect(embed.toJSON()).toStrictEqual({
330333
fields: [
331334
{ name: 'foo', value: 'bar' },
332335
{ name: 'foo', value: 'bar' },
336+
{ name: '', value: '' },
333337
],
334338
});
335339
});
@@ -381,38 +385,24 @@ describe('Embed', () => {
381385
expect(() => embed.setFields(Array.from({ length: 26 }, () => ({ name: 'foo', value: 'bar' })))).toThrowError();
382386
});
383387

384-
describe('GIVEN invalid field amount THEN throws error', () => {
385-
test('1', () => {
386-
const embed = new EmbedBuilder();
387-
388-
expect(() =>
389-
embed.addFields(...Array.from({ length: 26 }, () => ({ name: 'foo', value: 'bar' }))),
390-
).toThrowError();
391-
});
392-
});
393-
394-
describe('GIVEN invalid field name THEN throws error', () => {
395-
test('2', () => {
396-
const embed = new EmbedBuilder();
388+
test('GIVEN invalid field amount THEN throws error', () => {
389+
const embed = new EmbedBuilder();
397390

398-
expect(() => embed.addFields({ name: '', value: 'bar' })).toThrowError();
399-
});
391+
expect(() =>
392+
embed.addFields(...Array.from({ length: 26 }, () => ({ name: 'foo', value: 'bar' }))),
393+
).toThrowError();
400394
});
401395

402-
describe('GIVEN invalid field name length THEN throws error', () => {
403-
test('3', () => {
404-
const embed = new EmbedBuilder();
396+
test('GIVEN invalid field name length THEN throws error', () => {
397+
const embed = new EmbedBuilder();
405398

406-
expect(() => embed.addFields({ name: 'a'.repeat(257), value: 'bar' })).toThrowError();
407-
});
399+
expect(() => embed.addFields({ name: 'a'.repeat(257), value: 'bar' })).toThrowError();
408400
});
409401

410-
describe('GIVEN invalid field value length THEN throws error', () => {
411-
test('4', () => {
412-
const embed = new EmbedBuilder();
402+
test('GIVEN invalid field value length THEN throws error', () => {
403+
const embed = new EmbedBuilder();
413404

414-
expect(() => embed.addFields({ name: '', value: 'a'.repeat(1_025) })).toThrowError();
415-
});
405+
expect(() => embed.addFields({ name: '', value: 'a'.repeat(1_025) })).toThrowError();
416406
});
417407
});
418408
});

packages/builders/src/messages/embed/Assertions.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,9 @@ import { s } from '@sapphire/shapeshift';
22
import type { APIEmbedField } from 'discord-api-types/v10';
33
import { isValidationEnabled } from '../../util/validation.js';
44

5-
export const fieldNamePredicate = s
6-
.string()
7-
.lengthGreaterThanOrEqual(1)
8-
.lengthLessThanOrEqual(256)
9-
.setValidationEnabled(isValidationEnabled);
5+
export const fieldNamePredicate = s.string().lengthLessThanOrEqual(256).setValidationEnabled(isValidationEnabled);
106

11-
export const fieldValuePredicate = s
12-
.string()
13-
.lengthGreaterThanOrEqual(1)
14-
.lengthLessThanOrEqual(1_024)
15-
.setValidationEnabled(isValidationEnabled);
7+
export const fieldValuePredicate = s.string().lengthLessThanOrEqual(1_024).setValidationEnabled(isValidationEnabled);
168

179
export const fieldInlinePredicate = s.boolean().optional();
1810

@@ -32,7 +24,10 @@ export function validateFieldLength(amountAdding: number, fields?: APIEmbedField
3224
fieldLengthPredicate.parse((fields?.length ?? 0) + amountAdding);
3325
}
3426

35-
export const authorNamePredicate = fieldNamePredicate.nullable().setValidationEnabled(isValidationEnabled);
27+
export const authorNamePredicate = fieldNamePredicate
28+
.lengthGreaterThanOrEqual(1)
29+
.nullable()
30+
.setValidationEnabled(isValidationEnabled);
3631

3732
export const imageURLPredicate = s
3833
.string()
@@ -96,4 +91,7 @@ export const embedFooterPredicate = s
9691

9792
export const timestampPredicate = s.union([s.number(), s.date()]).nullable().setValidationEnabled(isValidationEnabled);
9893

99-
export const titlePredicate = fieldNamePredicate.nullable().setValidationEnabled(isValidationEnabled);
94+
export const titlePredicate = fieldNamePredicate
95+
.lengthGreaterThanOrEqual(1)
96+
.nullable()
97+
.setValidationEnabled(isValidationEnabled);

0 commit comments

Comments
 (0)