1

I'm building the REST API of a platform I'm working on in Ruby, on top of Sinatra.

Data comes in during a POST, at that stage, I check the format of parsed JSON against a validation hash built for each route with hash_validator.

The POST data, if valid, is passed into as a single hash to create a new class (e.g. User). So at the initialization, I check each variable to make sure they exist and are not nil.

Parts of the same object can reside in various collections (UserInfo, UserSubscriptions, UserPasswordHash etc.), so when loading the class from data store, I again validate each hash received through (in my case, Mongo).

This works so far but with a lot of repetitions of variable names around the code and it may still not be data-proof.

I'm following my engineering guts as much as possible on this but is there a common model to follow when validating input at various stages in the app?

4

1 回答 1

0

从数据库加载数据时,您永远不需要检查您的验证。如果您在途中验证,您可以假设它是有效的。我将在一步中执行验证。使用传递的数据初始化您的对象并检查以确保各种属性有效。如果发布的数据无效,则属性将无法通过验证

于 2013-09-21T03:30:49.653 回答