There is a library for doing that in schema: https://github.com/plumatic/schema-generators
The only problem is that we need a "smart" way for generating refined types based on predicates: simple filtering out "wrong" values might be super inefficient, but I don't see any other approach to cover all use cases.