checks.py 587 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from rest_framework.validators import ValidationError


def missing_field(field):
    return ValidationError("{} : this field is required".format(field))


def check_required(config, content):
    for field in config:
        if config[field]['required']:
            try:
                val = content[field]
                if type(val) is str:
                    if len(val) == 0:
                        raise missing_field(field)
                if val is None:
                    raise missing_field(field)
            except KeyError:
                raise missing_field(field)