Try to search your question here, if you can't find : Ask Any Question Now ?

Validations with the SQLAlchemy version of Python Eve

HomeCategory: stackoverflowValidations with the SQLAlchemy version of Python Eve
rupesh asked 2 weeks ago

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:

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)

# 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 scheme definition like this. Is there a recommended approach, and is it supported at all or liable to break in a future version.

1 Answers
Best Answer
Mikhail answered 2 weeks ago
Your Answer

2 + 17 =

Popular Tags

WP Facebook Auto Publish Powered By :