1

I'm new to TS/JS, I want to validate multiple condition passed as argument to the function

For example, here I'm checking for user role name, tmrw I might need to check some other condition

  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 want to pass the conditions to be check as argument to the function something below, tmrw I might have multiple values to pass I don't to pass as comma separated values, I want to pass may be like arrays or maps.this.userValidationService.validateUserDetails(['userRole', userID]).

this.userValidationService.validateUserDetails('userRole').then(isUserValid => {
  //some logic
}).catch((error) => {console.log(new Error(error))})

So my question is how can i pass arguments with multiple condition, If yes how can i handle inside my promise to check all/ partial conditions. supposingly we have ['userRole', 'userID', 'clientID']. If userRole returns true, userID returns false, clientId returns true, consolidated result should be false, basically if there is any one condition failing result should be false. How this can be achieved using rxjs forkJoin(). Thank you

1 Answer 1

1

You may looking for TypeScript - Rest Parameters

validateUserDetails(...fields: string[]): Promise<Boolean> {
  ...
}

Then you can pass function's parameters as sperate (comma) or array, the result sill be the same.

this.userValidationService.validateUserDetails(['userRole', 'userID', ...])

or

this.userValidationService.validateUserDetails('userRole', 'userID', ...)
Sign up to request clarification or add additional context in comments.

4 Comments

Just a query and how can i check for conditions inside promise. For eg; how can I check for userRole, UserId etc. I need to loop and check isn't it? can you update ur answer accordingly
Typescript can reference object field by string (same like Map<String, any>) eg. condition['userRole']; Now you can directly loop though all functions parameters without any pre-check.
I'm not sure sure how to make parallel check and give out one result true/false using rxjs forkJoin,. Need help on this can you explain this using forkJoin. Supposingly ['userRole', 'userID', 'clientID']. If userRole returns true, userID returns false, clientId returns true, consolidated result should be false, basically if there is any one condition failing result should be false. Need an help on this
Create new post would be more convenience to explain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.