gristlabs/ts-interface-checker

checker.check(obj).then( handle success and errors )

ohabash opened this issue · 2 comments

Seems like a lot of people already figured it out. But how can i get specific responses from checker.check.... Here is my code

(Product_Put).check(this.bcObj);

This will throw in the console at runtime but how do i get the message in TS so i can make errors for my users. This is what i tried

 try {
      (Product_Put).check(this.bcObj);
    } catch (e) {
      console.log('this returns the same throw as string', e);
    }

but that is not ideal for isolating the error or success msg.

Can you share examples on how to parse results?

Thank you!

Besides check(), two other methods you might find useful are test() and validate(). test() returns simply true or false, for success or error.

validate() is probably what you are looking for: it returns an object IErrorDetail on error, and null on success. For example, it could have a value like

{
        path: "value", message: "is not a Square",
        nested: [{
          path: "value.size", message: "is missing",
        }]
}

I should also mention that the error thrown includes e.path property, for the property that cause the check to fail. But for full details, you can call validate(), either in place of check() or inside the catch clause.