Skip to content

Commit

Permalink
Move validation error class decision
Browse files Browse the repository at this point in the history
  • Loading branch information
talyssonoc committed Aug 27, 2019
1 parent 20be616 commit 274b4b2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 65 deletions.
119 changes: 60 additions & 59 deletions dist/structure.js
Expand Up @@ -84,10 +84,10 @@ return /******/ (function(modules) { // webpackBootstrap
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

var Schema = __webpack_require__(4);
var Serialization = __webpack_require__(34);
var Serialization = __webpack_require__(33);
var Validation = __webpack_require__(6);
var Initialization = __webpack_require__(18);
var StrictMode = __webpack_require__(37);
var StrictMode = __webpack_require__(36);
var Errors = __webpack_require__(23);

var _require = __webpack_require__(15),
Expand Down Expand Up @@ -764,7 +764,7 @@ return /******/ (function(modules) { // webpackBootstrap
isString = _require.isString;

var Errors = __webpack_require__(23);
var Coercion = __webpack_require__(25);
var Coercion = __webpack_require__(24);
var Validation = __webpack_require__(6);

function normalizeTypeDescriptor(schemaOptions, typeDescriptor, attributeName) {
Expand Down Expand Up @@ -837,12 +837,10 @@ return /******/ (function(modules) { // webpackBootstrap

/***/ },
/* 23 */
/***/ function(module, exports, __webpack_require__) {
/***/ function(module, exports) {

'use strict';

var ValidationError = __webpack_require__(24);

module.exports = {
classAsSecondParam: function classAsSecondParam(ErroneousPassedClass) {
return new Error('You passed the structure class as the second parameter of attributes(). The expected usage is `attributes(schema)(' + (ErroneousPassedClass.name || 'StructureClass') + ')`.');
Expand All @@ -859,51 +857,22 @@ return /******/ (function(modules) { // webpackBootstrap
invalidType: function invalidType(attributeName) {
return new TypeError('Attribute type must be a constructor or the name of a dynamic type: ' + attributeName + '.');
},
invalidAttributes: function invalidAttributes(errors, CustomValidationError) {
return new (CustomValidationError || ValidationError)(errors);
invalidAttributes: function invalidAttributes(errors, StructureValidationError) {
return new StructureValidationError(errors);
}
};

/***/ },
/* 24 */
/***/ function(module, exports) {

'use strict';

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var ValidationError = function (_Error) {
_inherits(ValidationError, _Error);

function ValidationError(errors) {
_classCallCheck(this, ValidationError);

var _this = _possibleConstructorReturn(this, (ValidationError.__proto__ || Object.getPrototypeOf(ValidationError)).call(this, 'Invalid Attributes'));

_this.details = errors;
return _this;
}

return ValidationError;
}(Error);

module.exports = ValidationError;

/***/ },
/* 25 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

var arrayCoercionFor = __webpack_require__(26);
var genericCoercionFor = __webpack_require__(28);
var Coercion = __webpack_require__(29);
var arrayCoercionFor = __webpack_require__(25);
var genericCoercionFor = __webpack_require__(27);
var Coercion = __webpack_require__(28);

var types = [__webpack_require__(30), __webpack_require__(31), __webpack_require__(32), __webpack_require__(33)];
var types = [__webpack_require__(29), __webpack_require__(30), __webpack_require__(31), __webpack_require__(32)];

exports.for = function coercionFor(typeDescriptor, itemTypeDescriptor) {
if (itemTypeDescriptor) {
Expand All @@ -928,15 +897,15 @@ return /******/ (function(modules) { // webpackBootstrap
}

/***/ },
/* 26 */
/* 25 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var Errors = __webpack_require__(23);
var getType = __webpack_require__(27);
var getType = __webpack_require__(26);

module.exports = function arrayCoercionFor(typeDescriptor, itemTypeDescriptor) {
return function coerceArray(rawValue) {
Expand Down Expand Up @@ -990,7 +959,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

/***/ },
/* 27 */
/* 26 */
/***/ function(module, exports) {

"use strict";
Expand All @@ -1004,12 +973,12 @@ return /******/ (function(modules) { // webpackBootstrap
};

/***/ },
/* 28 */
/* 27 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

var getType = __webpack_require__(27);
var getType = __webpack_require__(26);

module.exports = {
isCoerced: function isCoerced(value, typeDescriptor) {
Expand All @@ -1023,7 +992,7 @@ return /******/ (function(modules) { // webpackBootstrap
};

/***/ },
/* 29 */
/* 28 */
/***/ function(module, exports, __webpack_require__) {

'use strict';
Expand Down Expand Up @@ -1061,7 +1030,7 @@ return /******/ (function(modules) { // webpackBootstrap
}

/***/ },
/* 30 */
/* 29 */
/***/ function(module, exports, __webpack_require__) {

'use strict';
Expand All @@ -1079,7 +1048,7 @@ return /******/ (function(modules) { // webpackBootstrap
};

/***/ },
/* 31 */
/* 30 */
/***/ function(module, exports, __webpack_require__) {

'use strict';
Expand All @@ -1097,7 +1066,7 @@ return /******/ (function(modules) { // webpackBootstrap
};

/***/ },
/* 32 */
/* 31 */
/***/ function(module, exports, __webpack_require__) {

'use strict';
Expand All @@ -1115,7 +1084,7 @@ return /******/ (function(modules) { // webpackBootstrap
};

/***/ },
/* 33 */
/* 32 */
/***/ function(module, exports, __webpack_require__) {

'use strict';
Expand All @@ -1135,22 +1104,22 @@ return /******/ (function(modules) { // webpackBootstrap
};

/***/ },
/* 34 */
/* 33 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

module.exports = {
descriptor: __webpack_require__(35)
descriptor: __webpack_require__(34)
};

/***/ },
/* 35 */
/* 34 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

var serialize = __webpack_require__(36);
var serialize = __webpack_require__(35);

module.exports = {
value: function toJSON() {
Expand All @@ -1159,15 +1128,15 @@ return /******/ (function(modules) { // webpackBootstrap
};

/***/ },
/* 36 */
/* 35 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

var _require = __webpack_require__(15),
SCHEMA = _require.SCHEMA;

var getType = __webpack_require__(27);
var getType = __webpack_require__(26);

function serialize(structure) {
if (structure == null) {
Expand Down Expand Up @@ -1228,14 +1197,17 @@ return /******/ (function(modules) { // webpackBootstrap
module.exports = serialize;

/***/ },
/* 37 */
/* 36 */
/***/ function(module, exports, __webpack_require__) {

'use strict';

var Errors = __webpack_require__(23);
var DefaultValidationError = __webpack_require__(37);

exports.buildStrictDescriptorFor = function buildStrictDescriptorFor(StructureClass, schemaOptions) {
var StructureValidationError = schemaOptions.strictValidationErrorClass || DefaultValidationError;

return {
value: function buildStrict(constructorArgs) {
var instance = new StructureClass(constructorArgs);
Expand All @@ -1245,14 +1217,43 @@ return /******/ (function(modules) { // webpackBootstrap
errors = _instance$validate.errors;

if (!valid) {
throw Errors.invalidAttributes(errors, schemaOptions.strictValidationErrorClass);
throw Errors.invalidAttributes(errors, StructureValidationError);
}

return instance;
}
};
};

/***/ },
/* 37 */
/***/ function(module, exports) {

'use strict';

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var DefautValidationError = function (_Error) {
_inherits(DefautValidationError, _Error);

function DefautValidationError(errors) {
_classCallCheck(this, DefautValidationError);

var _this = _possibleConstructorReturn(this, (DefautValidationError.__proto__ || Object.getPrototypeOf(DefautValidationError)).call(this, 'Invalid Attributes'));

_this.details = errors;
return _this;
}

return DefautValidationError;
}(Error);

module.exports = DefautValidationError;

/***/ },
/* 38 */
/***/ function(module, exports, __webpack_require__) {
Expand Down
@@ -1,8 +1,8 @@
class ValidationError extends Error {
class DefautValidationError extends Error {
constructor(errors) {
super('Invalid Attributes');
this.details = errors;
}
}

module.exports = ValidationError;
module.exports = DefautValidationError;
4 changes: 1 addition & 3 deletions src/errors/index.js
@@ -1,10 +1,8 @@
const ValidationError = require('./ValidationError');

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: (errors, CustomValidationError) => new (CustomValidationError || ValidationError)(errors)
invalidAttributes: (errors, StructureValidationError) => new StructureValidationError(errors)
};
5 changes: 4 additions & 1 deletion src/strictMode/index.js
@@ -1,14 +1,17 @@
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, schemaOptions.strictValidationErrorClass);
throw Errors.invalidAttributes(errors, StructureValidationError);
}

return instance;
Expand Down

0 comments on commit 274b4b2

Please sign in to comment.