Search code examples
mongodbevecerberus

Python-Eve. Declare an array of Object IDs on Cerberus schema


I am developing an API with Python-Eve and I need to create a MongoDB schema declaration using Cerberus to express a document like the one below:

{
    name : 'John Smith',
    type: 'home',
    devices : [
        ObjectID('1234'),
        ObjectID('ABCD'),
        ObjectID('D2AF'),
    ],
}

I would like to know how can I declare a Cerberus schema to have an array of ObjectID, as is the devices key above.

I would like to have a schema for an array of references to other documents, and maybe make them embeddable, as is the single element schema example below, taken from Python-Eve documentation:

{
     'author': {
         'type': 'objectid',
         'data_relation': {
             'resource': 'users',
             'field': '_id',
             'embeddable': True
         },
     },        
 }

I suspect this will need a custom type, but I still haven't figured how to do it.


Solution

  • OK, found how to express the devices:

    {   
        'devices': {
            'type': 'list',
            'schema': {
                'type': 'objectid',
                'data_relation': {
                    'resource': 'devices',
                    'field': '_id',
                    'embeddable': True
                },
            }
        }
    }
    

    The excellent Cerberus documentation has it.