Skip to content

Improve server-side assert() function to TypeScript use #437

@birdofpreyru

Description

@birdofpreyru

I guess, we should require the caller to specify expected type of asserted value, and make it clear to TS that this assertion / cast is done there.

export function assert(
value: unknown,
schema: joi.AnySchema,
message = '',
statusCode = CODES.BAD_REQUEST,
): void {
const { error } = schema.validate(value, { abortEarly: false });
if (error) {
fail(message.concat(message ? '\n' : '', error.message), statusCode);
}
}

Update: After first tests, it seems problematic to do something useful here, both due to peculiarities of TS compiler and its support of asserts feature, and also due to the way express requests are typed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important. Nice to have improvements and optimizations.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions