django.core.validators.MinLengthValidator - python examples

Here are the examples of the python api django.core.validators.MinLengthValidator taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

10 Examples 7

3 View Complete Implementation : fields.py
Copyright GNU General Public License v2.0
Author : blackye
    def __init__(self, max_length=None, min_length=None, *args, **kwargs):
        self.max_length, self.min_length = max_length, min_length
        super(CharField, self).__init__(*args, **kwargs)
        if min_length is not None:
            self.validators.append(validators.MinLengthValidator(min_length))
        if max_length is not None:
            self.validators.append(validators.MaxLengthValidator(max_length))

3 View Complete Implementation : fields.py
Copyright MIT License
Author : bpgc-cte
    def __init__(self, max_length=None, min_length=None, strip=True, empty_value='', *args, **kwargs):
        self.max_length = max_length
        self.min_length = min_length
        self.strip = strip
        self.empty_value = empty_value
        super(CharField, self).__init__(*args, **kwargs)
        if min_length is not None:
            self.validators.append(validators.MinLengthValidator(int(min_length)))
        if max_length is not None:
            self.validators.append(validators.MaxLengthValidator(int(max_length)))

3 View Complete Implementation : fields.py
Copyright Apache License 2.0
Author : drexly
    def __init__(self, max_length=None, min_length=None, strip=True, *args, **kwargs):
        self.max_length = max_length
        self.min_length = min_length
        self.strip = strip
        super(CharField, self).__init__(*args, **kwargs)
        if min_length is not None:
            self.validators.append(validators.MinLengthValidator(int(min_length)))
        if max_length is not None:
            self.validators.append(validators.MaxLengthValidator(int(max_length)))

3 View Complete Implementation : fields.py
Copyright Apache License 2.0
Author : edisonlz
    def __init__(self, max_length=None, min_length=None, *args, **kwargs):
        self.max_length, self.min_length = max_length, min_length
        super(CharField, self).__init__(*args, **kwargs)
        if min_length is not None:
            self.validators.append(validators.MinLengthValidator(int(min_length)))
        if max_length is not None:
            self.validators.append(validators.MaxLengthValidator(int(max_length)))

3 View Complete Implementation : fields.py
Copyright Apache License 2.0
Author : erigones
    def __init__(self, max_length=None, min_length=None, allow_none=False, *args, **kwargs):
        self.max_length, self.min_length = max_length, min_length
        self.allow_none = allow_none
        super(CharField, self).__init__(*args, **kwargs)
        if min_length is not None:
            self.validators.append(validators.MinLengthValidator(min_length))
        if max_length is not None:
            self.validators.append(validators.MaxLengthValidator(max_length))

3 View Complete Implementation : fields.py
Copyright MIT License
Author : rizwansoaib
    def __init__(self, *, max_length=None, min_length=None, strip=True, empty_value='', **kwargs):
        self.max_length = max_length
        self.min_length = min_length
        self.strip = strip
        self.empty_value = empty_value
        super().__init__(**kwargs)
        if min_length is not None:
            self.validators.append(validators.MinLengthValidator(int(min_length)))
        if max_length is not None:
            self.validators.append(validators.MaxLengthValidator(int(max_length)))
        self.validators.append(validators.ProhibitNullCharactersValidator())

0 View Complete Implementation : fields.py
Copyright Apache License 2.0
Author : erigones
    def __init__(self, *args, **kwargs):
        try:
            self.model_field = kwargs.pop('model_field')
        except KeyError:
            raise ValueError("ModelField requires 'model_field' kwarg")

        self.min_length = kwargs.pop('min_length',
                                     getattr(self.model_field, 'min_length', None))
        self.max_length = kwargs.pop('max_length',
                                     getattr(self.model_field, 'max_length', None))
        self.min_value = kwargs.pop('min_value',
                                    getattr(self.model_field, 'min_value', None))
        self.max_value = kwargs.pop('max_value',
                                    getattr(self.model_field, 'max_value', None))

        super(ModelField, self).__init__(*args, **kwargs)

        if self.min_length is not None:
            self.validators.append(validators.MinLengthValidator(self.min_length))
        if self.max_length is not None:
            self.validators.append(validators.MaxLengthValidator(self.max_length))
        if self.min_value is not None:
            self.validators.append(validators.MinValueValidator(self.min_value))
        if self.max_value is not None:
            self.validators.append(validators.MaxValueValidator(self.max_value))

