emmett-framework / emmett

The web framework for inventors
Other
1.03k stars 70 forks source link

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

Open josejachuf opened 3 years ago

josejachuf commented 3 years ago

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 commented 3 years ago

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

gi0baro commented 3 years ago

@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?

josejachuf commented 3 years ago

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

gi0baro commented 3 years ago

@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:

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.

josejachuf commented 3 years ago

thanks @gi0baro for the info.

I ended up resolving as you say in the controller.

Jose