Guest User

Untitled

a guest
Jan 22nd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. const { hooks } = require('@adonisjs/ignitor')
  2.  
  3. hooks.after.providersBooted(() => {
  4. const Validator = use('Validator')
  5. const Database = use('Database')
  6.  
  7. const notExistsFn = async (data, field, message, args, get) => {
  8. const value = get(data, field)
  9. if (!value) {
  10. return
  11. }
  12.  
  13. const [table, fieldName, caseSensitivy,ignoreKey, ignoreValue] = args
  14. let query = ''
  15. if (caseSensitivy !== 'true') {
  16. query = Database
  17. .from(table)
  18. .where(fieldName || field, value)
  19. } else {
  20. query = Database
  21. .from(table)
  22. .whereRaw(`LOWER(${fieldName || field}) = ?`, [value.toLowerCase()])
  23. }
  24.  
  25. if (ignoreKey && ignoreValue) {
  26. query.whereNot(ignoreKey, ignoreValue)
  27. }
  28.  
  29. const row = await query.first()
  30. if (typeof row !== 'undefined') {
  31. throw message
  32. }
  33. }
  34.  
  35. Validator.extend('notExists', notExistsFn)
  36. })
Add Comment
Please, Sign In to add comment