fix: only push error when option is required

This commit is contained in:
MARCROCK22 2024-09-12 00:55:44 +00:00
parent 041db86ac2
commit d8bf40b60c

View File

@ -644,13 +644,14 @@ export class HandleCommand {
if (channel) { if (channel) {
if ('channel_types' in i) { if ('channel_types' in i) {
if (!(i as SeyfertChannelOption).channel_types!.includes(channel.type)) { if (!(i as SeyfertChannelOption).channel_types!.includes(channel.type)) {
errors.push({ if (i.required)
name: i.name, errors.push({
error: `The entered channel type is not one of ${(i as SeyfertChannelOption) name: i.name,
.channel_types!.map(t => ChannelType[t]) error: `The entered channel type is not one of ${(i as SeyfertChannelOption)
.join(', ')}`, .channel_types!.map(t => ChannelType[t])
fullError: ['CHANNEL_TYPES', (i as SeyfertChannelOption).channel_types!], .join(', ')}`,
}); fullError: ['CHANNEL_TYPES', (i as SeyfertChannelOption).channel_types!],
});
break; break;
} }
} }
@ -730,22 +731,24 @@ export class HandleCommand {
if (option.min_length) { if (option.min_length) {
if (value.length < option.min_length) { if (value.length < option.min_length) {
value = undefined; value = undefined;
errors.push({ if (i.required)
name: i.name, errors.push({
error: `The entered string has less than ${option.min_length} characters. The minimum required is ${option.min_length} characters.`, name: i.name,
fullError: ['STRING_MIN_LENGTH', option.min_length], error: `The entered string has less than ${option.min_length} characters. The minimum required is ${option.min_length} characters.`,
}); fullError: ['STRING_MIN_LENGTH', option.min_length],
});
break; break;
} }
} }
if (option.max_length) { if (option.max_length) {
if (value.length > option.max_length) { if (value.length > option.max_length) {
value = undefined; value = undefined;
errors.push({ if (i.required)
name: i.name, errors.push({
error: `The entered string has more than ${option.max_length} characters. The maximum required is ${option.max_length} characters.`, name: i.name,
fullError: ['STRING_MAX_LENGTH', option.max_length], error: `The entered string has more than ${option.max_length} characters. The maximum required is ${option.max_length} characters.`,
}); fullError: ['STRING_MAX_LENGTH', option.max_length],
});
break; break;
} }
} }
@ -753,13 +756,14 @@ export class HandleCommand {
const choice = option.choices.find(x => x.name === value); const choice = option.choices.find(x => x.name === value);
if (!choice) { if (!choice) {
value = undefined; value = undefined;
errors.push({ if (i.required)
name: i.name, errors.push({
error: `The entered choice is invalid. Please choose one of the following options: ${option.choices name: i.name,
.map(x => x.name) error: `The entered choice is invalid. Please choose one of the following options: ${option.choices
.join(', ')}.`, .map(x => x.name)
fullError: ['STRING_INVALID_CHOICE', option.choices], .join(', ')}.`,
}); fullError: ['STRING_INVALID_CHOICE', option.choices],
});
break; break;
} }
value = choice.value; value = choice.value;
@ -778,32 +782,35 @@ export class HandleCommand {
} }
if (Number.isNaN(value)) { if (Number.isNaN(value)) {
value = undefined; value = undefined;
errors.push({ if (i.required)
name: i.name, errors.push({
error: 'The entered choice is an invalid number.', name: i.name,
fullError: ['NUMBER_NAN', args[i.name]], error: 'The entered choice is an invalid number.',
}); fullError: ['NUMBER_NAN', args[i.name]],
});
break; break;
} }
if (option.min_value) { if (option.min_value) {
if (value < option.min_value) { if (value < option.min_value) {
value = undefined; value = undefined;
errors.push({ if (i.required)
name: i.name, errors.push({
error: `The entered number is less than ${option.min_value}. The minimum allowed is ${option.min_value}`, name: i.name,
fullError: ['NUMBER_MIN_VALUE', option.min_value], error: `The entered number is less than ${option.min_value}. The minimum allowed is ${option.min_value}`,
}); fullError: ['NUMBER_MIN_VALUE', option.min_value],
});
break; break;
} }
} }
if (option.max_value) { if (option.max_value) {
if (value > option.max_value) { if (value > option.max_value) {
value = undefined; value = undefined;
errors.push({ if (i.required)
name: i.name, errors.push({
error: `The entered number is greater than ${option.max_value}. The maximum allowed is ${option.max_value}`, name: i.name,
fullError: ['NUMBER_MAX_VALUE', option.max_value], error: `The entered number is greater than ${option.max_value}. The maximum allowed is ${option.max_value}`,
}); fullError: ['NUMBER_MAX_VALUE', option.max_value],
});
break; break;
} }
} }
@ -812,13 +819,14 @@ export class HandleCommand {
const choice = option.choices.find(x => x.name === args[i.name]); const choice = option.choices.find(x => x.name === args[i.name]);
if (!choice) { if (!choice) {
value = undefined; value = undefined;
errors.push({ if (i.required)
name: i.name, errors.push({
error: `The entered choice is invalid. Please choose one of the following options: ${option.choices name: i.name,
.map(x => x.name) error: `The entered choice is invalid. Please choose one of the following options: ${option.choices
.join(', ')}.`, .map(x => x.name)
fullError: ['NUMBER_INVALID_CHOICE', option.choices], .join(', ')}.`,
}); fullError: ['NUMBER_INVALID_CHOICE', option.choices],
});
break; break;
} }
value = choice.value; value = choice.value;