0
到目前爲止,我正在使用漏勺驗證我的aiohttp應用程序中的數據。如何使用Pyramid的漏勺驗證應用程序邏輯?
我面臨的問題是我不知道如何做「深度」驗證。
考慮以下模式:
import colander
class User(colander.MappingSchema):
username = colander.SchemaNode(colander.String())
password = colander.SchemaNode(colander.String())
confirmation = colander.SchemaNode(colander.String())
我兩者都做驗證輸入數據結構具有所有必要的字段有(約束最小爲清楚起見),但我還需要 檢查:
username
尚未被其它用戶password
和confirmation
是相同
所以在我的控制器,代碼如下所示的僞代碼:
def create_user(request):
user = await request.json()
schema = User()
# do schema validation
try:
user = schema.deserialize(user)
except colander.Invalid, exc:
response = dict(
status='error',
errors=errors.asdict()
)
return json_response(response)
else:
# check password and confirmation are the same
if user['password'] != user['confirmation']:
response = dict(
status='error'
errors=dict(confirmation="doesn't match password")
)
return json_response(response)
# check the user is not already used by another user
# we want usernames to be unique
if user_exists(user['user']):
response = dict(
status='error',
errors=dict(username='Choose another username')
)
return json_response(response)
return json_response(dict(status='ok'))
基本上有有兩種驗證。是否有可能在單個漏斗模式中具有邏輯?這是一個很好的模式嗎?
Tx!這段代碼大多是僞代碼,它不是我正在運行的代碼。我有一個關於第三點的問題。如果我將所有查詢包含在一個事務中,它是否會提供與使用'on conflict'或捕獲重複用戶相同的功能? – amirouche
該代碼是在線https://github.com/amirouche/socialite/blob/master/socialite/api.py#L54 – amirouche
順便說一句我沒有使用漏勺,但traferet更容易寫IMO。 – amirouche