Alex Kay
Alex Kay

Reputation: 23

How to make all Voluptuous fields required with a single command?

I try to check my JSON fields and their types. So, I use voluptuous and its methods.

An example is here.

check = Schema({
Required('Id'): All(str, Length(min=1)),
Required('CalculationId'): All(str, Length(min=1)),
Required('Routes'): All([
    {Required('Id'): All(str, Length(min=1)),
     Required('Origin'): {
         Required('Longitude'): float, 
         Required('Latitude'): float},
     Required('Destination'): {
         Required('Longitude'): float, 
         Required('Latitude'): float}}], Length(min=1)),
Required('CreateDate'): str,
Required('CreateUserName'): str,
Required('CreateUserEmail'): str})

Can I make all fields required in a shorter and more convenient way? I would like to do not use this "Required" mark at the beginning of each line =)

Upvotes: 0

Views: 310

Answers (1)

gore
gore

Reputation: 571

Can I make all fields required in a shorter and more convenient way? I would like to do not use this "Required" mark at the beginning of each line

Send required=True param to Schema() after the json.

Example:

Genre = Schema(
    {
        'id': int,
        'label': str,
    },
    required=True)

Upvotes: 1

Related Questions