Refining Validation
There are some cases where you want to create a validation that is more fine-grained than a "type". For example, you might want not just a string
, but a specific format of string. Or not just a User
, but a user that is also an administrator.
For these situations, you can use "refinements". Refinements allow you to create a new struct that is derived from an existing struct with an extra bit of validation layered on top.
Built-in Refinements
Superstruct has several built-in refinements for common use cases. For example, a common one is ensuring that a string matches a specific regular expression pattern:
Or maybe that a string (or array, number, etc.) has a specific size:
Another common use case is validating nonnegative integers (like indexes in an array) using the built-in min
helper:
These refinements don't change the inferred type of the data, but they do ensure that a slightly stricter validation is enforced at runtime.
Custom Refinements
You can also write your own custom refinements for more domain-specific use cases. For example, for a specific kind of string:
Now the MyString
will only validate strings that begin with "The" and are longer than 20 characters.
And whenever an error is thrown from the refinements, the error.refinement
property will tell you which refinement was the cause.
Last updated