sailorproject / sailor

A Lua MVC Web Framework.
MIT License
924 stars 124 forks source link

Suggestion: Model attributes structure #115

Open xspager opened 8 years ago

xspager commented 8 years ago

Can't the model attributes table a plain table? Instead of:

 post.attributes = { 
-- { <attribute> = <validation function, valua required> or "safe" if no validation required for this attribute} 
  { id = "safe" }, -- No validation rules
  { author = val:new().not_empty().len(6,20) }, -- Cannot be empty and must have between 6 and 20 characters
  { title = val:new().not_empty().len(6,100) }, -- Cannot be empty and must have between 6 and 100 characters
  { body = val:new().not_empty() } -- Cannot be empty
}

be like:

post.attributes = { 
-- <attribute> = <validation function, valua required> or "safe" if no validation required for this attribute 
  id = "safe", -- No validation rules
  author = val:new().not_empty().len(6,20), -- Cannot be empty and must have between 6 and 20 characters
  title = val:new().not_empty().len(6,100), -- Cannot be empty and must have between 6 and 100 characters
  body = val:new().not_empty() -- Cannot be empty
}
`
Etiene commented 8 years ago

It was like that before! The problem is that plain tables will not loop in a specific order. So the validations of the attributions were running in different orders every time. I changed to nested tables so it goes in the same order as they are declared.

mpeterv commented 8 years ago

You could also sort keys before validating. This is typically implemented using utility iterator "sorted_pairs". It won't be the declaration order but it will be deterministic.

limadm commented 8 years ago

I do not feel a need for ordered validations, could not sailor support both? Like... use ipairs if model.attributes[1] ~= nil or #model.attributes >0, pairs otherwise.