murphyslemon / Raspberry_Pi_Server

International Sensor Development Project: This project is a voting system that uses a raspberry pi server and multiple voting devices. The system is easy, secure, scalable, and supports anonymous and registered voting. This repository focuses on the Raspberry Pi server.
0 stars 3 forks source link

validateKeywordsInJSON Function #1

Open MagnusLii opened 7 months ago

MagnusLii commented 7 months ago

Keyword Validation Function

Goal

The goal is to create a function that takes in a JSON object, a list of keywords, and a verification level. The function first checks the verification level and based on the level either verifies that all keywords are present or that all keywords are present and have a non empty or NULL value assigned to them.

Arguments

MagnusLii commented 7 months ago
def validateKeywordsInJSON(decodedJSON, keywordList, verifycationLevel):
    jsonKeySet = set(decodedJSON.keys())

    if verifycationLevel == 1:
        for keyword in keywordList:
            if keyword not in jsonKeySet:
                return False
        return True

    elif verifycationLevel == 2:
        for keyword in keywordList:
            if keyword not in jsonKeySet or not decodedJSON[keyword] or decodedJSON[keyword] == "NULL":
                return False
        return True

    else:
        raise ValueError("Invalid verification level. Please provide either 1 or 2.")