简体   繁体   English

使用Colander验证PATCH请求

[英]Using Colander to validate PATCH requests

EDIT: My original question refered to PUT requests, I have changed it to PATCH based on the answer provided by thecoshman. 编辑:我的原始问题提到PUT请求,我已根据thecoshman提供的答案将其更改为PATCH。

I am developing a RESTful webservice using cornice and I have recently discovered colander. 我正在使用檐口开发RESTful Web服务,我最近发现了漏勺。 My question is related to PATCH requests. 我的问题与PATCH请求有关。 I now know PUT requests should be complete records but not so with PATCH requests. 我现在知道PUT请求应该是完整记录,但PATCH请求不是这样。 Can I use colander to validate json data attached to a PATCH request? 我可以使用漏勺来验证附加到PATCH请求的json数据吗?

Colander is great for validating POST requests as it ensures I have all the right data in my json and also removes any extraneous data. Colander非常适合验证POST请求,因为它确保我在json中拥有所有正确的数据,并且还可以删除任何无关的数据。

Here's my simple schema. 这是我的简单架构。

class OrganisationSchemaRecord(MappingSchema):
    orgname = SchemaNode(String())
    fullname = SchemaNode(String())
    description = SchemaNode(String(), missing=drop)

class OrganisationSchema(MappingSchema):
    organisation = OrganisationSchemaRecord()

This allows me to keep my view code simple like this. 这允许我像这样保持我的视图代码简单。

@view(validators=(unique,), renderer='json', schema=OrganisationSchema)
def collection_post(self):
    """Adds a new organisation"""
    org = DBOrg(**self.request.validated['organisation'])#sqlalchemy model
    DBSession.add(org)
    return {'organisation': org}

The magic being schema=OrganisationSchema which validates the json body of the request and places is in self.request.validated['organisation'] as per the schema. 神奇的是schema=OrganisationSchema ,它验证请求的json主体,并根据模式放置在self.request.validated['organisation']中。

It also works great with my other validator which ensures the primary key is not already in use. 它也适用于我的其他验证器,确保主键尚未使用。

def unique(request):
    if 'organisation' in request.validated: #Implies a validated schema
        orgname = request.validated['organisation']['orgname']
        if DBSession.query(DBOrg).get(orgname):
            request.errors.add('url', 'orgname', 'This organisation already exists!')

However, if I want to process a PATCH request to update the fullname or description fields then validation fails unless the request also includes a value for orgname which I don't want to change. 但是,如果我想处理PATCH请求以更新fullnamedescription字段,则验证将失败,除非请求还包含orgname的值,我不想更改。

What is the best solution? 什么是最好的解决方案? Do I insist on complete and valid records being PATCHed to the server, do I define a different schema or am I missing something? 我是否坚持将完整且有效的记录修复到服务器,我是否定义了不同的模式或者我错过了什么?

honestly, skipped over most of the question, so hopefully I didn't miss anything too big. 老实说,跳过大部分问题,所以希望我没有错过任何太大的东西。

Should PUT requests be full records - Yes, absolutely. Should PUT requests be full records - 是的,绝对的。

PUT requests put an entire replacement record at the URI you requested. PUT请求将完整的替换记录放在您请求的URI上。

If you want to perform a partial modification, you should use PATCH (which is surprisingly lesser known). 如果要执行部分修改,则应使用PATCH(这一点令人惊讶地鲜为人知)。 Before PATCH, the theory would be, GET the record, modify locally, PUT entire record back 在PATCH之前,理论将是,获取记录,在本地修改,PUT整个记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM