Input validation
info
The Validation API is available in SDK versions v0.22.0 and higher.
Out of the box, I/O methods handle basic validation for you. For example, io.input.email will ensure the string you receive is a valid email and io.input.number will ensure you receive a valid number.
Custom validation can be performed by chaining a .validate()
method call
to an I/O method or to an io.group.
The validation function that you pass to this method receives the group or
method's potential return value as its argument and must return either a
string error message if the value is invalid, or undefined
if it is valid.
Error messages returned from a validation function will be shown to the user, giving them the opportunity to make changes and retry submission.
tip
Validation functions are regular functions that run alongside the rest of your action logic on your infrastructure — you're able to reference any other code just like you can within actions.
Validation functions can also be async
or return a Promise.
Usage
- TypeScript
- JavaScript
const { name, email, age, includeDrinkTickets } = await io
.group({
name: io.input.text("Name"),
email: io.input.email("Email").validate(email => {
if (!email.endsWith("@interval.com"))
return "Only Interval employees are invited to the holiday party.";
}),
age: io.input.number("Age"),
includeDrinkTickets: io.input.boolean("Include drink tickets?"),
})
.validate(({ age, includeDrinkTickets }) => {
if (age < 21 && includeDrinkTickets)
return "Attendees must be 21 years or older to receive drink tickets.";
});
const { name, email, age, includeDrinkTickets } = await io
.group({
name: io.input.text("Name"),
email: io.input.email("Email").validate(email => {
if (!email.endsWith("@interval.com"))
return "Only Interval employees are invited to the holiday party.";
}),
age: io.input.number("Age"),
includeDrinkTickets: io.input.boolean("Include drink tickets?"),
})
.validate(({ age, includeDrinkTickets }) => {
if (age < 21 && includeDrinkTickets)
return "Attendees must be 21 years or older to receive drink tickets.";
});
In the example above, only people with a matching organization email address are allowed to RSVP to the holiday party, and only attendees 21 years or older are allowed to receive drink tickets.