Pipelines and Blocks - Block Input Validation | Razor Think
Accessing and Manipulating Data

Pipelines and Blocks – Block Input Validation

By December 11, 2020 No Comments

Pipelines and Blocks – Block Input Validation

More articles in Accessing and Manipulating Data

  • @inputs.atomic.string(...) @inputs.atomi.number(...) @inputs.atomic.boolean(...) # generic accepts any type of input @inputs.atomic.generic(...)

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.

  • @inputs.series.generic('numbers') @outputs.atomic.generic('total') class Adder(Block): @staticmethod 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) total.put(t)

Leave a Reply