Skip to content

Commit

Permalink
Fix Release Issues & Linting (#24)
Browse files Browse the repository at this point in the history
* Remove breakpoint

* Manually fix pre-commit issues

* Added pre-commit hooks for ast and debug statements
  • Loading branch information
qoda authored Oct 26, 2021
1 parent 02ec0b5 commit f24535a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
8 changes: 7 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ repos:
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
- id: flake8

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: check-ast
- id: debug-statements
13 changes: 5 additions & 8 deletions drf_react_template/schema_form_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
]


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 5 additions & 7 deletions tests/test_schema_form_encoder.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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'
Expand All @@ -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()
Expand All @@ -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.'}
]

0 comments on commit f24535a

Please sign in to comment.