Validating Data
Superstruct is designed to let you validate any data, ensuring that it matches a specific schema. In this guide we'll show you some of the possibilities.
Primitive Values
The simplest structs are ones that validate "primitive" values, like strings, numbers or booleans. For example:
In this case, assert
will throw an error if the input data
is not a a string. So on any line after the assertion we're guaranteed to be dealing with a string input.
🤖 Note: Superstruct works well with TypeScript guards and assertions, so after calling
assert
oris
you can access your data in a type-safe way!
But Superstruct has simple structs like these for more than the primitive types. It has support out of the box for many of the common types you might need to validate—dates, functions, regexps, etc.
Here we're ensuring that data
is a valid Date
object.
🤖 Check out the Types reference for all of the possible struct types.
Composed Values
In addition to simple, "flat" values, you can also compose structs into more complex shapes. The most common example of this is object
structs:
This User
struct will ensure that input data is an object with specific shape of properties, and with property values that match structs.
You could also define a struct which represents a list of values that all match a specific type, using the array
factory. For example:
These are only two examples, but Superstruct supports many complex structs—maps, sets, records, tuples, etc.
You can also compose structs together, for cases where you have relationships between pieces of data. For example, a User
and a Team
:
🤖 For modelling recursive structures you can use the
lazy
utility to prevent circular errors.
Optional Values
You can also model optional properties. For example, maybe an email
address isn't strictly required for all your users, you could do:
Wrapping a struct in optional
means that the value can also be undefined
and it will still be considered valid.
So now both of these pieces of data would be valid:
Similarly to optional
, you can use nullable
for properties that can also be null
values. For example:
🤖 Check out the Types reference for all of the possible struct types.
Custom Values
Next up, you might have been wondering about the email
property. So far we've just defined it as a string, which means that any old string will pass validation.
But we'd really like to validate that the email is a valid email address. You can do this by defining a custom validation struct:
Now we can define structs know about the email
type:
Now if you pass in an email string that is invalid, it will throw:
And there you have it!
🤖 Check out the Types reference for all of the possible struct types.
Last updated