Skip to content

Commit

Permalink
- Removed extra flag
Browse files Browse the repository at this point in the history
- Updated logic on when to run field validators
- Updated schemas
  • Loading branch information
elipe17 committed Oct 3, 2023
1 parent 6817519 commit 18c987a
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 309 deletions.
7 changes: 3 additions & 4 deletions tdrs-backend/tdpservice/parsers/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ def value_is_empty(value, length):
class Field:
"""Provides a mapping between a field name and its position."""

def __init__(self, item, name, type, startIndex, endIndex, can_be_empty=False, required=True, validators=[]):
def __init__(self, item, name, type, startIndex, endIndex, required=True, validators=[]):
self.item = item
self.name = name
self.type = type
self.startIndex = startIndex
self.endIndex = endIndex
self.required = required
self.validators = validators
self.can_be_empty = can_be_empty

def create(self, item, name, length, start, end, type):
"""Create a new field."""
Expand Down Expand Up @@ -61,9 +60,9 @@ def parse_value(self, line):
class TransformField(Field):
"""Represents a field that requires some transformation before serializing."""

def __init__(self, transform_func, item, name, type, startIndex, endIndex, can_be_empty=False, required=True,
def __init__(self, transform_func, item, name, type, startIndex, endIndex, required=True,
validators=[], **kwargs):
super().__init__(item, name, type, startIndex, endIndex, can_be_empty, required, validators)
super().__init__(item, name, type, startIndex, endIndex, required, validators)
self.transform_func = transform_func
self.kwargs = kwargs

Expand Down
4 changes: 3 additions & 1 deletion tdrs-backend/tdpservice/parsers/row_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def run_field_validators(self, instance, generate_error):
else:
value = getattr(instance, field.name, None)

if (field.required and not value_is_empty(value, field.endIndex-field.startIndex)) or field.can_be_empty:
is_empty = value_is_empty(value, field.endIndex-field.startIndex)
should_validate = not field.required and not is_empty
if (field.required and not is_empty) or should_validate:
for validator in field.validators:
validator_is_valid, validator_error = validator(value)
is_valid = False if not validator_is_valid else is_valid
Expand Down
59 changes: 23 additions & 36 deletions tdrs-backend/tdpservice/parsers/schema_defs/tanf/t1.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@
validators.isNumber(),
]),
Field(item="5", name='STRATUM', type='string', startIndex=22, endIndex=24,
can_be_empty=True, required=True, validators=[
validators.or_validators(validators.isInStringRange(0, 99),
validators.isNone()),
required=False, validators=[
validators.isInStringRange(0, 99),
]),
Field(item="7", name='ZIP_CODE', type='string', startIndex=24, endIndex=29,
required=True, validators=[
Expand Down Expand Up @@ -126,21 +125,19 @@
]),
Field(item="14", name='RECEIVES_MED_ASSISTANCE', type='number', startIndex=36, endIndex=37,
required=True, validators=[
validators.isInLimits(1, 2)
validators.isInLimits(1, 2),
]),
Field(item="15", name='RECEIVES_FOOD_STAMPS', type='number', startIndex=37, endIndex=38,
can_be_empty=True, required=True, validators=[
validators.or_validators(validators.isInLimits(0, 2),
validators.isNone()),
required=False, validators=[
validators.isInLimits(0, 2),
]),
Field(item="16", name='AMT_FOOD_STAMP_ASSISTANCE', type='number', startIndex=38, endIndex=42,
required=True, validators=[
validators.isLargerThanOrEqualTo(0),
]),
Field(item="17", name='RECEIVES_SUB_CC', type='number', startIndex=42, endIndex=43,
can_be_empty=True, required=True, validators=[
validators.or_validators(validators.isInLimits(0, 3),
validators.isNone()),
required=False, validators=[
validators.isInLimits(0, 3),
]),
Field(item="18", name='AMT_SUB_CC', type='number', startIndex=43, endIndex=47,
required=True, validators=[
Expand Down Expand Up @@ -183,24 +180,20 @@
validators.isLargerThanOrEqualTo(0),
]),
Field(item="24A", name='TRANSITION_SERVICES_AMOUNT', type='number', startIndex=78, endIndex=82,
can_be_empty=True, required=True, validators=[
validators.or_validators(validators.isLargerThanOrEqualTo(0),
validators.isNone()),
required=False, validators=[
validators.isLargerThanOrEqualTo(0),
]),
Field(item="24B", name='TRANSITION_NBR_MONTHS', type='number', startIndex=82, endIndex=85,
can_be_empty=True, required=True, validators=[
validators.or_validators(validators.isLargerThanOrEqualTo(0),
validators.isNone()),
required=False, validators=[
validators.isLargerThanOrEqualTo(0),
]),
Field(item="25A", name='OTHER_AMOUNT', type='number', startIndex=85, endIndex=89,
can_be_empty=True, required=True, validators=[
validators.or_validators(validators.isLargerThanOrEqualTo(0),
validators.isNone()),
required=False, validators=[
validators.isLargerThanOrEqualTo(0),
]),
Field(item="25B", name='OTHER_NBR_MONTHS', type='number', startIndex=89, endIndex=92,
can_be_empty=True, required=True, validators=[
validators.or_validators(validators.isLargerThanOrEqualTo(0),
validators.isNone()),
required=False, validators=[
validators.isLargerThanOrEqualTo(0),
]),
Field(item="26AI", name='SANC_REDUCTION_AMT', type='number', startIndex=92, endIndex=96,
required=True, validators=[
Expand All @@ -211,9 +204,8 @@
validators.oneOf([1, 2]),
]),
Field(item="26AIII", name='FAMILY_SANC_ADULT', type='number', startIndex=97, endIndex=98,
can_be_empty=True, required=True, validators=[
validators.or_validators(validators.oneOf([0, 1, 2]),
validators.isNone()),
required=False, validators=[
validators.oneOf([0, 1, 2]),
]),
Field(item="26AIV", name='SANC_TEEN_PARENT', type='number', startIndex=98, endIndex=99,
required=True, validators=[
Expand Down Expand Up @@ -252,26 +244,21 @@
validators.oneOf([1, 2]),
]),
Field(item="27", name='WAIVER_EVAL_CONTROL_GRPS', type='string', startIndex=113, endIndex=114,
can_be_empty=True, required=True, validators=[
validators.or_validators(
validators.and_validators(
validators.or_validators(validators.matches('9'), validators.isEmpty()),
validators.isAlphaNumeric()
),
validators.isNone()),
required=False, validators=[
validators.or_validators(validators.matches('9'), validators.isEmpty()),
validators.isAlphaNumeric(),
]),
Field(item="28", name='FAMILY_EXEMPT_TIME_LIMITS', type='number', startIndex=114, endIndex=116,
required=True, validators=[
validators.oneOf([1, 2, 3, 4,
6, 7, 8, 9])
]),
Field(item="29", name='FAMILY_NEW_CHILD', type='number', startIndex=116, endIndex=117,
can_be_empty=True, required=True, validators=[
validators.or_validators(validators.oneOf([1, 2]),
validators.isNone()),
required=False, validators=[
validators.oneOf([1, 2]),
]),
Field(item="-1", name='BLANK', type='string', startIndex=117, endIndex=156, required=False,
validators=[]),
],
)]
)
)
Loading

0 comments on commit 18c987a

Please sign in to comment.