Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Add Validation to Path Parameters #13

Closed
ParentJA opened this issue Feb 24, 2019 · 3 comments
Closed

Add Validation to Path Parameters #13

ParentJA opened this issue Feb 24, 2019 · 3 comments
Labels
enhancement New feature or request

Comments

@ParentJA
Copy link

Django lets you add validation to path parameters in its URL config.

# Using django.urls.path
'tenant/<uuid:tenant_uuid>/'

# Using django.urls.re_path
r'tenant/(?P<tenant_uuid>[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{8})/'

I'd like something similar in Minik.

@pdiazvargas pdiazvargas added the enhancement New feature or request label Feb 24, 2019
@incognos
Copy link

Stop torturing him @ParentJA :-) - question, why use this framework instead of Serverless https://www.serverless.com ? Up https://up.docs.apex.sh ? Architect https://arc.codes/guides/offline? You might want to add yourself to https://github.com/anaibol/awesome-serverless

@pdiazvargas
Copy link
Contributor

pdiazvargas commented Mar 21, 2019

@incognos great resources indeed! however, using a nodejs library to write python serverless API is not ideal at all. Also, if you're interested in python frameworks, I would actually suggest https://github.com/aws/chalice

The main purpose of this project was to create a minimal serverless framework. This means it will not have full feature parity with django or flask, but it will be functional and lightweight.
Give it a try!

@pdiazvargas
Copy link
Contributor

pdiazvargas commented Mar 25, 2019

Using metadata annotations seems to be a really natural fit for this type of feature. After a few iterations, this is how the route definition looks like:

@sample_app.route('/articles/{year}/{month}/', methods=['GET'])
def get_articles_view(year: int, month: int):
    # match /articles/2015/10/
    # match /articles/2020/5234 which is not ideal.
    # does not match /articles/2020/ten
    assert isinstance(year, int) and isinstance(month, int)
    return {'year': year, 'month': month}

Note that in order to implement the uuid, I was able to use the standard uuid.UUID class. The added advantage is that the product_id field that gets passed to the view is already an instance of the object.

@sample_app.route('/product/{product_id}/', methods=['GET'])
def get_product(product_id: uuid.UUID):
    assert isinstance(product_id, uuid.UUID)
    return {'id': str(product_id)}

Thoughts?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants