Skip to main content

Validating with external data

info

For more detail about row hooks, see the Reference page

Although it's not required to use async/await with hooks, we recommend you use it with an external API call.

export const fields = [
{
label: 'Email',
key: 'email'
}
];

...

let hasFetchedData = false;
let serverResults;

dromo.registerRowHook(async function (record) {
const newRecord = record;
if (!hasFetchedData) {
await fetch('https://my-json-server.typicode.com/dromoio/docs-email-api/leads', {
method: 'GET'
})
.then((response) => response.json())
.then(function (json) {
serverResults = json.map((record) => {
return record['email'];
});
hasFetchedData = true;
});
}
if (serverResults.includes(newRecord.row['email'].value)) {
newRecord.row['email'].info = [
{
message: 'Duplicate record. This user is already in the database.',
level: 'error'
}
];
}
return newRecord;
});