Skip to content

Semantics of validation from path has changed #411

@dairiki

Description

@dairiki

In Cornice < 2, when a schema item had location=path, it was extracted/validated from request.matchdict. This allowed for code like

class MySchema(colander.Schema):
    item_id = colander.SchemaNode(
        colander.Integer(),
        location='path')

@resource(
    path='/items/{item_id}.json',
    schema=MySchema)
class ItemResource(object):
    def __init__(self, request):
        self.request = request

    def get(self):
        item_id = request.validated['item_id']
        return {'id': item_id}

In Cornice >= 2, I would naively translate the above to

class PathSchema(colander.Schema):
    item_id = colander.SchemaNode(colander.Integer())

class MySchema(colander.Schema):
    path = PathSchema()

@resource(
    path='/items/{item_id}.json',
    schema=MySchema,
    validators=(colander_validator,))
class ItemResource(object):
    def __init__(self, request):
        self.request = request

    def get(self):
        item_id = request.validated['path']['item_id']
        return {'id': item_id}

This does not, however, seem to work. Currently, extract_cstruct puts request.path (a string) into cstruct['path'] (and does not appear to make request.matchdict available at all.)

I would suggest that extract_cstruct should be modified to make request.matchdict available in the cstruct; either as cstruct['path'] (replacing request.path) or as cstruct['matchdict'].

Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions