Skip to content

Commit

Permalink
Version 1.1.10
Browse files Browse the repository at this point in the history
Merge pull request #20 from Djcharles26/work
  • Loading branch information
Djcharles26 authored Aug 19, 2021
2 parents 5206af7 + 98b4433 commit f4fd018
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
## Minor updates
- Added complex_model for more unit testings
- Corrected validation methods
- Added complex test integration
- Updated type and required validations inside mongo documents
97 changes: 59 additions & 38 deletions pymongoose/mongo_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,12 @@ def type_validator(self, type, value) -> bool:
return True if type(value) is datetime.datetime else False
else: return True

def _item_type_check(self, key, type, item_type):
def _item_type_check(self, key, type, item_type):
if item_type == None or item_type == dict:
if methods.debug_log:
Logger.printLog(f"key {key} doesn't come in json, returning True")
return True

if type == Types.Number:
if item_type is int or item_type is float:
return True
Expand Down Expand Up @@ -226,38 +231,48 @@ def _item_type_check(self, key, type, item_type):
def _validate_type_cycle(self, scAux, json_obj, last_key):

if "type" in scAux:
retval = self._item_type_check(last_key, scAux["type"], type(json_obj))
tp = type(json_obj) if json_obj is not None else None
retval = self._item_type_check(last_key, scAux["type"], tp)

if methods.debug_log:
if retval:
Logger.printSuccess(f"(type) key={last_key} in schema has a correct value type")
else:
Logger.printError(f"(type) key={last_key} in schema has an incorrect value type")
return retval


retval = True
for k in scAux:
retval = True
if "type" in scAux[k]:
tp = type(json_obj[k])
retval = self._item_type_check(k, scAux[k]["type"], tp)
if type(scAux[k]) is dict or type(scAux[k]) is list:
if "type" in scAux[k]:
tp = type(json_obj[k]) if k in json_obj and json_obj[k] is not None else None
retval = self._item_type_check(k, scAux[k]["type"], tp)

elif type(scAux[k]) is list or dict in list(map(type, scAux[k].values())):
retval = self.validate_type(
json_obj[k] if k in json_obj and json_obj[k] is not None else
[{}] if type(scAux[k]) is list else {},
scAux[k],
k
)
else:
if methods.debug_log:
Logger.printWarn(f"(type) key={k} in schema doesn't contain any type, returning True")
retval = True

if not retval:
if methods.debug_log:
Logger.printError(f"(type) key={k} in schema has an incorrect value type")
return retval

elif type(scAux[k]) is list or dict in list(map(type, scAux[k].values())):
retval = self.validate_type(json_obj[k], scAux[k], last_key=k)
else:
if methods.debug_log:
Logger.printWarn(f"(type) key={k} in schema doesn't contain any type, returning True")
retval = True

if not retval:
Logger.printSuccess(f"(type) key={k} in schema has a correct value type")
else:
if methods.debug_log:
Logger.printError(f"(type) key={k} in schema has an incorrect value type")
return retval
Logger.printWarn(f"(type) key={k} schema is not a dict, returning True")

if methods.debug_log:
Logger.printSuccess(f"(type) key={k} in schema has a correct value type")

return True
return retval

def validate_type(self, json_obj: dict, sc=schema, last_key = None) -> bool:
scAux = sc
Expand All @@ -281,36 +296,42 @@ def validate_type(self, json_obj: dict, sc=schema, last_key = None) -> bool:
retval = self._validate_type_cycle(scAux, json_obj, last_key)

return retval




def _validate_required_cycle(self, scAux, json_obj) -> bool:
if "required" in scAux:
return json_obj is not None

for k in scAux:
retval = True
if "required" in scAux[k]:
if not k in json_obj or json_obj[k] is None:
return False
if type(scAux[k]) is dict or type(scAux[k]) is list:
if "required" in scAux[k]:
if not k in json_obj or json_obj[k] is None:
return False
else:
retval = True
elif type(scAux[k]) is list or dict in list(map(type, scAux[k].values())):
retval = self.validate_required(
json_obj[k] if k in json_obj and json_obj[k] is not None else
[{}] if type(scAux[k]) is list else {},
scAux[k],
k
)
else:
if methods.debug_log:
Logger.printLog(f"(req) key={k} doesn't contain required value, setting by default in False")
retval = True
elif type(scAux[k]) is list or dict in list(map(type, scAux[k].values())):
retval = self.validate_required(json_obj[k], scAux[k], k)
else:
if methods.debug_log:
Logger.printLog(f"(req) key={k} doesn't contain required value, setting by default in False")
retval = True

if retval:
if methods.debug_log:
Logger.printSuccess(f"(req) key '{k}' has a valid argument")
if retval:
if methods.debug_log:
Logger.printSuccess(f"(req) key '{k}' has a valid argument")

if not retval:
if not retval:
if methods.debug_log:
Logger.printError(f"(req) key '{k}' is required but value doesn't come")
return retval
else:
if methods.debug_log:
Logger.printError(f"(req) key '{k}'' is required but values doesn't come")
return retval
Logger.printLog(f"(req) key={k} current schema isn't a dict, setting by default in False")

return retval

Expand Down Expand Up @@ -344,7 +365,7 @@ def validate_required(self, json_obj: dict, sc=schema, last_key="") -> bool:
retval = self._validate_required_cycle(scAux, json_obj)

return retval

def get_default_value(self, name, kwargs):
"""
Wrapper function for parse_schema_value
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = pymongoose
version = 1.1.9
version = 1.1.10
author = Juan Carlos Lara
author_email = jlaraanaya@gmail.com
description = A pymongo helper with methods and classes
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.9
1.1.10

0 comments on commit f4fd018

Please sign in to comment.