Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
django-rest-form-fields
Advanced tools
Extended form fields to validate REST-request data via django.forms.
JsonField
and its child classesTimezoneField
Install via pip:
pip install django-rest-form-fields
or via setup.py:
python setup.py install
You can use standard django forms, adding new fields for them.
All *args
and **kwargs
fields in reference below will be passed to base django constructors as is.
Example:
from django import forms
from django_rest_form_fields import FileField, ArrayField
class MyForm(forms.Form):
file_field = FileField(max_size=1024, valid_extensions=['valid_extensions'])
array_field = ArrayField(min_items=1, max_items=10)
Since version 1.2.2 this library also contains base form classes. Together with library fields, they give ability to change field's source attribute. To have this feature, just inherit your class from BaseForm or BaseModelForm:
from django_rest_form_fields.forms import BaseForm
from django_rest_form_fields.fields import RestIntegerField
class MyForm(BaseForm):
int_field = RestIntegerField(source='intField')
f = MyForm({'intField': 123})
f.full_clean()
print(f.cleaned_data['int_field'])
# Outputs: 123
Wraps django.forms.forms.CharField:
Parameters:
Resulting value: Optional[str]
RestCharField child class, that automatically validates given string with regex (re.match function)
If regex parameter is specified and value matches expression, you can get MatchObject using field match
attribute
Parameters:
re.compile()
objectResulting value: Optional[str]
Wraps django.forms.forms.ChoiceField:
Parameters:
Resulting value: Optional[str]
Wrap django.forms.forms.IntegerField and django.forms.forms.FloatField, fixing initial value (Base fields returns None, ignoring 'initial' parameter and None value)
Parameters:
Resulting value: Optional[int/float]
Child of RestIntegerField, validating value as positive integer.
Parameters:
Resulting value: Optional[int]
Child of PositiveIntegerField, validating integer id value.
Upper value can be limited globally with ID_FIELD_MAX_VALUE
in django.conf.settings
.
Resulting value: Optional[int]
Child of RestFloatField. Gets timestamp value and converts it into datetime.datetime
object in UTC.
Parameter initial
can be float or datetime.datetime
value.
Parameters:
django.utils.timezone.now()
Resulting value: Optional[datetime.datetime]
Child of RestCharField. Parses datetime string to datetime.datetime
value.
Parameters:
Resulting value: Optional[datetime.datetime]
Child of DateTimeField. Parses month string to datetime.date
value.
Returns date of the first day of the month.
Parameters:
Resulting value: Optional[datetime.date]
Child of RestCharField. Validates string as one of pytz timezone names.
Parameters:
Resulting value: Optional[str]
Child of RestChoiceField, validating value as one of [hour, day, week]
Parameters:
Resulting value: Optional[str]
Standard django.forms.forms.BooleanField
is based on /django/forms/widgets.py CheckboxInput.value_from_datadict(value)
It works improperly for REST model: required=True
+ value=False
=> ValidationError
This filed fixes this issue, giving opportunity to send False (required or not):
Parameters:
Resulting value: Optional[str]
Wraps django.forms.forms.EmailField
:
Parameters:
Resulting value: Optional[str]
Child of RestCharField, validating color. Color should be six hexadecimal characters.
Parameters:
Resulting value: Optional[str]
Child of RestCharField, which truncates given value, leaving first truncate_length characters.
Parameters:
Resulting value: Optional[str]
Child of RestCharField. Validates, that value is dict, list or JSON-encoded string. If string - decodes it.
Parameters:
Resulting value: Optional[Any]
JsonField child. Validates array. It can be represented in 3 forms:
Parameters:
Resulting value: Optional[List[Any]]
ArrayField child. Validates array of IdField().
Each array element is cleaned with IdField().
Parameters:
Resulting value: Optional[List[int]]
IdArrayField child. Validates set of IdField().
Each element is cleaned with IdField(). Removes duplicated ids from input, if needed.
Parameters:
Resulting value: Optional[Set[int]]
RegexField child. Validates string as URL with django.core.validators.URLValidator
Parameters:
Resulting value: Optional[str]
RestCharField child. Validates that string has hexadecimal characters only.
Parameters:
Resulting value: Optional[str]
RegexField child. Validates field to be correct UUID.
Parameters:
Resulting value: Optional[str]
Wraps django.forms.forms.FileField:
Parameters:
Resulting value: Optional[file]
docker build . --tag django-rest-form-fields
in project directorydocker-compose run run_tests
in project directorypip3 install -U -r requirements-test.txt
python3 runtests.py
FAQs
Extended form fields to validate REST-request data via django.forms
We found that django-rest-form-fields demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.