Skip to content

Commit

Permalink
Merge 0a8702a into d5e88f2
Browse files Browse the repository at this point in the history
  • Loading branch information
talyssonoc committed Aug 27, 2019
2 parents d5e88f2 + 0a8702a commit e140408
Show file tree
Hide file tree
Showing 70 changed files with 1,194 additions and 977 deletions.
14 changes: 8 additions & 6 deletions .eslintrc
Expand Up @@ -4,19 +4,21 @@
"es6": true,
"mocha": true
},
"extends": "eslint:recommended",
"plugins": [
"mocha"
],
"extends": ["eslint:recommended", "prettier"],
"plugins": ["mocha"],
"rules": {
"comma-spacing": ["error", { "before": false, "after": true }],
"complexity": ["error", 4],
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
"object-shorthand": ["error", "always", { "avoidQuotes": true, "avoidExplicitReturnArrows": true }],
"object-shorthand": [
"error",
"always",
{ "avoidQuotes": true, "avoidExplicitReturnArrows": true }
],
"mocha/no-exclusive-tests": "error",
"mocha/no-skipped-tests": "error",
"quotes": ["error", "single"],
"quotes": ["error", "single", { "avoidEscape": true }],
"semi": ["error", "always"]
}
}
1 change: 1 addition & 0 deletions .prettierignore
@@ -0,0 +1 @@
/dist
8 changes: 8 additions & 0 deletions .prettierrc
@@ -0,0 +1,8 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"arrowParens": "always",
"endOfLine": "lf"
}
66 changes: 42 additions & 24 deletions dist/structure.js
Expand Up @@ -304,7 +304,10 @@ return /******/ (function(modules) { // webpackBootstrap

joiSchema = equalOption(typeDescriptor, { initial: joiSchema });

return mapToJoi(typeDescriptor, { initial: joiSchema, mappings: this.joiMappings });
return mapToJoi(typeDescriptor, {
initial: joiSchema,
mappings: this.joiMappings
});
}
};

Expand Down Expand Up @@ -447,7 +450,10 @@ return /******/ (function(modules) { // webpackBootstrap

joiSchema = equalOption(typeDescriptor, { initial: joiSchema });

return mapToJoi(typeDescriptor, { initial: joiSchema, mappings: this.joiMappings });
return mapToJoi(typeDescriptor, {
initial: joiSchema,
mappings: this.joiMappings
});
}
};

Expand All @@ -471,7 +477,10 @@ return /******/ (function(modules) { // webpackBootstrap

joiSchema = equalOption(typeDescriptor, { initial: joiSchema });

return mapToJoi(typeDescriptor, { initial: joiSchema, mappings: this.joiMappings });
return mapToJoi(typeDescriptor, {
initial: joiSchema,
mappings: this.joiMappings
});
}
};

Expand Down Expand Up @@ -500,7 +509,10 @@ return /******/ (function(modules) { // webpackBootstrap

joiSchema = equalOption(typeDescriptor, { initial: joiSchema });

return mapToJoi(typeDescriptor, { initial: joiSchema, mappings: this.joiMappings });
return mapToJoi(typeDescriptor, {
initial: joiSchema,
mappings: this.joiMappings
});
}
};

