Skip to content

Commit

Permalink
Merge 343637c into 6cd666d
Browse files Browse the repository at this point in the history
  • Loading branch information
Xstoudi committed May 25, 2019
2 parents 6cd666d + 343637c commit 4688014
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/Validator/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict'

/*
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* adonis-lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
Expand Down Expand Up @@ -37,6 +36,8 @@ class ValidatorRules {
* email: 'unique:users' // define table
* email: 'unique:users,user_email' // define table + field
* email: 'unique:users,user_email,id:1' // where id !== 1
* email: 'unique:users,user_email,caseSensitive'
* email: 'unique:users,user_email,id:1,caseSensitive
*
* // Via new rule method
* email: [rule('unique', ['users', 'user_email', 'id', 1])]
Expand All @@ -58,12 +59,21 @@ class ValidatorRules {
/**
* Extracting values of the args array
*/
const [ table, fieldName, ignoreKey, ignoreValue ] = args
const [ table, fieldName, ignoreKey, ignoreValue, caseSensitivity ] = args
let caseSensitivityOnThird
if (args.length === 3) {
caseSensitivityOnThird = ignoreKey
}

/**
* Base query to select where key=value
*/
const query = this.Database.table(table).where(fieldName || field, value)
let query = ''
if (caseSensitivity || caseSensitivityOnThird) {
query = this.Database.table(table).whereRaw(`LOWER(${fieldName || field}) = ?`, [value.toLowerCase()])
} else {
query = this.Database.table(table).where(fieldName || field, value)
}

/**
* If a ignore key and value is defined, then add a whereNot clause
Expand Down
22 changes: 22 additions & 0 deletions test/unit/validation-rules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ test.group('Validator | Unique', (group) => {
assert.equal(result, 'validation passed')
})

test('throw error when row is found with case sensitivity as third argument', async (assert) => {
assert.plan(1)
const validator = new Validator(this.database)
await this.database.table('users').insert({ username: 'foo' })
try {
await validator.unique({ username: 'Foo' }, 'username', message, ['users', 'username', 'caseSensitivity'], _.get)
} catch (error) {
assert.equal(error, 'unique validation failed')
}
})

test('skip validation when data does not have field', async (assert) => {
const validator = new Validator(this.database)
const result = await validator.unique({}, 'email', message, ['users'], _.get)
Expand All @@ -71,4 +82,15 @@ test.group('Validator | Unique', (group) => {
const result = await validator.unique({ username: 'foo' }, 'username', message, args, _.get)
assert.equal(result, 'validation passed')
})

test('throw error on validation when row is not found with case sensitivity and when whereNot key/value pairs are passed', async (assert) => {
assert.plan(1)
const validator = new Validator(this.database)
await this.database.table('users').insert({ username: 'foo' })
try {
await validator.unique({ username: 'Foo' }, 'username', message, ['users', 'username', 'id', 5, 'caseSensitivity'], _.get)
} catch (error) {
assert.equal(error, 'unique validation failed')
}
})
})

0 comments on commit 4688014

Please sign in to comment.