stwhite
stwhite

Reputation: 3275

Build Python Voluptuous Schema within another Schema

I am attempting to validate a schema within another schema. The error I receive with the following schema is "expected a dictionary" for the campaign parameter.

The reason why I am trying to do this is so that if I change one field in the campaign, I don't have to go change it in the other schema as well. I've also tried to put the create_campaign_schema dictionary itself——without Schema()——into the create_ad_schema directly.

My initial schema looked like this and used Required('campaign'): All() but it doesn't allow for values to be set as False or None, which is essentially all that I'm trying to do:

create_ad_schema = Schema({
    Required('ad_type_id'): All(str),
    Optional('background'): All(str),
    Required('name'): All(str, Length(min=1, max=40)),
    Required('token'): All(str),
    Required('cpm'): Any(int, float),
    Required('impressions'): All(int, CheckAvailableImpressions()),
    Required('click_url'): All(str, Length(min=10, max=400)),
    Optional('click_tracking_url'): All(str, Length(min=10, max=400)),
    Optional('impression_urls'): All([str], Length(max=3)),
    Required('media'): All(str, Length(min=32, max=100)),
    Required('campaign'): All(),
})

Here is my latest attempt at solving this problem:

create_campaign_schema = Schema({
    Required('name'): All(str, Length(min=1, max=40)),
    Optional('description', default=None): All(str, Length(max=200)),
    Required('start_date'): All(str),
    Optional('end_date'): Any(str, False, None),
    Required('schedule'): All(str, ScheduleType()),
    Required('minimize_duplicate'): All(bool),
})

create_ad_schema = Schema({
    Required('ad_type_id'): All(str),
    Optional('background'): All(str),
    Required('name'): All(str, Length(min=1, max=40)),
    Required('token'): All(str),
    Required('cpm'): Any(int, float),
    Required('impressions'): All(int, CheckAvailableImpressions()),
    Required('click_url'): All(str, Length(min=10, max=400)),
    Optional('click_tracking_url'): All(str, Length(min=10, max=400)),
    Optional('impression_urls'): All([str], Length(max=3)),
    Required('media'): All(str, Length(min=32, max=100)),
    Required('campaign'): create_campaign_schema,
})

The expected data that would be processed by this schema is:

{
    'ad_type_id': 'homepage_banner',
    'name': 'Homepage Btanner',
    'cpm': 1,
    'impressions': 5,
    'click_url': 'http://test.com',
    'click_tracking_url': 'http://test.com',
    'impression_urls': ['http://test.com'],
    'media': 'fedf2e93260e8e895933e6c265b44029.jpg',
    'campaign': CampaignEmbeddedInUser(
        id = ObjectId('5ae7b207754ad24fa9a77a1a'), 
        status = 1, 
        created_at = datetime.datetime(2018, 5, 1, 0, 16, 57, 923000), 
        deleted_at = datetime.datetime(2018, 5, 1, 0, 16, 57, 924000), 
        name = 'Test', 
        description = '', 
        start_date = datetime.datetime(2018, 4, 30, 0, 0), 
        end_date = None, 
        schedule = 'impressions', 
        minimize_duplicate = False
    ),
    'token': 'homepage-btanner'
}

Upvotes: 1

Views: 525

Answers (0)

Related Questions