Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.9 kB
2
Indexable
class ValidateJson():
    def __init__(self, path):
        tmp = tempfile.NamedTemporaryFile(mode="w+")
        with open(path, "r") as file:
            tmp.write(file.read())

        tmp.seek(0)
        self.json = tmp.read()

    def validate_json_file(self):
        try:
            json.loads(self.json)
            return True
        except ValueError:
           return False
        
    def validate_parameters(self):
        j = json.loads(self.json)
        arr = []
        for n in j:
            if "name" in n:
                arr.append(True)
            else:
                arr.append(False)

            if "description" in n:
                arr.append(True)
            else:
                arr.append(False)

            if "Command" in n:
                arr.append(True)
            else:
                arr.append(False)
            
            if "run" in n:
                arr.append(True)
            else:
                arr.append(False)

        return all(element for element in arr)


    def validate_values(self):
        j = json.loads(self.json)
        arr = []
        for n in j:
            if validate_name(n['name']):
                arr.append(True)
            else:
                arr.append(False)

            if len(n["description"]) <= 250:
                arr.append(True)
            else:
                arr.append(False)
            
            if isinstance(n["Command"], list):
                arr.append(True)
            else:
                arr.append(False)

        return all(element for element in arr)
        
      
    def validate(self):
        Thread(target = self.validate_json_file).start()
        Thread(target = self.validate_parameters).start()
        Thread(target = self.validate_values).start()

validator = ValidateJson("./test.json")
validator.validate()