Expand Down Expand Up @@ -594,7 +606,10 @@ return /******/ (function(modules) { // webpackBootstrap

joiSchema = joiSchema.sparse(canBeSparse);

joiSchema = mapToJoi(typeDescriptor, { initial: joiSchema, mappings: joiMappings });
joiSchema = mapToJoi(typeDescriptor, {
initial: joiSchema,
mappings: joiMappings
});

return joiSchema;
};
Expand Down Expand Up @@ -841,25 +856,28 @@ return /******/ (function(modules) { // webpackBootstrap

'use strict';

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') + ')`.');
},
nonObjectAttributes: function nonObjectAttributes() {
return new TypeError('#attributes can\'t be set to a non-object.');
},
arrayOrIterable: function arrayOrIterable() {
return new TypeError('Value must be iterable or array-like.');
},
missingDynamicType: function missingDynamicType(attributeName) {
return new Error('Missing dynamic type for attribute: ' + attributeName + '.');
},
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, StructureValidationError) {
return new StructureValidationError(errors);
}
exports.classAsSecondParam = function (ErroneousPassedClass) {
return new Error('You passed the structure class as the second parameter of attributes(). The expected usage is `attributes(schema)(' + (ErroneousPassedClass.name || 'StructureClass') + ')`.');
};

exports.nonObjectAttributes = function () {
return new TypeError("#attributes can't be set to a non-object.");
};

exports.arrayOrIterable = function () {
return new TypeError('Value must be iterable or array-like.');
};

exports.missingDynamicType = function (attributeName) {
return new Error('Missing dynamic type for attribute: ' + attributeName + '.');
};

exports.invalidType = function (attributeName) {
return new TypeError('Attribute type must be a constructor or the name of a dynamic type: ' + attributeName + '.');
};

exports.invalidAttributes = function (errors, StructureValidationError) {
return new StructureValidationError(errors);
};

/***/ },
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -36,7 +36,8 @@
"lint": "eslint {src,test,benchmark}/**/*.js",
"benchmark": "node benchmark/benchmark.js",
"prepublish": "npm run build",
"coveralls": "npm run coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
"coveralls": "npm run coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"format": "prettier --write {src,test}/**/*.js"
},
"dependencies": {
"joi": "^10.2.0",
Expand All @@ -52,6 +53,7 @@
"chai": "^4.1.2",
"coveralls": "^2.11.15",
"eslint": "^4.19.1",
"eslint-config-prettier": "^6.1.0",
"eslint-plugin-mocha": "^5.0.0",
"istanbul": "^0.4.5",
"joi-browser": "^10.0.6",
Expand All @@ -63,6 +65,7 @@
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.1",
"mocha": "^3.1.0",
"prettier": "^1.18.2",
"proxy-polyfill": "^0.1.6",
"sinon": "^7.2.4",
"webpack": "^1.13.3"
Expand Down
30 changes: 20 additions & 10 deletions src/attributes/decorator.js
Expand Up @@ -7,13 +7,13 @@ const Errors = require('../errors');
const { SCHEMA } = require('../symbols');
const {
attributeDescriptorFor,
attributesDescriptorFor
attributesDescriptorFor,
} = require('./descriptors');

const define = Object.defineProperty;

function attributesDecorator(schema, schemaOptions = {}) {
if(typeof schemaOptions !== 'object') {
if (typeof schemaOptions !== 'object') {
throw Errors.classAsSecondParam(schemaOptions);
}

Expand All @@ -26,34 +26,44 @@ function attributesDecorator(schema, schemaOptions = {}) {
Initialization.initialize(schema, passedAttributes, instance);

return instance;
}
},
});

define(WrapperClass, 'buildStrict', StrictMode.buildStrictDescriptorFor(WrapperClass, schemaOptions));
define(WrapperClass, 'buildStrict', StrictMode.buildStrictDescriptorFor(
WrapperClass,
schemaOptions
));

if(WrapperClass[SCHEMA]) {
if (WrapperClass[SCHEMA]) {
schema = Object.assign({}, WrapperClass[SCHEMA], schema);
}

schema = Schema.normalize(schema, schemaOptions);

define(WrapperClass, SCHEMA, {
value: schema
value: schema,
});

define(WrapperClass, 'validate', Validation.staticDescriptorFor(schema));

define(WrapperClass.prototype, SCHEMA, {
value: schema
value: schema,
});

define(WrapperClass.prototype, 'attributes', attributesDescriptorFor(schema));
define(WrapperClass.prototype, 'attributes', attributesDescriptorFor(
schema
));

Object.keys(schema).forEach((attr) => {
define(WrapperClass.prototype, attr, attributeDescriptorFor(attr, schema));
define(WrapperClass.prototype, attr, attributeDescriptorFor(
attr,
schema
));
});

define(WrapperClass.prototype, 'validate', Validation.descriptorFor(schema));
define(WrapperClass.prototype, 'validate', Validation.descriptorFor(
schema
));

define(WrapperClass.prototype, 'toJSON', Serialization.descriptor);

Expand Down
15 changes: 9 additions & 6 deletions src/attributes/descriptors.js
Expand Up @@ -2,7 +2,10 @@ const { isObject } = require('lodash');
const Errors = require('../errors');
const { ATTRIBUTES } = require('../symbols');

exports.attributeDescriptorFor = function attributeDescriptorFor(attributeName, schema) {
exports.attributeDescriptorFor = function attributeDescriptorFor(
attributeName,
schema
) {
return {
enumerable: true,

Expand All @@ -12,7 +15,7 @@ exports.attributeDescriptorFor = function attributeDescriptorFor(attributeName,

set(value) {
this.attributes[attributeName] = schema[attributeName].coerce(value);
}
},
};
};

Expand All @@ -23,24 +26,24 @@ exports.attributesDescriptorFor = function attributesDescriptorFor(schema) {
},

set(newAttributes) {
if(!isObject(newAttributes)) {
if (!isObject(newAttributes)) {
throw Errors.nonObjectAttributes();
}

const attributes = coerceAttributes(newAttributes, schema);

Object.defineProperty(this, ATTRIBUTES, {
configurable: true,
value: attributes
value: attributes,
});
}
},
};
};

function coerceAttributes(newAttributes, schema) {
const attributes = Object.create(null);

for(let attrName in schema) {
for (let attrName in schema) {
attributes[attrName] = schema[attrName].coerce(newAttributes[attrName]);
}

Expand Down
8 changes: 4 additions & 4 deletions src/coercion/array.js
Expand Up @@ -3,7 +3,7 @@ const getType = require('../typeResolver');

module.exports = function arrayCoercionFor(typeDescriptor, itemTypeDescriptor) {
return function coerceArray(rawValue) {
if(rawValue === undefined) {
if (rawValue === undefined) {
return;
}

Expand All @@ -18,7 +18,7 @@ module.exports = function arrayCoercionFor(typeDescriptor, itemTypeDescriptor) {
};

function validateIfIterable(value) {
if(!isIterable(value)) {
if (!isIterable(value)) {
throw Errors.arrayOrIterable();
}
}
Expand All @@ -28,7 +28,7 @@ function isIterable(value) {
}

function extractItems(iterable) {
if(!Array.isArray(iterable) && iterable[Symbol.iterator]) {
if (!Array.isArray(iterable) && iterable[Symbol.iterator]) {
return Array(...iterable);
}

Expand All @@ -41,7 +41,7 @@ function createInstance(typeDescriptor) {
}

function fillInstance(instance, items, itemTypeDescriptor) {
for(let i = 0; i < items.length; i++) {
for (let i = 0; i < items.length; i++) {
instance.push(coerceItem(itemTypeDescriptor, items[i]));
}

Expand Down
2 changes: 1 addition & 1 deletion src/coercion/boolean.js
Expand Up @@ -6,5 +6,5 @@ module.exports = {
nullValue: false,
coerce(value) {
return this.type(value);
}
},
};
30 changes: 15 additions & 15 deletions src/coercion/coercion.js
@@ -1,22 +1,20 @@
const { isFunction, curryRight } = require('lodash');

exports.execute = curryRight(
function(value, coercion, typeDescriptor) {
if(value === undefined) {
return;
}

if (value === null) {
return getNullableValue(coercion, typeDescriptor);
}
exports.execute = curryRight(function(value, coercion, typeDescriptor) {
if (value === undefined) {
return;
}

if (coercion.isCoerced(value, typeDescriptor)) {
return value;
}
if (value === null) {
return getNullableValue(coercion, typeDescriptor);
}

return coercion.coerce(value, typeDescriptor);
if (coercion.isCoerced(value, typeDescriptor)) {
return value;
}
);

return coercion.coerce(value, typeDescriptor);
});

function getNullableValue(coercion, typeDescriptor) {
return needsNullableInitialization(typeDescriptor)
Expand All @@ -29,5 +27,7 @@ function needsNullableInitialization(typeDescriptor) {
}

function getNullValue(coercion) {
return isFunction(coercion.nullValue) ? coercion.nullValue() : coercion.nullValue;
return isFunction(coercion.nullValue)
? coercion.nullValue()
: coercion.nullValue;
}
2 changes: 1 addition & 1 deletion src/coercion/date.js
Expand Up @@ -6,5 +6,5 @@ module.exports = {
nullValue: () => new Date(null),
coerce(value) {
return new this.type(value);
}
},
};
2 changes: 1 addition & 1 deletion src/coercion/generic.js
Expand Up @@ -8,5 +8,5 @@ module.exports = {
const type = getType(typeDescriptor);

return new type(value);
}
},
};

0 comments on commit e140408

Please sign in to comment.