diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8fcc03c..da00ee2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,4 +15,10 @@ repos: - repo: https://gitlab.com/pycqa/flake8 rev: 3.8.4 hooks: - - id: flake8 \ No newline at end of file + - id: flake8 + +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: check-ast + - id: debug-statements diff --git a/drf_react_template/schema_form_encoder.py b/drf_react_template/schema_form_encoder.py index 754c429..f8f3834 100644 --- a/drf_react_template/schema_form_encoder.py +++ b/drf_react_template/schema_form_encoder.py @@ -2,8 +2,8 @@ from typing import Any, Dict, List, Tuple, Union from django.conf import settings -from django.core.serializers.json import DjangoJSONEncoder from django.core import validators +from django.core.serializers.json import DjangoJSONEncoder from rest_framework import fields, serializers from rest_framework import validators as drf_validators @@ -44,12 +44,12 @@ validators.MinLengthValidator: ['minLength', lambda v: v.limit_value], validators.MaxValueValidator: ['maximum', lambda v: v.limit_value], validators.MinValueValidator: ['minimum', lambda v: v.limit_value], - validators.RegexValidator: ['pattern', lambda v: v.regex.pattern] + validators.RegexValidator: ['pattern', lambda v: v.regex.pattern], } EXCLUDED_VALIDATOR_CLASSES = [ validators.ProhibitNullCharactersValidator, - drf_validators.ProhibitSurrogateCharactersValidator + drf_validators.ProhibitSurrogateCharactersValidator, ] @@ -181,7 +181,6 @@ def _set_validation_properties( return result - def _get_field_properties(self, field: SerializerType, name: str) -> Dict[str, Any]: result = {} type_map_obj = self._get_type_map_value(field) @@ -410,13 +409,11 @@ def _set_validation_properties( EXCLUDED_VALIDATOR_CLASSES + list(VALIDATION_MAP.keys()) ) custom_validators = [ - v for v in field.validators - if not isinstance(v, excluded_validators) + v for v in field.validators if not isinstance(v, excluded_validators) ] if custom_validators: result['ui:custom-validators'] = [ - {'code': v.code, 'message': v.message} - for v in custom_validators + {'code': v.code, 'message': v.message} for v in custom_validators ] return result diff --git a/tests/test_schema_form_encoder.py b/tests/test_schema_form_encoder.py index fe541d8..7f6bfb6 100644 --- a/tests/test_schema_form_encoder.py +++ b/tests/test_schema_form_encoder.py @@ -1,5 +1,5 @@ -from django.utils.translation import gettext_lazy as _ import pytest +from django.utils.translation import gettext_lazy as _ from rest_framework import serializers from drf_react_template.schema_form_encoder import ( @@ -406,7 +406,6 @@ class CustomFieldTypeSerializer(ChoiceSerializer): def test_validation_schema(): - class MinSizeImageValidator: message = _('Image is too small, must be 1KB minimum.') code = 'image_min_1KB' @@ -425,7 +424,9 @@ class CustomValidationSerializer(ChoiceSerializer): ) list_field = serializers.ListField( child=serializers.IntegerField(min_value=0, max_value=100), - min_length=1, max_length=5, allow_empty=True + min_length=1, + max_length=5, + allow_empty=True, ) result = SchemaProcessor(CustomValidationSerializer(), {}).get_schema() @@ -437,14 +438,11 @@ class CustomValidationSerializer(ChoiceSerializer): assert result['properties']['int_field']['minimum'] == 3 assert result['properties']['list_field']['maxLength'] == 5 assert result['properties']['list_field']['minLength'] == 1 - assert result['properties']['list_field']['required'] == False + assert result['properties']['list_field']['required'] is False ui_result = UiSchemaProcessor(CustomValidationSerializer(), {}).get_ui_schema() - breakpoint() - assert 'ui:custom-validators' not in ui_result['char_text'] assert ui_result['image_field']['ui:custom-validators'] == [ {'code': 'image_min_1KB', 'message': 'Image is too small, must be 1KB minimum.'} ] -