Hi,

Is it possible to implement this in a more elegant way, where we can 
collapse those three schedule fields into one, and different schema nodes 
would be used based on the value of job_type? 


class JobScheduleSchema(colander.MappingSchema):
    job_type = colander.SchemaNode(colander.String(), 
validator=colander.OneOf(['simple', 'interval', 'cron']))
    simple_schedule = colander.SchemaNode(colander.DateTime(), 
missing=None, validator=...)
    interval_schedule = colander.SchemaNode(colander.String(), 
missing=None, validator=...)
    cron_schedule = colander.SchemaNode(colander.String(), missing=None, 
validator=...)

    @staticmethod
    def validator(node, value):  # validate schedule dict
        if value['job_type'] == 'simple':
            if not value['simple_schedule']:
                raise colander.Invalid(node, 'Specify a simple schedule.')
        elif value['job_type'] == 'interval':
            if not value['interval_schedule']:
                raise colander.Invalid(node, 'Specify a interval schedule.')
        elif value['job_type'] == 'cron':
            if not value['cron_schedule']:
                raise colander.Invalid(node, 'Specify a cron schedule.')
        else:
            raise colander.Invalid(node, 'Invalid job type.')

ie.

class JobScheduleSchema(colander.MappingSchema):
    job_type = colander.SchemaNode(colander.String(), 
validator=colander.OneOf(['simple', 'interval', 'cron']))
    schedule = colander.SchemaNode(colander.DateTime()) if job_type == 
"simple", else: IntervalScheduleSchema()

Thanks,
Otto

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to