Define a complex rule
This page explains how to create a complex rule by combining multiple rules.
Overview
The most important thing when creating complex rules is to break them down into minimal rules. By decomposing rules, their implementation becomes very simple and testing becomes easier. Furthermore, these decomposed rules can be reused in other situations.
Get Started
Let’s create a type that represents a number that either:
- Falls within the range of 0–50, is even, and is NOT 10 or 20
- Or falls within the range of 60–100, is odd, and is NOT 75 or 85
At first, define the 1st rule.
Next, define the 2nd rule.
Now, combine these two rules using the Or
rule composer.
Let’s see how this rule works in practice.
Even for complex cases, you can easily create a type! Naturally, if a value is held by this type, it guarantees that the complex rules specified earlier are satisfied. This means there’s no need for redundant validation on this type in subsequent processing. Be sure to use refined_type to write type-safe and runtime-safe programs!