I'm trying to use the
# package-development
m
I'm trying to use the UmbValidationContext for validation in my package. I've got a modal with steps, so each step has a sub-context. Before sending the validation to the server I want to ensure that the parent context (and there for sub-contexts) are valid so I'm calling
await this.validationContext.validate()
, then checking
this.validationContext.isValid
My problem is that
validationContext.validate()
will reject the promise if validation fails which outputs some more or less random "Uncaught (in promise) undefined" to the log and I can't seem to understand how to catch these. My call to
validate()
is inside a try catch. These errors only seem to happen when I'm navigating away from a step (hence while one of the sub-contexts are leaving being destroyed). I'm I doing something fundamentally wrong here to validate the context? https://cdn.discordapp.com/attachments/882984798719729704/1295763525272076368/ns-validation-rejection.gif?ex=670fd552&is=670e83d2&hm=7d9c9fbb64e1169ddf387a2c7c74a297ffbcbcebc013a731ae6230677e0c1a5f&
I've spotted a couple of places where the core calls
validate()
without awaiting (for example during destroy). @Niels Lyngsø Maybe you know if this is intentional? https://cdn.discordapp.com/attachments/1295763525658087558/1295766222197231639/image.png?ex=670fd7d5&is=670e8655&hm=c6f4f6d5a5f3ca7a66ad3e26be954acf1c7e174342df6d48e7c749a60bf08e82&
n
@Markus Johansson I understand your pain in this regards, let me first highlight this is Native JS, not backoffice specific. It is not very intuitive that you can choose just to await a promise, but if it gets rejected you will get an error. The right implementation is to have an implementation that handles the rejection. This can be done either by wrapping your code in a try-catch or instead of calling await do a
.then
That can be done in many ways but here is an example where we transform that promise into a promise that resolves in true/false depending on the outcome.
Copy code
const resultsStatus = await validationContext.validate().then(
            () => true,
            () => false,
        );
Regarding the intentional not awaiting method calls. That is on purpose — If you do not need to wait for the call to finish. Like if you dont do anything afterwards or waits for the result of it. Then there is no need to await it. 🙂
m
Thank you very much for the great explanation @Niels Lyngsø 🎉 My current attempt looked like this:
Copy code
try {
  await this.validation?.validate().catch((detail)=>{
    console.log('catched ', detail);
  });

} catch (error) {
  console.log('error inside hasValidationError',error);
}

if(!this.validation.isValid){
   // Do stuff
   return;
}

// Do other stuff
return;
But as you're pointing out, using
validate()
like that would mean that I don't have to check
.isValid
after the call I guess. I'll give it a try to see what happens 😄
n
Yes, exactly. that’s the point, it only resolves if the validation was good 🙂
Cool, let us know how it goes
m
I've tried the changes that you suggested but still getting these errors in the log. There are several calls to
this.validate()
inside
validation.controller.ts
that does not have any
.then(..)
or
catch(..)
, like I mentioned before they are just "fire and forget" - but if the promise is rejected - wouldn't that be a potential reason for the console errors that I'm seeing? Really hard to understand where these rejections are coming from 🤔 https://cdn.discordapp.com/attachments/1295763525658087558/1296606426646577282/image.png?ex=6712e655&is=671194d5&hm=99beb8290b5c287e1160c1fafff2abcd546d614f0ec73d94bf21d352fcfbba36&
n
You are right, that seems like a issue that we should fix 🙂
Could you write up the case that causes this to happen? Just so I can re-produce the case?
5 Views