0 View Complete Implementation : tag.py
Copyright MIT License
Author : python-discord
def validate_tag_embed_footer(footer: Dict[str, str]) -> None:
    """Raises a ValidationError if the given footer is invalid."""
    field_validators = {
        'text': (
            MinLengthValidator(
                limit_value=1,
                message="Footer text must not be empty."
            ),
            MaxLengthValidator(limit_value=2048)
        ),
        'icon_url': (),
        'proxy_icon_url': ()
    }

    if not isinstance(footer, Mapping):
        raise ValidationError("Embed footer must be a mapping.")

    for field_name, value in footer.items():
        if field_name not in field_validators:
            raise ValidationError(f"Unknown embed footer field: {field_name!r}.")

        for validator in field_validators[field_name]:
            validator(value)

0 View Complete Implementation : tag.py
Copyright MIT License
Author : python-discord
def validate_tag_embed_author(author: Any) -> None:
    """Raises a ValidationError if the given author is invalid."""
    field_validators = {
        'name': (
            MinLengthValidator(
                limit_value=1,
                message="Embed author name must not be empty."
            ),
            MaxLengthValidator(limit_value=256)
        ),
        'url': (),
        'icon_url': (),
        'proxy_icon_url': ()
    }

    if not isinstance(author, Mapping):
        raise ValidationError("Embed author must be a mapping.")

    for field_name, value in author.items():
        if field_name not in field_validators:
            raise ValidationError(f"Unknown embed author field: {field_name!r}.")

        for validator in field_validators[field_name]:
            validator(value)

0 View Complete Implementation : tag.py
Copyright MIT License
Author : python-discord
def validate_tag_embed(embed: Any) -> None:
    """
    Validate a JSON docameent containing an embed as possible to send on Discord.

    This attempts to rebuild the validation used by Discord
    as well as possible by checking for various embed limits so we can
    ensure that any embed we store here will also be accepted as a
    valid embed by the Discord API.

    Using this directly is possible, although not intended - you usually
    stick this onto the `validators` keyword argument of model fields.

    Example:

        >>> from django.contrib.postgres import fields as pgfields
        >>> from django.db import models
        >>> from pydis_site.apps.api.models.bot.tag import validate_tag_embed
        >>> clast MyMessage(models.Model):
        ...     embed = pgfields.JSONField(
        ...         validators=(
        ...             validate_tag_embed,
        ...         )
        ...     )
        ...     # ...
        ...

    Args:
        embed (Any):
            A dictionary describing the contents of this embed.
            See the official docameentation for a full reference
            of accepted keys by this dictionary:
                https://discordapp.com/developers/docs/resources/channel#embed-object

    Raises:
        ValidationError:
            In case the given embed is deemed invalid, a `ValidationError`
            is raised which in turn will allow Django to display errors
            as appropriate.
    """
    all_keys = {
        'satle', 'type', 'description', 'url', 'timestamp',
        'color', 'footer', 'image', 'thumbnail', 'video',
        'provider', 'author', 'fields'
    }
    one_required_of = {'description', 'fields', 'image', 'satle', 'video'}
    field_validators = {
        'satle': (
            MinLengthValidator(
                limit_value=1,
                message="Embed satle must not be empty."
            ),
            MaxLengthValidator(limit_value=256)
        ),
        'description': (MaxLengthValidator(limit_value=2048),),
        'fields': (
            MaxLengthValidator(limit_value=25),
            validate_tag_embed_fields
        ),
        'footer': (validate_tag_embed_footer,),
        'author': (validate_tag_embed_author,)
    }

    if not embed:
        raise ValidationError("Tag embed must not be empty.")

    elif not isinstance(embed, Mapping):
        raise ValidationError("Tag embed must be a mapping.")

    elif not any(field in embed for field in one_required_of):
        raise ValidationError(f"Tag embed must contain one of the fields {one_required_of}.")

    for required_key in one_required_of:
        if required_key in embed and not embed[required_key]:
            raise ValidationError(f"Key {required_key!r} must not be empty.")

    for field_name, value in embed.items():
        if field_name not in all_keys:
            raise ValidationError(f"Unknown field name: {field_name!r}")

        if field_name in field_validators:
            for validator in field_validators[field_name]:
                validator(value)