Validations with the SQLAlchemy version of Python Eve

Peter Swords

I'd like to use the Eve framework for creating a REST api and performing data validations. But I want to use the SQLAlchemy version of Eve with an rdbms back end. The Eve-SQLAlchemy documentation says nothing about how to do this.

For example, I will have a db table called People:

# sql_schema.py
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import column_property, relationship

Base = declarative_base()

class People(Base):
    __tablename__ = 'people'
    id = Column(Integer, primary_key=True, autoincrement=True)
    firstname = Column(String(80))
    lastname = Column(String(120))
    fullname = column_property(firstname + " " + lastname)

Later I tell Eve about my database definitions:

from eve_sqlalchemy.config import DomainConfig, ResourceConfig
from sql_schema import People

# The default schema is generated using DomainConfig:
DOMAIN = DomainConfig({
    'people': ResourceConfig(People)
}).render()

# now I can stuff in my validations, a bit klunkily
DOMAIN['people']['schema']['firstname'].update({'allowed': ['Pete']})

The above works! If I try to store People with a firstname other than 'Pete' I get a validation error. But it's a bit klunky to bolt on the validations after the schema definition like this. For instance, Eve picks up the max length 80 constraint from the People table definition. It would be nice if the firstname allowed constraint could be specified there too. Is there a recommended approach, and is it supported at all or liable to break in a future version.

dkellner

I've tried to clarify the relationship between DomainConfig and Eve settings in the docs: https://eve-sqlalchemy.readthedocs.io/en/latest/tutorial.html#eve-settings :

You can do so using DomainConfig and ResourceConfig, which will give you a default schema (DOMAIN dictionary) derived from your SQLAlchemy models. This is intended as a starting point and to save you from redundant configuration, but there's nothing wrong with customizing this dictionary if you need to!

# examples/simple/settings.py

# ...

# Even adding custom validations just for the REST-layer is possible:
DOMAIN['invoices']['schema']['number'].update({
    'min': 10000
})

That said, if you actually have a constraint specified in your SQLAlchemy models that is not automatically translated into proper validation rules in Eve's schema definition, please open an issue!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Eve, SQLalchemy and ForeignKey

From Dev

Python Eve contains filter

From Dev

Enum type in python eve schema

From Dev

python-eve array field contains query

From Dev

submit request (post) internally in python-eve

From Dev

python-eve oplog in not working in my application

From Dev

Python Eve 0.8: how to perform a $geowithin with a $centerSphere?

From Dev

Python eve validation with value from another property

From Dev

Debugging error 500 issues in Python EVE

From Dev

Python-eve: Create a custom webservice

From Dev

gunicorn fails to launch python-eve

From Dev

Filtering on embedded document with Python Eve Rest and Mongo

From Dev

Python Eve - Query Embedded Data Relation

From Dev

calling javascript procedure with python-eve

From Dev

Python Eve: fields not returned in default configuration

From Dev

Setting MongoDB authorization configuration in Python-Eve

From Dev

Running Python Eve Rest API in Production

From Dev

Unit Test cases for Python Eve Webservices

From Dev

Use variables in geonear aggregator in Python Eve with MongoDB

From Dev

How add checks/validations in python for user input?

From Python

Pandas dataframe Grouping and Counting with validations in Python

From Dev

data_validations in different worksheets python

From Dev

Python EVE:- Blocking POST Method and Enabling PUT Method for some endpoints in Python EVE

From Dev

What does 'Environment: production' mean for python-eve?

From Dev

WSGI with python eve [Errno 32] Broken pipe... Ajax call

From Dev

Python Eve Auth can't return 401 exceptions

From Dev

Python Eve - REST API additional_lookup not working

From Dev

connect docker container with python-eve app to docker container with mongodb

From Dev

What is the format of the request for authentication to python-eve api

Related Related

  1. 1

    Python Eve, SQLalchemy and ForeignKey

  2. 2

    Python Eve contains filter

  3. 3

    Enum type in python eve schema

  4. 4

    python-eve array field contains query

  5. 5

    submit request (post) internally in python-eve

  6. 6

    python-eve oplog in not working in my application

  7. 7

    Python Eve 0.8: how to perform a $geowithin with a $centerSphere?

  8. 8

    Python eve validation with value from another property

  9. 9

    Debugging error 500 issues in Python EVE

  10. 10

    Python-eve: Create a custom webservice

  11. 11

    gunicorn fails to launch python-eve

  12. 12

    Filtering on embedded document with Python Eve Rest and Mongo

  13. 13

    Python Eve - Query Embedded Data Relation

  14. 14

    calling javascript procedure with python-eve

  15. 15

    Python Eve: fields not returned in default configuration

  16. 16

    Setting MongoDB authorization configuration in Python-Eve

  17. 17

    Running Python Eve Rest API in Production

  18. 18

    Unit Test cases for Python Eve Webservices

  19. 19

    Use variables in geonear aggregator in Python Eve with MongoDB

  20. 20

    How add checks/validations in python for user input?

  21. 21

    Pandas dataframe Grouping and Counting with validations in Python

  22. 22

    data_validations in different worksheets python

  23. 23

    Python EVE:- Blocking POST Method and Enabling PUT Method for some endpoints in Python EVE

  24. 24

    What does 'Environment: production' mean for python-eve?

  25. 25

    WSGI with python eve [Errno 32] Broken pipe... Ajax call

  26. 26

    Python Eve Auth can't return 401 exceptions

  27. 27

    Python Eve - REST API additional_lookup not working

  28. 28

    connect docker container with python-eve app to docker container with mongodb

  29. 29

    What is the format of the request for authentication to python-eve api

HotTag

Archive