Skip to main content

Validation logic for TerminalOne models used for api request models

Project description

testv

Python library for pre-validation of models used for terminalOne API based on jsonschema validation and combination of rules. Implemented validators and rules include different type, length and business logic validations.

Prerequirement. Install Python 3

MacOS (using brew)

brew install python3

Installation

Installation is simple with pip in a virtual environment:

$ pip install ts-t1-validator

Execution

General usage

To use some specific validator for particular terminalOne object call approariate validator and provide dictionary of the object you'd like to validate and t1 service if needed:

>>> from ts_t1_validator import T1Service, CampaignPostValidator

>>> t1_service = T1Service({"token": token, "host": host})
>>> validator = CampaignPostValidator(dto=campaign.__dict__, t1_service=t1_service)
>>> errors_list = validator.validate()

The result of the validation method will be a list of errors. If there are no errors, your model is valid. T1Service is used for validations that are performed with the help of T1 api, so it requires valid oauth access token and host that is used in api code from which this validators were called.

Custom validator

To create custom validator use inheritance from AbstractValidator and init pack of validation rules (self.rules) from ts_t1_validator.validators.rules:

import os
from typing import Dict

from ts_t1_validator import AbstractValidator, T1Service
from ts_t1_validator.validators.rules.t1_advertiser import T1AdvertiserRule


class CustomValidator(AbstractValidator):
    def __init__(self, dto: Dict, t1_service: T1Service):
        self.rules = list()
        self.errors = list()
        self.json_schema = os.path.dirname(os.path.abspath(__file__)) + "/path_to_file/file.json"
        self.dto = dto
        self.t1_service = t1_service

        # init object parameters
        advertiser_id = dto.get('param_name')

        self.rules.append(T1AdvertiserRule(
            advertiser_id=advertiser_id,
            t1_service=self.t1_service))

AbstractValidator.validate() uses jsonschema validation for json from self.json_schema that can be created according to JSONSchema standard. Or you can use the following example with general jsonschema validations:

{
  "title": "exampleJsonSchema",
  "description": "description about this schema",
  "properties": {
    "advertiser_id": {
      "type": "integer",
      "minimum": 1
    },
    "campaign_name": {
      "type": "string",
      "maxLength": 256,
      "minLength": 2,
      "example": "Campaign 1"
    },
    "status": {
      "type": "boolean"
    },
    "some_enum": {
      "type": "string",
      "enum": [
        "val1",
        "val2"
      ]
    }
  },
  "required": ["advertiser_id",
    "campaign_name",
    "status"
  ]
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ts-t1-validator-0.1.0.tar.gz (25.0 kB view hashes)

Uploaded Source

Built Distribution

ts_t1_validator-0.1.0-py3-none-any.whl (46.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page