Skip to content

Commit

Permalink
fix(validator): add comments and reformat into a more clear loop for …
Browse files Browse the repository at this point in the history
…creating the validation config
  • Loading branch information
atticusofsparta committed Nov 7, 2024
1 parent b0c9548 commit ea3e70c
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/common/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,25 +323,27 @@ export class AoANTReadable implements AoANTRead {
>
>;
} = {}): Promise<Record<AoANTHandler, { valid: boolean; error?: string }>> {
// default to the validations provided in the config
const handlerValidationConfig = validations ?? {};

// validations not specified in the config will be defaulted to a check that the handler is present
const antHandlers = await this.getHandlers();
const handlerValidationConfig = {
...(Object.fromEntries(
AntHandlerNames.map((handler) => [
handler,
async (_: { ant: AoANTRead }) => {
if (antHandlers.includes(handler)) return true;
throw new Error(`Handler ${handler} not found`);
},
]),
) as Record<
AoANTHandler,
(p: { ant: AoANTReadable }) => Promise<boolean>
>),
...(validations ?? {}),
};
for (const handlerName of AntHandlerNames) {
// skip if the handler is already in the config
if (handlerValidationConfig[handlerName]) continue;
// create a default validator for the handler and add it to the config
const handlerNameValidator = async (_: { ant: AoANTRead }) => {
if (antHandlers.includes(handlerName)) return true;
// this will be caught and assigned to the error for the validation result
throw new Error(`Handler ${handlerName} not found`);
};
handlerValidationConfig[handlerName] = handlerNameValidator;
}

const results: Record<AoANTHandler, { valid: boolean; error?: string }> =
{} as Record<AoANTHandler, { valid: boolean; error?: string }>;
const results = {} as Record<
AoANTHandler,
{ valid: boolean; error?: string }
>;

for (const [handler, validator] of Object.entries(
handlerValidationConfig,
Expand Down

0 comments on commit ea3e70c

Please sign in to comment.