Per-row validation
You can define custom validation rules for individual records by including a validation object inside the data item. These per-row rules will be applied in addition to any global validationRule defined in the options object.
If a conflict occurs between a global rule and a per-row rule for the same field, the per-row rule takes precedence.
Example
Combining global and per-row validation
In this example, the global rule ensures all emails follow a standard format, while the first record includes an additional regex check for the username.
- Manta data table
- PostgreSQL
const res = await manta.createRecords({
table: "users",
data: [
{
email: "real@example.com",
username: "femi",
validation: {
// Rules specific to this record
username: { required: true, regex: /^[a-z0-9_]{3,16}$/ },
email: { required: true, format: "email" },
},
},
{ email: "not-an-email" },
],
options: {
// This rule applies to all records in the array
validationRule: { email: { format: "email" } }
},
});
const res = await manta.createRecords({
db: 'analytics_pg',
table: "users",
data: [
{
email: "real@example.com",
username: "femi",
validation: {
// Rules specific to this record
username: { required: true, regex: /^[a-z0-9_]{3,16}$/ },
email: { required: true, format: "email" },
},
},
{ email: "not-an-email" },
],
options: {
// This rule applies to all records in the array
validationRule: { email: { format: "email" } }
},
});
Validation results
When per-row validation fails, the API response identifies the specific row that failed using the rowIndex property. This allows you to pinpoint errors in large batches without failing the entire request (if continueOnError is enabled).