Problem Statement

You want to validate the type or value of the inputs that are going in to your block.


Basic input types like string, number, boolean etc can be specified at the time of declaration as shown below:


# generic accepts any type of input

Type validation will be automatically performed on for these basic types. If you want to accept a complex type or perform validation on the value of the inputs, define input validators and attach them in the load method. A validator is a simple function that returns a validation boolean for every input.

For example, let's create a block that accepts an input which is a series of numbers. The validator checks if the number is an even number or not.

class Adder(Block):
    def numbers_validator(item):
        return item % 2 == 0
    def load(self):
        self.inputs['numbers'].validator = self.numbers_validator
    def run(self, numbers, total):
        t = 0
        for n in numbers:
            t += n
        print('>>> Total is:', t)