-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom error for strict mode (#95)
* Allow custom error class for strict mode * Cover custom error class with tests * Make linter happy * Bump minor * Move validation error class decision
- Loading branch information
1 parent
6de520a
commit d5e88f2
Showing
9 changed files
with
184 additions
and
51 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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class DefautValidationError extends Error { | ||
constructor(errors) { | ||
super('Invalid Attributes'); | ||
this.details = errors; | ||
} | ||
} | ||
|
||
module.exports = DefautValidationError; |
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 |
---|---|---|
@@ -1,14 +1,8 @@ | ||
function invalidAttributes(errors){ | ||
let error = new Error('Invalid Attributes'); | ||
error.details = errors; | ||
return error; | ||
} | ||
|
||
module.exports = { | ||
classAsSecondParam: (ErroneousPassedClass) => new Error(`You passed the structure class as the second parameter of attributes(). The expected usage is \`attributes(schema)(${ ErroneousPassedClass.name || 'StructureClass' })\`.`), | ||
nonObjectAttributes: () => new TypeError('#attributes can\'t be set to a non-object.'), | ||
arrayOrIterable: () => new TypeError('Value must be iterable or array-like.'), | ||
missingDynamicType: (attributeName) => new Error(`Missing dynamic type for attribute: ${ attributeName }.`), | ||
invalidType: (attributeName) => new TypeError(`Attribute type must be a constructor or the name of a dynamic type: ${ attributeName }.`), | ||
invalidAttributes | ||
invalidAttributes: (errors, StructureValidationError) => new StructureValidationError(errors) | ||
}; |
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,20 @@ | ||
const Errors = require('../errors'); | ||
const DefaultValidationError = require('../errors/DefaultValidationError'); | ||
|
||
exports.buildStrictDescriptorFor = function buildStrictDescriptorFor(StructureClass, schemaOptions) { | ||
const StructureValidationError = schemaOptions.strictValidationErrorClass || DefaultValidationError; | ||
|
||
return { | ||
value: function buildStrict(constructorArgs) { | ||
const instance = new StructureClass(constructorArgs); | ||
|
||
const { valid, errors } = instance.validate(); | ||
|
||
if (!valid) { | ||
throw Errors.invalidAttributes(errors, StructureValidationError); | ||
} | ||
|
||
return instance; | ||
} | ||
}; | ||
}; |
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