diff --git a/.eslintrc.json b/.eslintrc.json index 81206ef..ecf96ea 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -7,10 +7,7 @@ "globals": {}, "parserOptions": { "sourceType": "module", - "ecmaVersion": 8, - "ecmaFeatures": { - "experimentalObjectRestSpread": true - } + "ecmaVersion": 2018 }, "extends": [ "eslint:recommended" @@ -19,222 +16,183 @@ "import" ], "rules": { - // POSSIBLE ERRORS - "comma-dangle": 2, - "no-cond-assign": [2, "always"], - "no-console": 2, - "no-constant-condition": 2, - "no-control-regex": 2, - "no-debugger": 2, - "no-dupe-keys": 2, - "no-duplicate-case": 2, - "no-empty-character-class": 2, - "no-empty": 2, - "no-ex-assign": 2, - "no-extra-boolean-cast": 2, - "no-extra-parens": [2, "functions"], - "no-extra-semi": 2, - "no-func-assign": 2, - "no-inner-declarations": [2, "both"], - "no-invalid-regexp": 2, - "no-irregular-whitespace": 2, - "no-negated-in-lhs": 2, - "no-obj-calls": 2, - "no-regex-spaces": 2, - "no-sparse-arrays": 2, - "no-unexpected-multiline": 2, - "no-unreachable": 2, - "use-isnan": 2, - "valid-jsdoc": 0, - "valid-typeof": 2, + "comma-dangle": "error", + "no-cond-assign": ["error", "always"], + "no-extra-parens": ["error", "functions"], + "no-inner-declarations": ["error", "both"], + "valid-jsdoc": "off", - // BEST PRACTICES "accessor-pairs": [1, { "getWithoutSet": true }], - "block-scoped-var": 0, + "block-scoped-var": "off", "consistent-return": 1, - "curly": [2, "all"], - "default-case": 0, - "dot-location": [2, "property"], - "dot-notation": [2, { + "curly": ["error", "all"], + "default-case": "off", + "dot-location": ["error", "property"], + "dot-notation": ["error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" }], - "guard-for-in": 0, - "no-alert": 2, - "no-caller": 2, - "no-case-declarations": 1, - "no-div-regex": 2, - "no-else-return": 2, - "no-empty-pattern": 2, - "no-eq-null": 0, - "no-eval": 2, + "guard-for-in": "off", + "no-caller": "error", + "no-div-regex": "error", + "no-else-return": "error", + "no-eq-null": "error", + "no-eval": "error", "no-extend-native": 1, - "no-extra-bind": 2, - "no-fallthrough": 2, - "no-floating-decimal": 2, - "no-implicit-coercion": [2, { + "no-extra-bind": "error", + "no-floating-decimal": "error", + "no-implicit-coercion": ["error", { "boolean": false }], - "no-implied-eval": 2, - "no-iterator": 2, - "no-labels": 0, - "no-lone-blocks": 2, - "no-loop-func": 0, - "no-magic-numbers": [2, { + "no-implied-eval": "error", + "no-iterator": "error", + "no-labels": "off", + "no-lone-blocks": "error", + "no-loop-func": "off", + "no-magic-numbers": ["error", { "ignore": [-1, 0, 1, 2] }], - "no-multi-spaces": 2, - "no-multi-str": 0, - "no-native-reassign": [2], - "no-new-func": 2, - "no-new-wrappers": 2, - "no-new": 2, - "no-octal-escape": 2, - "no-octal": 2, - "no-param-reassign": 0, - "no-process-env": 0, - "no-proto": 2, - "no-redeclare": 2, - "no-return-assign": 2, - "no-script-url": 2, - "no-self-compare": 2, - "no-sequences": 2, - "no-throw-literal": 2, - "no-unused-expressions": 2, - "no-useless-call": 2, - "no-useless-concat": 2, - "no-void": 2, - "no-warning-comments": [0], - "no-with": 2, - "radix": 2, - "vars-on-top": 0, - "wrap-iife": 2, - "yoda": 2, + "no-multi-spaces": "error", + "no-multi-str": "off", + "no-native-reassign": ["error"], + "no-new-func": "error", + "no-new-wrappers": "error", + "no-new": "error", + "no-octal-escape": "error", + "no-octal": "error", + "no-param-reassign": "off", + "no-process-env": "off", + "no-proto": "error", + "no-return-assign": "error", + "no-script-url": "error", + "no-self-compare": "error", + "no-sequences": "error", + "no-throw-literal": "error", + "no-unused-expressions": "error", + "no-useless-call": "error", + "no-useless-concat": "error", + "no-void": "error", + "no-warning-comments": ["off"], + "no-with": "error", + "radix": "error", + "vars-on-top": "off", + "wrap-iife": "error", + "yoda": "error", - // VARIABLES - "init-declarations": 0, - "no-catch-shadow": 0, - "no-delete-var": 2, - "no-label-var": 2, - "no-shadow-restricted-names": 2, - "no-shadow": 0, - "no-undef-init": 2, - "no-undef": 2, - "no-undefined": 0, - "no-unused-vars": 1, - "no-use-before-define": [2, "nofunc"], + "strict": ["error", "never"], - // NODE.JS AND COMMONJS - "callback-return": [2, ["callback", "cb", "next", "done"]], - "global-require": 0, - "handle-callback-err": [2, "^(e|err|error)$"], - "no-mixed-requires": 0, - "no-new-require": 2, - "no-path-concat": 2, - "no-process-exit": 2, - "no-restricted-modules": 0, - "no-sync": 0, + "init-declarations": "off", + "no-catch-shadow": "off", + "no-label-var": "error", + "no-shadow-restricted-names": "error", + "no-shadow": "off", + "no-undef-init": "error", + "no-undefined": "off", + "no-use-before-define": ["error", "nofunc"], - // STYLISTIC ISSUES - "array-bracket-spacing": 2, - "block-spacing": 2, - "brace-style": [2, "stroustrup"], - "comma-spacing": 2, - "comma-style": 2, - "computed-property-spacing": 2, - "consistent-this": [2, "that"], - "eol-last": 2, - "func-names": 2, - "func-style": [2, "declaration", { "allowArrowFunctions": true }], - "id-length": 0, - "id-match": 0, - "indent": [2, 4, { + "callback-return": ["error", ["callback", "cb", "next", "done"]], + "global-require": "off", + "handle-callback-err": ["error", "^(e|err|error)$"], + "no-mixed-requires": "off", + "no-new-require": "error", + "no-path-concat": "error", + "no-process-exit": "error", + "no-restricted-modules": "off", + "no-sync": "off", + + "array-bracket-spacing": "error", + "block-spacing": "error", + "brace-style": ["error", "stroustrup"], + "comma-spacing": "error", + "comma-style": "error", + "computed-property-spacing": "error", + "consistent-this": ["error", "that"], + "eol-last": "error", + "func-names": "error", + "func-style": ["error", "declaration", { "allowArrowFunctions": true }], + "id-length": "off", + "id-match": "off", + "indent": ["error", 4, { "SwitchCase": 1 }], - "key-spacing": 2, - "keyword-spacing": 2, - "lines-around-comment": 0, + "key-spacing": "error", + "keyword-spacing": "error", + "lines-around-comment": "off", "max-depth": [1, 4], - "max-len": [0, 80, 4], + "max-len": ["off", 80, 4], "max-nested-callbacks": [1, 5], - "max-params": [2, 8], - "max-statements": [0, 10], - "new-cap": 2, - "new-parens": 2, - "newline-after-var": 2, - "no-array-constructor": 2, - "no-bitwise": [2, { "allow": ["~"] }], + "max-params": ["error", 8], + "max-statements": ["off", 10], + "new-cap": "error", + "new-parens": "error", + "newline-after-var": "error", + "no-array-constructor": "error", + "no-bitwise": ["error", { "allow": ["~"] }], "no-continue": 1, - "no-inline-comments": 0, - "no-lonely-if": 2, - "no-mixed-spaces-and-tabs": 2, - "no-multiple-empty-lines": [2, { + "no-inline-comments": "off", + "no-lonely-if": "error", + "no-mixed-spaces-and-tabs": "error", + "no-multiple-empty-lines": ["error", { "max": 1, "maxBOF": 0 }], - "no-negated-condition": 2, - "no-nested-ternary": 2, - "no-new-object": 2, - "no-plusplus": 0, - "no-restricted-syntax": 0, - "no-spaced-func": 2, - "no-ternary": 0, - "no-trailing-spaces": 0, - "no-underscore-dangle": 0, - "no-unneeded-ternary": [2, { + "no-negated-condition": "error", + "no-nested-ternary": "error", + "no-new-object": "error", + "no-plusplus": "off", + "no-restricted-syntax": "off", + "no-spaced-func": "error", + "no-ternary": "off", + "no-trailing-spaces": "off", + "no-underscore-dangle": "off", + "no-unneeded-ternary": ["error", { "defaultAssignment": false }], - "object-curly-spacing": [2, "always", { + "object-curly-spacing": ["error", "always", { "objectsInObjects": false, "arraysInObjects": true }], - "one-var": [2, "never"], - "operator-assignment": [2, "always"], - "operator-linebreak": [2, "before"], - "padded-blocks": [2, "never"], - "quote-props": [2, "as-needed", { + "one-var": ["error", "never"], + "operator-assignment": ["error", "always"], + "operator-linebreak": ["error", "before"], + "padded-blocks": ["error", "never"], + "prefer-object-spread": "error", + "quote-props": ["error", "as-needed", { "keywords": true }], - "quotes": [2, "single", "avoid-escape"], - "semi-spacing": 2, - "semi": [2, "always"], - "sort-vars": 0, - "space-before-blocks": 2, - "space-before-function-paren": [2, "never"], - "space-in-parens": 2, - "space-infix-ops": [2, { + "quotes": ["error", "single", "avoid-escape"], + "semi-spacing": "error", + "semi": ["error", "always"], + "sort-vars": "off", + "space-before-blocks": "error", + "space-before-function-paren": ["error", "never"], + "space-in-parens": "error", + "space-infix-ops": ["error", { "int32Hint": true }], - "space-unary-ops": 2, - "spaced-comment": [2, "always", { + "space-unary-ops": "error", + "spaced-comment": ["error", "always", { "exceptions": ["-", "+", "*"] }], - "wrap-regex": 0, + "wrap-regex": "off", - // ECMASCRIPT 6 - "strict": [2, "never"], - "import/no-commonjs": [2, { "allowRequire": true }], - "arrow-body-style": [0, "never"], - "arrow-parens": [2, "as-needed"], - "arrow-spacing": 2, - "constructor-super": 2, - "generator-star-spacing": 2, - "no-class-assign": 2, - "no-confusing-arrow": 2, - "no-const-assign": 2, - "no-dupe-class-members": 2, - "no-this-before-super": 2, - "no-var": 2, - "object-shorthand": 2, - "prefer-arrow-callback": 2, + "arrow-body-style": ["off", "never"], + "arrow-parens": ["error", "as-needed"], + "arrow-spacing": "error", + "generator-star-spacing": "error", + "no-confusing-arrow": "error", + "no-var": "error", + "object-shorthand": "error", + "prefer-arrow-callback": "error", "prefer-spread": 1, - "prefer-template": 2, + "prefer-template": "error", "prefer-const": ["error", { "destructuring": "all", "ignoreReadBeforeAssign": false }], "no-useless-constructor": 1, - "require-yield": 2 + + "import/no-commonjs": ["error", { "allowRequire": true }] } } diff --git a/src/utils/database/orm/index.js b/src/utils/database/orm/index.js index e64a6fb..af536c3 100644 --- a/src/utils/database/orm/index.js +++ b/src/utils/database/orm/index.js @@ -13,8 +13,13 @@ const ASSOCIATION_TYPES = { export default class Orm { constructor(options) { - options = Object.assign({ operatorsAliases: false, logging: false }, options); - this.conn = new Sequelize(options.database, options.username, options.password, options); + const settings = { + operatorsAliases: false, + logging: false, + ...options + }; + + this.conn = new Sequelize(settings.database, settings.username, settings.password, settings); this.models = {}; } diff --git a/src/utils/helpers/units.test.js b/src/utils/helpers/units.test.js index 239cbde..bd429fb 100644 --- a/src/utils/helpers/units.test.js +++ b/src/utils/helpers/units.test.js @@ -5,7 +5,7 @@ import { DAY, toMilliseconds } from './units'; describe('Helpers: Units', () => { it('should return a time in milliseconds', () => { const inMs = toMilliseconds(DAY); - const expected = DAY * 1000; + const expected = DAY * 1000; // eslint-disable-line no-magic-numbers expect(inMs).toEqual(expected); }); diff --git a/src/utils/validator/index.test.js b/src/utils/validator/index.test.js new file mode 100644 index 0000000..505366b --- /dev/null +++ b/src/utils/validator/index.test.js @@ -0,0 +1,78 @@ +/* global describe it expect */ +import Validator from './'; + +import Joi from 'joi'; +const schema = Joi.object().keys({ + field: Joi.string().required() +}); + +describe('Utils: Validator', () => { + it('"getter defaultOptions": should return an object with default options', () => { + const options = Validator.defaultOptions; + + expect(options).toBeInstanceOf(Object); + expect(Object.keys(options).length).toBeGreaterThan(1); + }); + + describe('validate()', () => { + it('should fail when values are not present', () => { + const { error } = Validator.validate({}, schema); + + expect(error).toBeInstanceOf(Error); + expect(error.message).toBe('child "field" fails because ["field" is required]'); + }); + + it('should fail when receiving invalid values', () => { + const { error } = Validator.validate({ field: 1234 }, schema); + + expect(error).toBeInstanceOf(Error); + expect(error.message).toBe('child "field" fails because ["field" must be a string]'); + }); + + it('should work when receiving valid values', () => { + const { value } = Validator.validate({ field: 'string' }, schema); + + expect(typeof value.field).toBe('string'); + }); + }); + + describe('parseValidation()', () => { + it('should return validation details', () => { + const validation = Validator.validate({ field: 'string' }, schema); + const parsed = Validator.parseValidation(validation); + + expect(parsed).toBeInstanceOf(Object); + expect(Object.keys(parsed)).toEqual(['errors', 'value']); + }); + + it('should return "errors" key undefined when receiving valid data', () => { + const validation = Validator.validate({ field: 'string' }, schema); + const parsed = Validator.parseValidation(validation); + + expect(parsed.errors).toBeUndefined(); + }); + + it('should return "errors" key with details when receiving invalid data', () => { + const validation = Validator.validate({ field: 1234 }, schema); + const parsed = Validator.parseValidation(validation); + + expect(parsed.errors).toContainEqual({ field: '"field" must be a string' }); + }); + }); + + describe('toResponse()', () => { + it('should return a Boom formatted error', () => { + const validation = Validator.validate({ field: 1234 }, schema); + const parsed = Validator.parseValidation(validation); + const response = Validator.toResponse(parsed.errors); + const payload = response.output.payload; + + expect(response).toBeInstanceOf(Error); + expect(response.isBoom).toBeTruthy(); + expect(payload.error).toBe('Bad Request'); + expect(payload.message).toBe('Validation Errors'); + expect(payload.statusCode).toBe(400); // eslint-disable-line no-magic-numbers + expect(payload.errors).toBeInstanceOf(Array); + }); + }); +});