Package: jsonmix
Json MixA Python library for validate json. In this0.1.0version of jsonmix, it allows you to validate the json that you this awaiting.InstallationThe latest stable versionis available on PyPI. Either addjsonmixto yourrequirements.txtfile or install with pip:pip install jsonmixDescriptionThis project is to help other types of projects that need a json validation, and with this lib, you can validate fields, validate the types of fields or if it is necessary to validate both together.StepsStep 0:In this lib we use the decorator, so for you to use it would be through a function or class, so it would start by importing the lib:import jsonmixTo be more specific and save space in memory, it could also import this way:from jsonmix.validate import validate_jsonexamples of how to use:operation receiving fieldsdef hello():

      receive_json = {

          'uid': 1,

          'Name': 'Json Mix',

          'Version': '0.1.0',

          'message': 'A library for validate in python'

      }

      model_json = {

          'uid': int,

          'Name': str,

          'Version': str,

          'message': str

      }

      response = {

          "code": "JMERR-001",

          "data": {

              "error": "inconsistency on JSON structure",

              "message": "Missing required JSON field"

          },

          "date": str(datetime.now().timestamp()),

      }

      return validate_json(receive=receive_json, model=model_json, response_field=response, operation='fields')operation receiving fields typesdef hello():

          receive_json = {

              'uid': 1,

              'Name': 'Json Mix',

              'Version': '0.1.0',

              'message': 'A library for validate in python'

          }

          model_json = {

              'uid': int,

              'Name': str,

              'Version': str,

              'message': str

          }

          response = {

              "code": "JMERR-002",

              "data": {

                  "error": "inconsistency on JSON structure",

                  "message": "JSON field type is incorrect"

              },

              "date": str(datetime.now().timestamp())

          }

          return validate_json(receive=receive_json, model=model_json, response_field=response, operation='types')operation receiving decoratorreceive_json = {

              'uid': 1,

              'Name': 'Json Mix',

              'Version': '0.1.0',

              'message': 'A library for validate in python'

          }

      model_json = {

          'uid': int,

          'Name': str,

          'Version': str,

          'message': str

      }

      response_fields = {

          "code": "JMERR-001",

          "data": {

              "error": "inconsistency on JSON structure",

              "message": "Missing required JSON field"

          },

          "date": str(datetime.now().timestamp()),

      }

      response_types = {

          "code": "JMERR-002",

          "data": {

              "error": "inconsistency on JSON structure",

              "message": "JSON field type is incorrect"

          },

          "date": str(datetime.now().timestamp())

      }



      @validate_json(receive=receive_json, model=model_json, response_field=response_fields, response_field_types=response_types, operation='decorator')

      def hello():

          return 'Hello Word'operation receiving defaultdef hello():

        receive_json = {

                'uid': 1,

                'Name': 'Json Mix',

                'Version': '0.1.0',

                'message': 'A library for validate in python'

            }

        model_json = {

            'uid': int,

            'Name': str,

            'Version': str,

            'message': str

        }

        response_fields = {

            "code": "JMERR-001",

            "data": {

                "error": "inconsistency on JSON structure",

                "message": "Missing required JSON field"

            },

            "date": str(datetime.now().timestamp()),

        }

        response_types = {

            "code": "JMERR-002",

            "data": {

                "error": "inconsistency on JSON structure",

                "message": "JSON field type is incorrect"

            },

            "date": str(datetime.now().timestamp())

        }

        return validate_json(receive=receive_json, model=model_json, response_field=response_fields, response_field_types=response_types, operation='decorator')Notice that I pass 4 parameters that are them:receive_json: The json you receive from an API or elsewhere.model_json: The model json you created to be the basis of your validationsresponse_json_field: Your error response if the error is in the json keysresponse_json_field_type: Your error response if the error is in the json typesStep 2:We have a test example of each of the parameter passagesreceive_jsonexample_json_receive = {

        'uid': 1,

        'Name': 'Json Mix',

        'Version': '0.1.0',

        'message': 'A library for validate in python'

    }model_jsonexample_json_model = {

        'uid': int,

        'Name': str,

        'Version': str,

        'message': str

    }response_json_fieldexample_json_custom_error_response_field = {

        "code": "JMERR-001",

        "data": {

            "error": "inconsistency on JSON structure",

            "message": "Missing required JSON field"

        },

        "date": str(datetime.now().timestamp())

  }response_json_field_typeexample_json_custom_error_response_field_type = {

        "code": "JMERR-002",

        "data": {

            "error": "inconsistency on JSON structure",

            "message": "JSON field type is incorrect"

        },

        "date": str(datetime.now().timestamp())

    }Now that I have shown you the examples of possible json of each parameter thatvalidate_jsonreceives and I have shown an example of use.Step 3:Let's suppose that you set up your json model, to be able to validate with the json that is coming, and this was your model json:example_json_model = {

        'uid': int,

        'Name': str,

        'Version': str,

        'message': str

    }With that there are some malicious people who want to invade or damage your website, app or some software of your own in some way and start editing your receiving json like changing the names of the keys, changing the types or even adding a new key to your json , see these examples of possible json that a malicious person could pass on to you.example_json_model = {

        'uid': int,

        'Name': str,

        'Version': str,

        'message': str

    }example_json_model = {

        'uid': int,

        'Name': str,

        'Version': str,

        'message': str

    }example_json_model = {

        'uid': int,

        'Name': str,

        'Version': str,

        'message': str

    }Soon you realize that in the first example you receive a json with the wrong type, in this case your key 'uid' you defined as int and is receiving it as a string, in the second example we can see that an extra key was assigned in your json that would be the 'add_field' that is not in your json model, in the third example you receive a json with the invalid key, in which case your key for the json model is 'Name' and what you received is 'Na'. So these are the possible invalid json that can be received.