Skip to content

Commit

Permalink
Updated validation of model
Browse files Browse the repository at this point in the history
  • Loading branch information
Locustv2 committed Oct 18, 2018
1 parent 1270f27 commit cfc9e86
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions rebase/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,24 @@ def rules(self) -> Dict[str, List[Validator]]:
def scenarios(self) -> Dict[str, List[str]]:
return {}

def validate(self) -> bool:
def validate(self, attribute = None) -> bool:
is_valid = True
self._errors.clear()
if not attribute:
self._errors.clear()
else:
self._errors.update({attribute: []})

for attr, ruleset in self.rules().items():
if attr not in self.attributes:
if attribute and attr != attribute:
continue
for rule in ruleset:
value = getattr(self, attr, None)
if rule.required or value is not None:
is_valid &= rule.validate(value)
self.add_errors(attr, rule.errors)
if rule.required and value is None:
self.add_errors(attr, [f'`{attr}` is a required field.'])
is_valid = False
continue
is_valid &= rule.validate(value)
self.add_errors(attr, rule.errors)

return is_valid

Expand Down

0 comments on commit cfc9e86

Please sign in to comment.