emmett icon indicating copy to clipboard operation
emmett copied to clipboard

Bug with 'in': {'dbset': ...} validation

Open josejachuf opened this issue 3 years ago • 5 comments

I have this validation

'f1': {'in': {'dbset': lambda db: db.where(db.A.f2 == session.f2)}}

In another way:

def val_f2(db):
    print(session.f2)
    return db.where(db.A.f2 == session.f2)
    
'f1': {'in': {'dbset': lambda db: val_f2(db)}}

The problem I have is that lambda runs only once and after that first time does not come to run, then it is always calling db.where from the first condition. I can check it with the print() that only runs once

Jose

josejachuf avatar Jul 05 '21 18:07 josejachuf

Or if it is not a bug, how can I do this dynamically?

josejachuf avatar Jul 05 '21 18:07 josejachuf

@josejachuf I'm sorry, but I'm not sure I got the issue.

What do you mean by

lambda runs only once and after that first time does not come to run, then it is always calling db.where from the first condition

Can you explain it differently?

gi0baro avatar Jul 06 '21 06:07 gi0baro

Hi @gi0baro

I hoped that by changing the value of session.f2 Validation is dynamic and contextual to what contains the value of session.f2.

About lambda, I thought that every time the validation was executed, I was going to take the conditions of the value of session.f2

Jose

josejachuf avatar Jul 06 '21 12:07 josejachuf

@josejachuf so, theoretically speaking, the validation functions get called every time you pass a value for the relevant field. Also theoretically speaking, the session values should be the ones part of the dedicated request. Then, if something doesn't work in this way, there's a bug and I will investigate.

Nevertheless, under an architecting point of view, I strongly disagree with the principle of binding models with requests, for a couple of reasons:

  • MVC strongly identifies roles of the tree components, and users' sessions are not part of the M part
  • biding models to sessions implicitly means you wouldn't be able to perform operations on such models outside of the request flow (background jobs, shell, etc.)

So IMHO the validation logics performed against users, or in general the request flow should be made in the C part of MVC paradigm, which means routes or pipes in Emmett.

gi0baro avatar Jul 07 '21 13:07 gi0baro

thanks @gi0baro for the info.

I ended up resolving as you say in the controller.

Jose

josejachuf avatar Jul 07 '21 14:07 josejachuf