Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { hooks } = require('@adonisjs/ignitor')
- hooks.after.providersBooted(() => {
- const Validator = use('Validator')
- const Database = use('Database')
- const notExistsFn = async (data, field, message, args, get) => {
- const value = get(data, field)
- if (!value) {
- return
- }
- const [table, fieldName, caseSensitivy,ignoreKey, ignoreValue] = args
- let query = ''
- if (caseSensitivy !== 'true') {
- query = Database
- .from(table)
- .where(fieldName || field, value)
- } else {
- query = Database
- .from(table)
- .whereRaw(`LOWER(${fieldName || field}) = ?`, [value.toLowerCase()])
- }
- if (ignoreKey && ignoreValue) {
- query.whereNot(ignoreKey, ignoreValue)
- }
- const row = await query.first()
- if (typeof row !== 'undefined') {
- throw message
- }
- }
- Validator.extend('notExists', notExistsFn)
- })
Add Comment
Please, Sign In to add comment