Interface ApiXHttpBodyValidator<T>

An interface used to implement a validator for a JSON body that is part of a request.

interface UserLoginJsonSchema extends ApiXRequestInputSchema {
readonly username: string;
readonly password: string;
}

// a simple object to validate the username and password of a login endpoint.
class UserLoginHttpBodyValidator implements ApiXHttpBodyValidator<UserLoginJsonSchema> {

isValid(body: UserLoginJsonSchema): boolean {
const usernameRegex = /^[a-zA-Z0-9]{5,12}$/;
const passwordRegex = /^[a-zA-Z0-9@_.*&$!()-]{8,20}$/;
return this.isString(body.username) && usernameRegex.test(body.username)
&& this.isString(body.password) && passwordRegex.test(body.password);
}

isString(str: unknown): boolean {
return typeof str === 'string';
}
}
interface ApiXHttpBodyValidator<T> {
    isValid(body: T): boolean;
}

Type Parameters

Methods

Methods