在另一个Schema中构建Python Voluptuous Schema

时间:2018-05-01 00:58:49

标签: python python-3.x voluptuous

我正在尝试验证另一个架构中的架构。对于"expected a dictionary"参数,我使用以下架构收到的错误为campaign

我尝试这样做的原因是,如果我在广告系列中更改了一个字段,我也不必在其他模式中更改它。我还尝试将create_campaign_schema词典本身 - 不Schema() - 直接放入create_ad_schema

我的初始架构看起来像这样,并使用了Required('campaign'): All(),但它不允许将值设置为FalseNone,这基本上就是我所有的#39}我试图这样做:

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(),
})

以下是我解决此问题的最新尝试:

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,
})

此架构将处理的预期数据是:

{
    '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'
}

0 个答案:

没有答案