tagged_item_validation.py 1.86 KB
Newer Older
1
from .checks import check_required
2
from .validators import validate_url, validate_text
3
from rest_framework.validators import ValidationError
4 5 6 7 8


def tagged_item_validation(attrs):
    tag_config = attrs["tag"].config
    sumbitted_content = attrs["custom_content"]
9
    validate_content_against_config(tag_config, sumbitted_content)
10

11 12 13 14 15 16 17 18 19 20 21

def validate_content_against_config(config, content):
    check_required(config, content)

    for field in content:
        if field not in config:
            raise ValidationError(
                "{} : this field is not supposed to be there".format(field))

        field_submitted = content[field]
        field_config = config[field]
22 23 24 25 26 27
        field_type = field_config['type']

        if field_type == 'url':
            validate_url(field_config, field_submitted)
        elif field_type == 'text':
            validate_text(field_config, field_submitted)
28
        elif field_type == 'array':
29
            if type(field_submitted) is not list:
30
                raise ValidationError("A array is required here !")
31 32
            for item in field_submitted:
                validate_content_against_config(field_config['content'], item)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        else:
            raise Exception("Dev, you have implement something here...")


# my_tag_config = {
#     "url": {
#         "type": "url",
#         "required": True,
#         "validators": {
#             "extension": ["jpg", "jpeg", "png"]
#         },
#     },
#     "title": {
#         "type": "text",
#         "required": True,
#         "validators": {
#             "max_length": 200
#         }
#     },
#     "description": {
#         "required": False,
#         "type": "text",
#         "validators": {
#             "max_length": 500
#         }
#     },
#     "licence": {
#         "type": "text",
#         "required": False,
#         "validators": {
#             "max_length": 200
#         }
#     }
# }