fix: Validate select menu options (#7566)

* fix: validate select menu options

* chore: make requested changes

* refactor: make requested changes

* fix: tests
This commit is contained in:
Suneet Tipirneni
2022-03-24 15:57:53 -04:00
committed by GitHub
parent b520c3df3c
commit b1d63d919a
3 changed files with 86 additions and 3 deletions

View File

@@ -1,12 +1,15 @@
import type { APISelectMenuComponent } from 'discord-api-types/v10';
import type { APISelectMenuComponent, APISelectMenuOption } from 'discord-api-types/v10';
import {
customIdValidator,
disabledValidator,
minMaxValidator,
optionsLengthValidator,
optionValidator,
placeholderValidator,
validateRequiredSelectMenuParameters,
} from '../Assertions';
import { UnsafeSelectMenuBuilder } from './UnsafeSelectMenu';
import { UnsafeSelectMenuOptionBuilder } from './UnsafeSelectMenuOption';
/**
* Represents a validated select menu component
@@ -32,6 +35,32 @@ export class SelectMenuBuilder extends UnsafeSelectMenuBuilder {
return super.setDisabled(disabledValidator.parse(disabled));
}
public override addOptions(...options: (UnsafeSelectMenuOptionBuilder | APISelectMenuOption)[]) {
optionsLengthValidator.parse(this.options.length + options.length);
this.options.push(
...options.map((option) =>
option instanceof UnsafeSelectMenuOptionBuilder
? option
: new UnsafeSelectMenuOptionBuilder(optionValidator.parse(option) as APISelectMenuOption),
),
);
return this;
}
public override setOptions(...options: (UnsafeSelectMenuOptionBuilder | APISelectMenuOption)[]) {
optionsLengthValidator.parse(options.length);
this.options.splice(
0,
this.options.length,
...options.map((option) =>
option instanceof UnsafeSelectMenuOptionBuilder
? option
: new UnsafeSelectMenuOptionBuilder(optionValidator.parse(option) as APISelectMenuOption),
),
);
return this;
}
public override toJSON(): APISelectMenuComponent {
validateRequiredSelectMenuParameters(this.options, this.data.custom_id);
return super.toJSON();