-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
163 additions
and
9 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
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 |
---|---|---|
|
@@ -31,7 +31,8 @@ | |
], | ||
"author": "John Szwaronek <[email protected]>", | ||
"contributors": [ | ||
"Luis A. Salas <[email protected]>" | ||
"Luis A. Salas <[email protected]>", | ||
"Marshall Thompson <[email protected]>" | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
|
@@ -42,7 +43,8 @@ | |
"@feathersjs/errors": "^4.5.1", | ||
"@hapi/joi": "^17.1.0", | ||
"feathers-hooks-common": "^5.0.2", | ||
"joi-errors-for-forms": "^0.2.2" | ||
"joi-errors-for-forms": "^0.2.2", | ||
"lodash": "^4.17.15" | ||
}, | ||
"devDependencies": { | ||
"babel-eslint": "^10.0.3", | ||
|
@@ -55,7 +57,7 @@ | |
"eslint-plugin-react": "7.18.3", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^7.0.1", | ||
"mongodb": "^3.5.3" | ||
"mongodb": "^3.5.5" | ||
}, | ||
"engines": { | ||
"node": ">4.2.4" | ||
|
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const assert = require('assert'); | ||
const Joi = require('@hapi/joi'); | ||
const { validateProvidedData: setupValidate } = require('../index'); | ||
|
||
describe('validate-provided-data hook', () => { | ||
it('throws an error if no valdationAttrs are provided', async () => { | ||
try { | ||
setupValidate(); | ||
} catch (error) { | ||
assert.equal(error.message, 'The `validationsObj` argument is required.'); | ||
} | ||
}); | ||
|
||
it('throws an error if used as an after hook', async () => { | ||
const attrs = { | ||
name: Joi.string().required(), | ||
email: Joi.string().required(), | ||
}; | ||
const context = { | ||
type: 'after', | ||
}; | ||
|
||
try { | ||
const validate = setupValidate(attrs); | ||
const responseContext = await validate(context); | ||
assert(!responseContext, 'should have failed when used as an after hook'); | ||
} catch (error) { | ||
assert.equal(error.message, 'validateProvidedData can only be a before hook', | ||
'should have been able to validate'); | ||
} | ||
}); | ||
|
||
it('validates only the attributes in context.data', async () => { | ||
const attrs = { | ||
name: Joi.string().required(), | ||
email: Joi.string().required(), | ||
}; | ||
const context = { | ||
type: 'before', | ||
data: { | ||
name: 'Marshall', | ||
}, | ||
}; | ||
|
||
try { | ||
const validate = setupValidate(attrs); | ||
const responseContext = await validate(context); | ||
assert(responseContext); | ||
} catch (error) { | ||
assert(!error, 'should have been able to validate'); | ||
} | ||
}); | ||
|
||
it('can fail if validations do not match', async () => { | ||
const attrs = { | ||
name: Joi.string().required(), | ||
email: Joi.string().required(), | ||
}; | ||
const context = { | ||
type: 'before', | ||
data: { | ||
name: 'Marshall', | ||
email: 200, // this should fail | ||
}, | ||
}; | ||
|
||
try { | ||
const validate = setupValidate(attrs); | ||
const options = { convert: true, abortEarly: false, stripUnknown: true }; | ||
const responseContext = await validate(context, options); | ||
assert(!responseContext, 'should have failed email validation'); | ||
} catch ({ errors }) { | ||
assert.equal(errors.email, '"email" must be a string', 'validation should have failed for bad email'); | ||
} | ||
}); | ||
}); |
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