As a beginner in TS/JS, I am looking to validate multiple conditions passed as arguments to a function. For instance, currently I am verifying the user role name, but in the future, I may need to check other conditions.
validateUserDetails(): Promise<Boolean> {
return new Promise<Boolean>((resolve, reject) => {
this.currentLoggedInUserRole = this.sharedService.getCurrentUser()['roleName'];
if (this.currentLoggedInUserRole) {
let url = `<some GET url>`;
this.http
.get<any>(url)
.pipe(
take(1),
catchError((error) => {
reject(false);
return throwError(error);
})
)
.subscribe((response: any) => {
if (
response.length > 0 && response.some((user) => user['roleName'] === this.currentLoggedInUserRole)) {
resolve(true);
} else {
resolve(false)
}
});
}
});
}
this.userValidationService.validateUserDetails().then(isUserValid => {
//some logic
}).catch((error) => {console.log(new Error(error))})
I aim to pass conditions to be checked as arguments to the function like below, perhaps using arrays or maps rather than comma separated values.
this.userValidationService.validateUserDetails(['userRole', userID])
.
this.userValidationService.validateUserDetails('userRole').then(isUserValid => {
//some logic
}).catch((error) => {console.log(new Error(error))})
My question is how can I pass arguments with multiple conditions and, how can I handle them inside my promise to validate all/partial conditions. If we have ['userRole', 'userID', 'clientID'], and userRole returns true, userID returns false, and clientID returns true, the consolidated result should be false - meaning if any one condition fails, the result should be false. How can this be achieved using rxjs forkJoin()
?