Database Driver
Kanun uses a pluggable database driver for exists and unique rules.
Why a Driver?
The validation package stays framework-agnostic and does not hard-couple to a specific ORM/query builder.
Registering a Global Driver
ts
import { IDatabaseDriver, Validator } from 'kanun';
class AppDatabaseDriver extends IDatabaseDriver {
async exists({ table, column, value, ignore }) {
const row = await db.table(table).where(column, value).first();
if (!row) return false;
if (ignore != null && String(row.id) === String(ignore)) return false;
return true;
}
}
const driver = new AppDatabaseDriver();
Validator.useDatabase(driver);Registering Per Validator Instance
ts
const validator = new Validator(data, rules).database(driver);Using with Rules
ts
const validator = new Validator(
{ username: 'legacy', email: 'john@example.com' },
{
username: 'unique:users,username',
email: 'exists:users,email',
},
);How unique Works
Kanun calls driver.exists(input) for both exists and unique rules.
existspasses whenexists(input)returnstrueuniquepasses whenexists(input)returnsfalse
Rule Parameter Format
exists:table,columnexists:connection.table,columnexists:table,column,ignoreunique:table,columnunique:connection.table,columnunique:table,column,ignore
Driver Input Shape
The exists(input) method receives:
tablecolumnvalueignore(optional)connection(optional)attribute(optional)data(optional full request payload)
See Database Driver Contract for types.