Skip to content

Commit

Permalink
List length constrains (0.19.0 release) 👗
Browse files Browse the repository at this point in the history
  • Loading branch information
Sibyx committed Jul 12, 2021
1 parent 3d34fd2 commit d73b3dd
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 387 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.19.0 : 12.07.2021

- **Feature**: `FieldList` and `FormFieldList` now supports optional min/max constrains using `min_length`/`max_length`

## 0.18.0 : 16.04.2021

- **Feature**: `ModelForm` class introduced (experimental, initial support - not recommended for production)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,5 @@ Especially, I have to recommend their database design tool. Many thanks [Navicat
supporting Open Source projects 🌈.

---
Made with ❤️ and ☕️ by Jakub Dubec & [BACKBONE s.r.o.](https://www.backbone.sk/en/)
Made with ❤️ and ☕️ by Jakub Dubec, [BACKBONE s.r.o.](https://www.backbone.sk/en/) &
[contributors](https://github.com/Sibyx/django_api_forms/graphs/contributors).
8 changes: 4 additions & 4 deletions django_api_forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def has_changed(self, initial, data):
class FieldList(Field):
default_error_messages = {
'max_length': _('Ensure this list has at most %(max)d values (it has %(length)d).'),
'min_length': _('Ensure this list has at least %(max)d values (it has %(length)d).'),
'min_length': _('Ensure this list has at least %(min)d values (it has %(length)d).'),
'not_field': _('Invalid Field type passed into FieldList!'),
'not_list': _('This field needs to be a list of objects!'),
}
Expand All @@ -64,7 +64,7 @@ def to_python(self, value) -> typing.List:
raise ValidationError(self.error_messages['not_list'], code='not_list')

if self._min_length is not None and len(value) < self._min_length:
params = {'max': self._min_length, 'length': len(value)}
params = {'min': self._min_length, 'length': len(value)}
raise ValidationError(self.error_messages['min_length'], code='min_length', params=params)

if self._max_length is not None and len(value) > self._max_length:
Expand Down Expand Up @@ -116,7 +116,7 @@ def __init__(self, form: typing.Type, min_length=None, max_length=None, **kwargs

default_error_messages = {
'max_length': _('Ensure this list has at most %(max)d values (it has %(length)d).'),
'min_length': _('Ensure this list has at least %(max)d values (it has %(length)d).'),
'min_length': _('Ensure this list has at least %(min)d values (it has %(length)d).'),
'not_list': _('This field needs to be a list of objects!')
}

Expand All @@ -128,7 +128,7 @@ def to_python(self, value):
raise ValidationError(self.error_messages['not_list'], code='not_list')

if self._min_length is not None and len(value) < self._min_length:
params = {'max': self._min_length, 'length': len(value)}
params = {'min': self._min_length, 'length': len(value)}
raise ValidationError(self.error_messages['min_length'], code='min_length', params=params)

if self._max_length is not None and len(value) > self._max_length:
Expand Down
2 changes: 1 addition & 1 deletion django_api_forms/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.18.0'
__version__ = '0.19.0'
10 changes: 6 additions & 4 deletions docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ check `FormFieldList`.
- Error message keys: `not_field`, `not_list`, `min_length`, `max_length`
- Required arguments:
- `field`: Instance of a form field representing children
- `min_length`: Minimum length of field size in integer (optional)
- `max_length`: Maximum length of field size in integer (optional)
- Optional arguments:
- `min_length`: Minimum length of field size as integer
- `max_length`: Maximum length of field size as integer

**JSON example**

Expand Down Expand Up @@ -135,8 +136,9 @@ Field used for embedded objects represented as another API form.
- Error message keys: `not_list`, `min_length`, `max_length`
- Required arguments:
- `form`: Type of a nested form
- `min_length`: Minimum length of field size in integer (optional)
- `max_length`: Maximum length of field size in integer (optional)
- Optional arguments:
- `min_length`: Minimum length of field size as integer
- `max_length`: Maximum length of field size as integer

**JSON example**

Expand Down
Loading

0 comments on commit d73b3dd

Please sign in to comment.