Skip to content

Commit

Permalink
fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
catplvsplus committed Sep 3, 2024
1 parent 3104029 commit 855466b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
2 changes: 0 additions & 2 deletions example/modules/Commands/Resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ export class Resolvers {
onStart({ client }) {
client.on('recipleCommandExecute', async (data) => {
if (data.type !== CommandType.MessageCommand) return;

logger.log(data.flags.cache.values());
});

return true;
Expand Down
2 changes: 1 addition & 1 deletion example/modules/Halts/HandleError.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ExampleHalt {
break;
}

console.log(data.error);
logger.error(data.error);

return true;
}
Expand Down
16 changes: 8 additions & 8 deletions packages/core/src/classes/structures/MessageCommandFlagValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface MessageCommandFlagValueData<T extends any = any, V extends 'str
}

export interface MessageCommandFlagParseOptionValueOptions<T extends any = any> extends Omit<MessageCommandFlagBuilderResolveValueOptions<T>, 'values'> {
values?: T[]|null;
values?: (string|boolean)[]|null;
}

export interface MessageCommandFlagValueOptions<T extends any = any, V extends 'string'|'boolean' = 'string'|'boolean'> extends MessageCommandFlagValueData<T, V>, Pick<MessageCommandFlagParseOptionValueOptions<T>, 'parserData'|'command'> {
Expand Down Expand Up @@ -98,28 +98,28 @@ export class MessageCommandFlagValue<T extends any = any, V extends 'string'|'bo
}

public static async parseFlagValue<T extends any = any>(options: MessageCommandFlagParseOptionValueOptions<T>): Promise<MessageCommandFlagValue<T>> {
const filteredValues = options.values?.filter(value => typeof value == options.flag.value_type);
const missing = !!options.flag.required && !filteredValues?.length;
const values = options.values?.filter(value => typeof value == options.flag.value_type) ?? [];
const missing = !!options.flag.required && !values.length;

const validateData = !missing
? options.flag.validate && filteredValues?.length
? options.flag.validate && values?.length
? await Promise.resolve(options.flag.validate({
values: filteredValues as string[]|boolean[],
values: values as string[]|boolean[],
flag: options.flag,
parserData: options.parserData,
command: options.command,
message: options.message,
client: options.client,
}))
: true
: new RecipleError(RecipleError.createCommandRequiredFlagNotFoundErrorOptions(options.flag.name, filteredValues?.join(', ') ?? 'undefined'));
: new RecipleError(RecipleError.createCommandRequiredFlagNotFoundErrorOptions(options.flag.name, values?.join(', ') ?? 'undefined'));

return new MessageCommandFlagValue({
name: options.flag.name,
flag: options.flag,
values: (filteredValues ?? []) as string[]|boolean[],
values: values as string[]|boolean[],
missing,
invalid: !validateData,
invalid: validateData !== true,
message: options.message,
error: typeof validateData !== 'boolean'
? typeof validateData === 'string' ? new Error(validateData) : validateData
Expand Down

0 comments on commit 855466b

Please sign in to comment.