-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from feathersjs/validator-1
Enhanced validate hook
- Loading branch information
Showing
2 changed files
with
25 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,14 @@ import { assert } from 'chai'; | |
import hooks from '../../src/services'; | ||
import errors from 'feathers-errors'; | ||
|
||
var fcnSync; | ||
var fcnPromise; | ||
var fcnPromiseSanitize; | ||
var origHookOk; | ||
var origHookBad; | ||
var hookOk; | ||
var hookBad; | ||
let fcnSync; | ||
let fcnPromise; | ||
let fcnPromiseSanitize; | ||
let origHookOk; | ||
let origHookBad; | ||
let hookOk; | ||
let hookBad; | ||
let fcnHook; | ||
|
||
describe('validate', () => { | ||
origHookOk = { type: 'before', method: 'create', data: { email: ' [email protected] ' } }; | ||
|
@@ -19,16 +20,19 @@ describe('validate', () => { | |
beforeEach(() => { | ||
hookOk = clone(origHookOk); | ||
hookBad = clone(origHookBad); | ||
fcnHook = {}; | ||
|
||
fcnSync = (values) => (values.email.trim() // eslint-disable-line no-unused-expressions | ||
? null | ||
: { email: 'Email is invalid' } | ||
); | ||
fcnSync = (values, hook) => { | ||
fcnHook = hook; | ||
|
||
return values.email.trim() ? null : { email: 'Email is invalid' }; | ||
} | ||
}); | ||
|
||
it('test passes on correct data', () => { | ||
const hook = hooks.validate(fcnSync)(hookOk); | ||
assert.deepEqual(hook, origHookOk); | ||
assert.deepEqual(fcnHook, origHookOk) | ||
}); | ||
|
||
it('test fails on errors', () => { | ||
|
@@ -40,16 +44,19 @@ describe('validate', () => { | |
beforeEach(() => { | ||
hookOk = clone(origHookOk); | ||
hookBad = clone(origHookBad); | ||
|
||
fcnPromise = (values) => ( | ||
new Promise((resolve, reject) => { | ||
fcnHook = {}; | ||
|
||
fcnPromise = (values, hook) => { | ||
fcnHook = hook; | ||
|
||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
values.email.trim() // eslint-disable-line no-unused-expressions | ||
? resolve() | ||
: reject(new errors.BadRequest({ email: 'Email is invalid' })); | ||
}, 100); | ||
}) | ||
); | ||
}); | ||
}; | ||
|
||
fcnPromiseSanitize = (values) => ( | ||
new Promise((resolve, reject) => { | ||
|
@@ -66,6 +73,7 @@ describe('validate', () => { | |
hooks.validate(fcnPromise)(hookOk) | ||
.then(hook => { | ||
assert.deepEqual(hook, origHookOk); | ||
assert.deepEqual(fcnHook, origHookOk); | ||
next(); | ||
}) | ||
.catch(err => next(err)); | ||
|