totaljs / framework4

Total.js framework v4
https://www.totaljs.com
Other
99 stars 36 forks source link

Is there a way to change the default ErrorBuilder Object Structure? #42

Closed LandyCuadra closed 2 years ago

LandyCuadra commented 2 years ago

Is the feature request related to a problem? Please describe. I am trying to customize the error response by adding/changing some keys to it:

this is the default error object

{
        name: 'String: error-name-or-error-code',
        error: 'String: error description',
        path: 'String: absolute path to invalid variable',
        index: Number // optional, array index if some item in array is invalid
}

Describe the solution you'd like would to know a way to add fields, that can be invoke with $.invalid()

{
        code: 'Number: Error code',
        name: 'String: error-name-or-error-code',
        message: 'String: error description',
        show: 'Boolean: flag to show or not error message',
}

Describe alternatives you've considered I have tried using the ErrorBuilder AddTransform function, to rename the fields.... but but it is kind of messy to assign builder['show'] = Boolean(err.path)... also I am limited to the quantity of fields of the default object

petersirka commented 2 years ago

How do you imagine to assign .show field? From where? Where do you want to specify it?

BTW: you can extend error builder instance dynamically in the schemas like this:

$.error. // --> is ErrorBuilder instance
$.error.visible = { errorid1: true, errorid2: true };

Then you can use it in transformation like this:

ErrorBuilder.addTransform('custom', function(isresponse) {
    this.visible. // your data
    for (var err of this.items)
        err.show = this.visible[err.name];
}, true);
LandyCuadra commented 2 years ago

You suggestion was correct, I was misunderstanding the function of error builder.... Finally I solved it with $.success(false, ErrorObject)... Thank you