diff --git a/.eslintrc b/.eslintrc index 24de724..05e6851 100644 --- a/.eslintrc +++ b/.eslintrc @@ -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"] } } diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..9b1c8b1 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +/dist diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..b36678f --- /dev/null +++ b/.prettierrc @@ -0,0 +1,8 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": true, + "arrowParens": "always", + "endOfLine": "lf" +} diff --git a/dist/structure.js b/dist/structure.js index 8ea6a8f..facbcf4 100644 --- a/dist/structure.js +++ b/dist/structure.js @@ -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 + }); } }; @@ -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 + }); } }; @@ -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 + }); } }; @@ -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 + }); } }; @@ -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; }; @@ -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); }; /***/ }, diff --git a/package.json b/package.json index caaa8d7..2868c00 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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" diff --git a/src/attributes/decorator.js b/src/attributes/decorator.js index 634c321..81bbdce 100644 --- a/src/attributes/decorator.js +++ b/src/attributes/decorator.js @@ -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); } @@ -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); diff --git a/src/attributes/descriptors.js b/src/attributes/descriptors.js index e3aafe3..75685d9 100644 --- a/src/attributes/descriptors.js +++ b/src/attributes/descriptors.js @@ -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, @@ -12,7 +15,7 @@ exports.attributeDescriptorFor = function attributeDescriptorFor(attributeName, set(value) { this.attributes[attributeName] = schema[attributeName].coerce(value); - } + }, }; }; @@ -23,7 +26,7 @@ exports.attributesDescriptorFor = function attributesDescriptorFor(schema) { }, set(newAttributes) { - if(!isObject(newAttributes)) { + if (!isObject(newAttributes)) { throw Errors.nonObjectAttributes(); } @@ -31,16 +34,16 @@ exports.attributesDescriptorFor = function attributesDescriptorFor(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]); } diff --git a/src/coercion/array.js b/src/coercion/array.js index e3dd7a9..a843bf2 100644 --- a/src/coercion/array.js +++ b/src/coercion/array.js @@ -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; } @@ -18,7 +18,7 @@ module.exports = function arrayCoercionFor(typeDescriptor, itemTypeDescriptor) { }; function validateIfIterable(value) { - if(!isIterable(value)) { + if (!isIterable(value)) { throw Errors.arrayOrIterable(); } } @@ -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); } @@ -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])); } diff --git a/src/coercion/boolean.js b/src/coercion/boolean.js index 062ca94..f52d8f5 100644 --- a/src/coercion/boolean.js +++ b/src/coercion/boolean.js @@ -6,5 +6,5 @@ module.exports = { nullValue: false, coerce(value) { return this.type(value); - } + }, }; diff --git a/src/coercion/coercion.js b/src/coercion/coercion.js index b6733b7..dc6d809 100644 --- a/src/coercion/coercion.js +++ b/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) @@ -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; } diff --git a/src/coercion/date.js b/src/coercion/date.js index d93b493..179027d 100644 --- a/src/coercion/date.js +++ b/src/coercion/date.js @@ -6,5 +6,5 @@ module.exports = { nullValue: () => new Date(null), coerce(value) { return new this.type(value); - } + }, }; diff --git a/src/coercion/generic.js b/src/coercion/generic.js index 7e6f9e7..4f028a3 100644 --- a/src/coercion/generic.js +++ b/src/coercion/generic.js @@ -8,5 +8,5 @@ module.exports = { const type = getType(typeDescriptor); return new type(value); - } + }, }; diff --git a/src/coercion/index.js b/src/coercion/index.js index 4a6c662..41662cb 100644 --- a/src/coercion/index.js +++ b/src/coercion/index.js @@ -6,11 +6,11 @@ const types = [ require('./string'), require('./number'), require('./boolean'), - require('./date') + require('./date'), ]; exports.for = function coercionFor(typeDescriptor, itemTypeDescriptor) { - if(itemTypeDescriptor) { + if (itemTypeDescriptor) { return arrayCoercionFor(typeDescriptor, itemTypeDescriptor); } @@ -22,7 +22,7 @@ exports.for = function coercionFor(typeDescriptor, itemTypeDescriptor) { function getCoercion(typeDescriptor) { const coercion = types.find((c) => c.type === typeDescriptor.type); - if(coercion) { + if (coercion) { return coercion; } diff --git a/src/coercion/number.js b/src/coercion/number.js index a26e1f1..39cf269 100644 --- a/src/coercion/number.js +++ b/src/coercion/number.js @@ -6,5 +6,5 @@ module.exports = { nullValue: 0, coerce(value) { return this.type(value); - } + }, }; diff --git a/src/coercion/string.js b/src/coercion/string.js index 234b28d..71aa79b 100644 --- a/src/coercion/string.js +++ b/src/coercion/string.js @@ -6,5 +6,5 @@ module.exports = { nullValue: '', coerce(value) { return this.type(value); - } + }, }; diff --git a/src/errors/index.js b/src/errors/index.js index 01e8cc6..48408af 100644 --- a/src/errors/index.js +++ b/src/errors/index.js @@ -1,8 +1,22 @@ -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, StructureValidationError) => new StructureValidationError(errors) -}; +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'})\`.` + ); + +exports.nonObjectAttributes = () => + new TypeError("#attributes can't be set to a non-object."); + +exports.arrayOrIterable = () => + new TypeError('Value must be iterable or array-like.'); + +exports.missingDynamicType = (attributeName) => + new Error(`Missing dynamic type for attribute: ${attributeName}.`); + +exports.invalidType = (attributeName) => + new TypeError( + `Attribute type must be a constructor or the name of a dynamic type: ${attributeName}.` + ); + +exports.invalidAttributes = (errors, StructureValidationError) => + new StructureValidationError(errors); diff --git a/src/index.js b/src/index.js index 7dd967d..d316a05 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,3 @@ module.exports = { - attributes: require('./attributes') + attributes: require('./attributes'), }; diff --git a/src/initialization/index.js b/src/initialization/index.js index 818b47a..8b280dd 100644 --- a/src/initialization/index.js +++ b/src/initialization/index.js @@ -1,4 +1,4 @@ module.exports = { forSchema: require('./initialization'), - initialize: require('./initialize') + initialize: require('./initialize'), }; diff --git a/src/initialization/initialization.js b/src/initialization/initialization.js index 5f293a7..f337904 100644 --- a/src/initialization/initialization.js +++ b/src/initialization/initialization.js @@ -1,8 +1,13 @@ const { ATTRIBUTES } = require('../symbols'); const initializationOrderFor = require('./initializationOrder'); -function initializedValue(attrPassedValue, attrInitializer, attrDescriptor, instance) { - if(attrPassedValue !== undefined) { +function initializedValue( + attrPassedValue, + attrInitializer, + attrDescriptor, + instance +) { + if (attrPassedValue !== undefined) { return attrPassedValue; } @@ -16,13 +21,18 @@ module.exports = function forSchema(schema) { initialize(attributes, instance) { instance[ATTRIBUTES] = Object.create(null); - for(let i = 0; i < initializationOrder.length; i++) { - const [ attrName, attrInitializer ] = initializationOrder[i]; + for (let i = 0; i < initializationOrder.length; i++) { + const [attrName, attrInitializer] = initializationOrder[i]; const attrDescriptor = schema[attrName]; const attrPassedValue = attributes[attrName]; - instance[attrName] = initializedValue(attrPassedValue, attrInitializer, attrDescriptor, instance); + instance[attrName] = initializedValue( + attrPassedValue, + attrInitializer, + attrDescriptor, + instance + ); } - } + }, }; }; diff --git a/src/initialization/initializationOrder.js b/src/initialization/initializationOrder.js index 409e986..1325595 100644 --- a/src/initialization/initializationOrder.js +++ b/src/initialization/initializationOrder.js @@ -16,10 +16,10 @@ module.exports = function initializationOrderFor(schema) { const staticInitializations = []; const derivedInitializations = []; - for(let attrName in schema) { + for (let attrName in schema) { const attributeDescriptor = schema[attrName]; - if(isStaticInitialization(attributeDescriptor)) { + if (isStaticInitialization(attributeDescriptor)) { staticInitializations.push([attrName, staticInitialization]); } else { derivedInitializations.push([attrName, derivedInitialization]); diff --git a/src/schema/index.js b/src/schema/index.js index 53f2e22..8e892b3 100644 --- a/src/schema/index.js +++ b/src/schema/index.js @@ -1,3 +1,3 @@ module.exports = { - normalize: require('./normalization') + normalize: require('./normalization'), }; diff --git a/src/schema/normalization.js b/src/schema/normalization.js index b57edd3..37d7703 100644 --- a/src/schema/normalization.js +++ b/src/schema/normalization.js @@ -7,19 +7,23 @@ module.exports = function normalizeSchema(rawSchema, schemaOptions) { const schema = Object.create(null); Object.keys(rawSchema).forEach((attributeName) => { - schema[attributeName] = TypeDescriptor.normalize(schemaOptions, rawSchema[attributeName], attributeName); + schema[attributeName] = TypeDescriptor.normalize( + schemaOptions, + rawSchema[attributeName], + attributeName + ); }); const schemaValidation = Validation.forSchema(schema); Object.defineProperty(schema, VALIDATE, { - value: schemaValidation + value: schemaValidation, }); const initialization = Initialization.forSchema(schema); Object.defineProperty(schema, INITIALIZE, { - value: initialization + value: initialization, }); return schema; diff --git a/src/serialization/descriptor.js b/src/serialization/descriptor.js index b79e9c2..5cbaefb 100644 --- a/src/serialization/descriptor.js +++ b/src/serialization/descriptor.js @@ -3,5 +3,5 @@ const serialize = require('./serialize'); module.exports = { value: function toJSON() { return serialize(this); - } + }, }; diff --git a/src/serialization/index.js b/src/serialization/index.js index 4b66cc2..e41b561 100644 --- a/src/serialization/index.js +++ b/src/serialization/index.js @@ -1,3 +1,3 @@ module.exports = { - descriptor: require('./descriptor') + descriptor: require('./descriptor'), }; diff --git a/src/serialization/serialize.js b/src/serialization/serialize.js index 8c77f7d..a042cdc 100644 --- a/src/serialization/serialize.js +++ b/src/serialization/serialize.js @@ -2,7 +2,7 @@ const { SCHEMA } = require('../symbols'); const getType = require('../typeResolver'); function serialize(structure) { - if(structure == null) { + if (structure == null) { return structure; } @@ -18,11 +18,15 @@ function getTypeSchema(typeDescriptor) { function serializeStructure(structure, schema) { const serializedStructure = Object.create(null); - for(let attrName in schema) { + for (let attrName in schema) { let attribute = structure[attrName]; - if(isPresent(attribute) || isNullable(attribute, schema, attrName)) { - serializedStructure[attrName] = serializeAttribute(attribute, attrName, schema); + if (isPresent(attribute) || isNullable(attribute, schema, attrName)) { + serializedStructure[attrName] = serializeAttribute( + attribute, + attrName, + schema + ); } } @@ -38,11 +42,11 @@ function isNullable(attribute, schema, attrName) { } function serializeAttribute(attribute, attrName, schema) { - if(isArrayType(schema, attrName)) { + if (isArrayType(schema, attrName)) { return attribute.map(serialize); } - if(isNestedSchema(schema, attrName)) { + if (isNestedSchema(schema, attrName)) { return serialize(attribute); } diff --git a/src/strictMode/index.js b/src/strictMode/index.js index 5e506f0..6aaf4f1 100644 --- a/src/strictMode/index.js +++ b/src/strictMode/index.js @@ -1,8 +1,12 @@ const Errors = require('../errors'); const DefaultValidationError = require('../errors/DefaultValidationError'); -exports.buildStrictDescriptorFor = function buildStrictDescriptorFor(StructureClass, schemaOptions) { - const StructureValidationError = schemaOptions.strictValidationErrorClass || DefaultValidationError; +exports.buildStrictDescriptorFor = function buildStrictDescriptorFor( + StructureClass, + schemaOptions +) { + const StructureValidationError = + schemaOptions.strictValidationErrorClass || DefaultValidationError; return { value: function buildStrict(constructorArgs) { @@ -15,6 +19,6 @@ exports.buildStrictDescriptorFor = function buildStrictDescriptorFor(StructureCl } return instance; - } + }, }; }; diff --git a/src/symbols.js b/src/symbols.js index 0a9ed78..596c2e3 100644 --- a/src/symbols.js +++ b/src/symbols.js @@ -2,5 +2,5 @@ module.exports = { SCHEMA: Symbol('schema'), ATTRIBUTES: Symbol('attributes'), VALIDATE: Symbol('validate'), - INITIALIZE: Symbol('initialize') + INITIALIZE: Symbol('initialize'), }; diff --git a/src/typeDescriptor/index.js b/src/typeDescriptor/index.js index 1b5e138..9163d74 100644 --- a/src/typeDescriptor/index.js +++ b/src/typeDescriptor/index.js @@ -4,21 +4,33 @@ const Coercion = require('../coercion'); const Validation = require('../validation'); function normalizeTypeDescriptor(schemaOptions, typeDescriptor, attributeName) { - if(isShorthandTypeDescriptor(typeDescriptor)) { + if (isShorthandTypeDescriptor(typeDescriptor)) { typeDescriptor = convertToCompleteTypeDescriptor(typeDescriptor); } validateTypeDescriptor(typeDescriptor, attributeName); - return normalizeCompleteTypeDescriptor(schemaOptions, typeDescriptor, attributeName); + return normalizeCompleteTypeDescriptor( + schemaOptions, + typeDescriptor, + attributeName + ); } -function normalizeCompleteTypeDescriptor(schemaOptions, typeDescriptor, attributeName) { - if(isDynamicTypeDescriptor(typeDescriptor)) { - typeDescriptor = addDynamicTypeGetter(schemaOptions, typeDescriptor, attributeName); +function normalizeCompleteTypeDescriptor( + schemaOptions, + typeDescriptor, + attributeName +) { + if (isDynamicTypeDescriptor(typeDescriptor)) { + typeDescriptor = addDynamicTypeGetter( + schemaOptions, + typeDescriptor, + attributeName + ); } - if(isArrayType(typeDescriptor)) { + if (isArrayType(typeDescriptor)) { typeDescriptor.itemType = normalizeTypeDescriptor( schemaOptions, typeDescriptor.itemType, @@ -32,12 +44,15 @@ function normalizeCompleteTypeDescriptor(schemaOptions, typeDescriptor, attribut function createNormalizedTypeDescriptor(typeDescriptor) { return Object.assign({}, typeDescriptor, { coerce: Coercion.for(typeDescriptor, typeDescriptor.itemType), - validation: Validation.forAttribute(typeDescriptor) + validation: Validation.forAttribute(typeDescriptor), }); } function validateTypeDescriptor(typeDescriptor, attributeName) { - if(!isObject(typeDescriptor.type) && !isDynamicTypeDescriptor(typeDescriptor)) { + if ( + !isObject(typeDescriptor.type) && + !isDynamicTypeDescriptor(typeDescriptor) + ) { throw Errors.invalidType(attributeName); } } @@ -47,7 +62,7 @@ function isDynamicTypeDescriptor(typeDescriptor) { } function addDynamicTypeGetter(schemaOptions, typeDescriptor, attributeName) { - if(!hasDynamicType(schemaOptions, typeDescriptor)) { + if (!hasDynamicType(schemaOptions, typeDescriptor)) { throw Errors.missingDynamicType(attributeName); } diff --git a/src/typeResolver.js b/src/typeResolver.js index 14ac804..97246b1 100644 --- a/src/typeResolver.js +++ b/src/typeResolver.js @@ -1,5 +1,5 @@ module.exports = function getAttributeType(typeDescriptor) { - if(typeDescriptor.dynamicType) { + if (typeDescriptor.dynamicType) { return typeDescriptor.getType(); } diff --git a/src/validation/array.js b/src/validation/array.js index 0cae966..e1c4f88 100644 --- a/src/validation/array.js +++ b/src/validation/array.js @@ -4,16 +4,20 @@ const { mapToJoi } = require('./utils'); const joiMappings = [ ['minLength', 'min', true], ['maxLength', 'max', true], - ['exactLength', 'length', true] + ['exactLength', 'length', true], ]; module.exports = function arrayValidation(typeDescriptor, itemTypeDescriptor) { var joiSchema = joi.array().items(itemTypeDescriptor.validation); - const canBeSparse = typeDescriptor.sparse === undefined || typeDescriptor.sparse; + const canBeSparse = + typeDescriptor.sparse === undefined || typeDescriptor.sparse; joiSchema = joiSchema.sparse(canBeSparse); - joiSchema = mapToJoi(typeDescriptor, { initial: joiSchema, mappings: joiMappings }); + joiSchema = mapToJoi(typeDescriptor, { + initial: joiSchema, + mappings: joiMappings, + }); return joiSchema; }; diff --git a/src/validation/boolean.js b/src/validation/boolean.js index 8c597e6..58147ef 100644 --- a/src/validation/boolean.js +++ b/src/validation/boolean.js @@ -9,6 +9,9 @@ module.exports = { joiSchema = equalOption(typeDescriptor, { initial: joiSchema }); - return mapToJoi(typeDescriptor, { initial: joiSchema, mappings: this.joiMappings }); - } + return mapToJoi(typeDescriptor, { + initial: joiSchema, + mappings: this.joiMappings, + }); + }, }; diff --git a/src/validation/date.js b/src/validation/date.js index d6b0176..c35a48a 100644 --- a/src/validation/date.js +++ b/src/validation/date.js @@ -4,18 +4,18 @@ const { mapToJoi, mapToJoiWithReference, equalOption } = require('./utils'); module.exports = { type: Date, joiMappings: [], - valueOrRefOptions: [ - ['min', 'min'], - ['max', 'max'], - ], + valueOrRefOptions: [['min', 'min'], ['max', 'max']], createJoiSchema(typeDescriptor) { var joiSchema = mapToJoiWithReference(typeDescriptor, { initial: joi.date(), - mappings: this.valueOrRefOptions + mappings: this.valueOrRefOptions, }); joiSchema = equalOption(typeDescriptor, { initial: joiSchema }); - return mapToJoi(typeDescriptor, { initial: joiSchema, mappings: this.joiMappings }); - } + return mapToJoi(typeDescriptor, { + initial: joiSchema, + mappings: this.joiMappings, + }); + }, }; diff --git a/src/validation/descriptors.js b/src/validation/descriptors.js index 7771f37..6aa1fbd 100644 --- a/src/validation/descriptors.js +++ b/src/validation/descriptors.js @@ -1,6 +1,8 @@ const { SCHEMA, VALIDATE } = require('../symbols'); -exports.validationDescriptorForSchema = function validationDescriptorForSchema(schema) { +exports.validationDescriptorForSchema = function validationDescriptorForSchema( + schema +) { const validation = schema[VALIDATE]; return { @@ -8,31 +10,33 @@ exports.validationDescriptorForSchema = function validationDescriptorForSchema(s const serializedStructure = this.toJSON(); return validateData(validation, serializedStructure); - } + }, }; }; -exports.staticValidationDescriptorForSchema = function staticValidationDescriptorForSchema(schema) { +exports.staticValidationDescriptorForSchema = function staticValidationDescriptorForSchema( + schema +) { const validation = schema[VALIDATE]; return { value: function validate(data) { - if(data[SCHEMA]) { + if (data[SCHEMA]) { data = data.toJSON(); } return validateData(validation, data); - } + }, }; }; function validateData(validation, data) { const errors = validation.validate(data); - if(errors) { + if (errors) { return { valid: false, - errors + errors, }; } diff --git a/src/validation/index.js b/src/validation/index.js index 4528b28..8aae48a 100644 --- a/src/validation/index.js +++ b/src/validation/index.js @@ -12,20 +12,20 @@ const arrayValidation = require('./array'); const { validationDescriptorForSchema, - staticValidationDescriptorForSchema + staticValidationDescriptorForSchema, } = require('./descriptors'); exports.descriptorFor = validationDescriptorForSchema; exports.staticDescriptorFor = staticValidationDescriptorForSchema; exports.forAttribute = function validationForAttribute(typeDescriptor) { - if(typeDescriptor.itemType !== undefined) { + if (typeDescriptor.itemType !== undefined) { return arrayValidation(typeDescriptor, typeDescriptor.itemType); } const validation = validations.find((v) => v.type === typeDescriptor.type); - if(!validation) { + if (!validation) { return nestedValidation(typeDescriptor); } @@ -37,7 +37,7 @@ const mapDetail = ({ message, path }) => ({ message, path }); const validatorOptions = { abortEarly: false, convert: false, - allowUnknown: false + allowUnknown: false, }; exports.forSchema = function validationForSchema(schema) { @@ -55,11 +55,11 @@ exports.forSchema = function validationForSchema(schema) { const { error } = joiValidation.validate(structure, validatorOptions); - if(error) { + if (error) { validationErrors = error.details.map(mapDetail); } return validationErrors; - } + }, }; }; diff --git a/src/validation/nested.js b/src/validation/nested.js index 4186011..3634331 100644 --- a/src/validation/nested.js +++ b/src/validation/nested.js @@ -3,7 +3,7 @@ const { SCHEMA } = require('../symbols'); const { requiredOption } = require('./utils'); module.exports = function nestedValidation(typeDescriptor) { - if(typeDescriptor.dynamicType) { + if (typeDescriptor.dynamicType) { return validationToDynamicType(typeDescriptor); } @@ -11,7 +11,7 @@ module.exports = function nestedValidation(typeDescriptor) { var joiSchema = getNestedValidations(typeSchema); joiSchema = requiredOption(typeDescriptor, { - initial: joiSchema + initial: joiSchema, }); return joiSchema; @@ -25,7 +25,7 @@ function validationToDynamicType(typeDescriptor) { }); joiSchema = requiredOption(typeDescriptor, { - initial: joiSchema + initial: joiSchema, }); return joiSchema; @@ -34,12 +34,14 @@ function validationToDynamicType(typeDescriptor) { function getNestedValidations(typeSchema) { var joiSchema = joi.object(); - if(typeSchema) { - const nestedValidations = Object.keys(typeSchema) - .reduce((validations, v) => { + if (typeSchema) { + const nestedValidations = Object.keys(typeSchema).reduce( + (validations, v) => { validations[v] = typeSchema[v].validation; return validations; - }, {}); + }, + {} + ); joiSchema = joiSchema.keys(nestedValidations); } diff --git a/src/validation/number.js b/src/validation/number.js index ec04a35..7671414 100644 --- a/src/validation/number.js +++ b/src/validation/number.js @@ -8,22 +8,25 @@ module.exports = { ['precision', 'precision', true], ['multiple', 'multiple', true], ['positive', 'positive', true], - ['negative', 'negative', true] + ['negative', 'negative', true], ], valueOrRefOptions: [ ['min', 'min'], ['greater', 'greater'], ['max', 'max'], - ['less', 'less'] + ['less', 'less'], ], createJoiSchema(typeDescriptor) { var joiSchema = mapToJoiWithReference(typeDescriptor, { initial: joi.number(), - mappings: this.valueOrRefOptions + mappings: this.valueOrRefOptions, }); joiSchema = equalOption(typeDescriptor, { initial: joiSchema }); - return mapToJoi(typeDescriptor, { initial: joiSchema, mappings: this.joiMappings }); - } + return mapToJoi(typeDescriptor, { + initial: joiSchema, + mappings: this.joiMappings, + }); + }, }; diff --git a/src/validation/string.js b/src/validation/string.js index e493b76..dd87e47 100644 --- a/src/validation/string.js +++ b/src/validation/string.js @@ -13,17 +13,20 @@ module.exports = { ['lowerCase', 'lowercase'], ['upperCase', 'uppercase'], ['email', 'email'], - ['guid', 'guid', isPlainObject] + ['guid', 'guid', isPlainObject], ], createJoiSchema(typeDescriptor) { var joiSchema = joi.string(); - if(typeDescriptor.empty) { + if (typeDescriptor.empty) { joiSchema = joiSchema.allow(''); } joiSchema = equalOption(typeDescriptor, { initial: joiSchema }); - return mapToJoi(typeDescriptor, { initial: joiSchema, mappings: this.joiMappings }); - } + return mapToJoi(typeDescriptor, { + initial: joiSchema, + mappings: this.joiMappings, + }); + }, }; diff --git a/src/validation/utils.js b/src/validation/utils.js index e3f6975..1c0cad6 100644 --- a/src/validation/utils.js +++ b/src/validation/utils.js @@ -2,18 +2,21 @@ const joi = require('joi'); const { isPlainObject, isFunction } = require('lodash'); exports.mapToJoi = function mapToJoi(typeDescriptor, { initial, mappings }) { - let joiSchema = mappings.reduce((joiSchema, [optionName, joiMethod, passValueToJoi]) => { - const attributeDescriptor = typeDescriptor[optionName]; - if(attributeDescriptor === undefined) { - return joiSchema; - } - - if(shouldPassValueToJoi(passValueToJoi, attributeDescriptor)) { - return joiSchema[joiMethod](attributeDescriptor); - } - - return joiSchema[joiMethod](); - }, initial); + let joiSchema = mappings.reduce( + (joiSchema, [optionName, joiMethod, passValueToJoi]) => { + const attributeDescriptor = typeDescriptor[optionName]; + if (attributeDescriptor === undefined) { + return joiSchema; + } + + if (shouldPassValueToJoi(passValueToJoi, attributeDescriptor)) { + return joiSchema[joiMethod](attributeDescriptor); + } + + return joiSchema[joiMethod](); + }, + initial + ); joiSchema = requiredOption(typeDescriptor, { initial: joiSchema }); @@ -21,22 +24,28 @@ exports.mapToJoi = function mapToJoi(typeDescriptor, { initial, mappings }) { }; function shouldPassValueToJoi(passValueToJoi, attributeDescriptor) { - return passValueToJoi && (!isFunction(passValueToJoi) || passValueToJoi(attributeDescriptor)); + return ( + passValueToJoi && + (!isFunction(passValueToJoi) || passValueToJoi(attributeDescriptor)) + ); } function mapValueOrReference(valueOrReference) { - if(isPlainObject(valueOrReference)) { + if (isPlainObject(valueOrReference)) { return joi.ref(valueOrReference.attr); } return valueOrReference; } -exports.mapToJoiWithReference = function mapToJoiWithReference(typeDescriptor, { initial, mappings }) { +exports.mapToJoiWithReference = function mapToJoiWithReference( + typeDescriptor, + { initial, mappings } +) { return mappings.reduce((joiSchema, [optionName, joiMethod]) => { var attributeDescriptor = typeDescriptor[optionName]; - if(attributeDescriptor === undefined) { + if (attributeDescriptor === undefined) { return joiSchema; } @@ -49,11 +58,11 @@ exports.mapToJoiWithReference = function mapToJoiWithReference(typeDescriptor, { exports.equalOption = function equalOption(typeDescriptor, { initial }) { var possibilities = typeDescriptor.equal; - if(possibilities === undefined) { + if (possibilities === undefined) { return initial; } - if(Array.isArray(possibilities)) { + if (Array.isArray(possibilities)) { possibilities = possibilities.map(mapValueOrReference); } else { possibilities = mapValueOrReference(possibilities); @@ -63,11 +72,11 @@ exports.equalOption = function equalOption(typeDescriptor, { initial }) { }; function requiredOption(typeDescriptor, { initial }) { - if(typeDescriptor.nullable) { + if (typeDescriptor.nullable) { initial = initial.allow(null); } - if(typeDescriptor.required) { + if (typeDescriptor.required) { initial = initial.required(); } diff --git a/test/fixtures/BrokenCircularBook.js b/test/fixtures/BrokenCircularBook.js index dc9bbc2..03f7680 100644 --- a/test/fixtures/BrokenCircularBook.js +++ b/test/fixtures/BrokenCircularBook.js @@ -1,10 +1,13 @@ const { attributes } = require('../../src'); -const Book = attributes({ - name: String, - owner: 'User' -}, { - dynamics: { } -})(class Book { }); +const Book = attributes( + { + name: String, + owner: 'User', + }, + { + dynamics: {}, + } +)(class Book {}); module.exports = Book; diff --git a/test/fixtures/CircularBook.js b/test/fixtures/CircularBook.js index 7c8f644..d35fb98 100644 --- a/test/fixtures/CircularBook.js +++ b/test/fixtures/CircularBook.js @@ -1,17 +1,20 @@ const { attributes } = require('../../src'); -const Book = attributes({ - name: String, - owner: 'User', - nextBook: 'Book', - pages: { - type: Number +const Book = attributes( + { + name: String, + owner: 'User', + nextBook: 'Book', + pages: { + type: Number, + }, + }, + { + dynamics: { + User: () => require('./CircularUser'), + Book: () => Book, + }, } -}, { - dynamics: { - User: () => require('./CircularUser'), - Book: () => Book - } -})(class Book { }); +)(class Book {}); module.exports = Book; diff --git a/test/fixtures/CircularUser.js b/test/fixtures/CircularUser.js index 2e5b513..d2f9f0f 100644 --- a/test/fixtures/CircularUser.js +++ b/test/fixtures/CircularUser.js @@ -1,29 +1,32 @@ const { attributes } = require('../../src'); -const User = attributes({ - name: String, - friends: { - type: Array, - itemType: 'User' +const User = attributes( + { + name: String, + friends: { + type: Array, + itemType: 'User', + }, + favoriteBook: { + type: 'Book', + required: true, + nullable: true, + }, + books: { + type: 'BooksCollection', + itemType: String, + }, + nextBook: { + type: 'Book', + }, }, - favoriteBook: { - type: 'Book', - required: true, - nullable: true - }, - books: { - type: 'BooksCollection', - itemType: String - }, - nextBook: { - type: 'Book' - } -}, { - dynamics: { - User: () => User, - Book: () => require('./CircularBook'), - BooksCollection: () => require('./BooksCollection') + { + dynamics: { + User: () => User, + Book: () => require('./CircularBook'), + BooksCollection: () => require('./BooksCollection'), + }, } -})(class User { }); +)(class User {}); module.exports = User; diff --git a/test/karma.conf.js b/test/karma.conf.js index cd4da4a..4fe0f26 100644 --- a/test/karma.conf.js +++ b/test/karma.conf.js @@ -4,10 +4,10 @@ Object.assign(webpackConfig, { externals: {}, resolve: { alias: { - joi: 'joi-browser' - } + joi: 'joi-browser', + }, }, - devtool: 'inline-source-map' + devtool: 'inline-source-map', }); module.exports = function(config) { @@ -18,28 +18,26 @@ module.exports = function(config) { reporters: ['mocha'], singleRun: true, - files: [ - 'browserSetup.js' - ], + files: ['browserSetup.js'], preprocessors: { - 'browserSetup.js': ['webpack', 'sourcemap'] + 'browserSetup.js': ['webpack', 'sourcemap'], }, mochaReporter: { - showDiff: true + showDiff: true, }, webpack: webpackConfig, webpackMiddleware: { - noInfo: true + noInfo: true, }, beforeMiddleware: ['webpackBlocker'], phantomjsLauncher: { - exitOnResourceError: true - } + exitOnResourceError: true, + }, }); }; diff --git a/test/unit/coercion/array.spec.js b/test/unit/coercion/array.spec.js index 8b9b4e6..330c3d5 100644 --- a/test/unit/coercion/array.spec.js +++ b/test/unit/coercion/array.spec.js @@ -10,14 +10,14 @@ describe('type coercion', () => { User = attributes({ books: { type: Array, - itemType: String - } + itemType: String, + }, })(class User {}); }); it('does not coerces undefined', () => { const user = new User({ - books: undefined + books: undefined, }); expect(user.books).to.be.undefined; @@ -26,26 +26,20 @@ describe('type coercion', () => { context('when raw value is already an array', () => { it('coerces items', () => { const user = new User({ - books: ['The Lord of The Rings', 1984, true] + books: ['The Lord of The Rings', 1984, true], }); - expect(user.books).to.eql([ - 'The Lord of The Rings', - '1984', - 'true' - ]); + expect(user.books).to.eql(['The Lord of The Rings', '1984', 'true']); }); it('does not coerce items that are of the expected type', () => { const book = new String('A Game of Thrones'); const user = new User({ - books: [book] + books: [book], }); - expect(user.books).to.eql([ - new String('A Game of Thrones') - ]); + expect(user.books).to.eql([new String('A Game of Thrones')]); expect(user.books[0]).to.equal(book); }); }); @@ -53,7 +47,7 @@ describe('type coercion', () => { context('when raw value is a string', () => { it('uses each character as an item', () => { const user = new User({ - books: 'ABC' + books: 'ABC', }); expect(user.books).to.eql(['A', 'B', 'C']); @@ -61,7 +55,7 @@ describe('type coercion', () => { it('coerces empty string to empty array', () => { const user = new User({ - books: '' + books: '', }); expect(user.books).to.eql([]); @@ -71,12 +65,12 @@ describe('type coercion', () => { const Library = attributes({ bookIds: { type: Array, - itemType: Number - } + itemType: Number, + }, })(class Library {}); const library = new Library({ - bookIds: '123' + bookIds: '123', }); expect(library.bookIds).to.eql([1, 2, 3]); @@ -86,7 +80,7 @@ describe('type coercion', () => { context('when raw value is an array-like', () => { it('loops using #length property', () => { const user = new User({ - books: { 0: 'Stonehenge', 1: 1984, length: 2 } + books: { 0: 'Stonehenge', 1: 1984, length: 2 }, }); expect(user.books).to.eql(['Stonehenge', '1984']); @@ -97,10 +91,10 @@ describe('type coercion', () => { it('converts to array then uses each index', () => { const books = { *[Symbol.iterator]() { - for(let i = 0; i < 3; i++) { + for (let i = 0; i < 3; i++) { yield i; } - } + }, }; const user = new User({ books }); @@ -113,7 +107,7 @@ describe('type coercion', () => { it('throws an error', () => { expect(() => { new User({ - books: 123 + books: 123, }); }).to.throw(TypeError, /^Value must be iterable or array-like\.$/); }); @@ -124,19 +118,17 @@ describe('type coercion', () => { Seat = attributes({ seats: { type: Array, - itemType: Number - } + itemType: Number, + }, })(class Seat {}); }); it('return the correct array', () => { const seat = new Seat({ - seats: [1] + seats: [1], }); - expect(seat.seats).to.eql([ - 1 - ]); + expect(seat.seats).to.eql([1]); }); }); }); @@ -152,10 +144,7 @@ describe('type coercion', () => { it('coerces collection', () => { const user = new CircularUser({ - books: [ - 'Dragons of Ether', - 'The Dark Tower' - ] + books: ['Dragons of Ether', 'The Dark Tower'], }); expect(user.books).to.be.instanceOf(BooksCollection); @@ -163,26 +152,20 @@ describe('type coercion', () => { it('coerces items', () => { const user = new CircularUser({ - books: ['The Lord of The Rings', 1984, true] + books: ['The Lord of The Rings', 1984, true], }); - expect(user.books).to.eql([ - 'The Lord of The Rings', - '1984', - 'true' - ]); + expect(user.books).to.eql(['The Lord of The Rings', '1984', 'true']); }); it('does not coerce items that are of the expected type', () => { const book = new String('A Game of Thrones'); const user = new CircularUser({ - books: [book] + books: [book], }); - expect(user.books).to.eql([ - new String('A Game of Thrones') - ]); + expect(user.books).to.eql([new String('A Game of Thrones')]); expect(user.books[0]).to.equal(book); }); }); diff --git a/test/unit/coercion/arraySubclass.spec.js b/test/unit/coercion/arraySubclass.spec.js index dc1c9a0..c5e8426 100644 --- a/test/unit/coercion/arraySubclass.spec.js +++ b/test/unit/coercion/arraySubclass.spec.js @@ -12,14 +12,14 @@ describe('type coercion', () => { User = attributes({ books: { type: Collection, - itemType: String - } + itemType: String, + }, })(class User {}); }); it('does not coerces undefined', () => { const user = new User({ - books: undefined + books: undefined, }); expect(user.books).to.be.undefined; @@ -28,19 +28,15 @@ describe('type coercion', () => { context('when raw value is already an array', () => { it('coerces items', () => { const user = new User({ - books: ['The Lord of The Rings', 1984, true] + books: ['The Lord of The Rings', 1984, true], }); - expect(user.books).to.eql([ - 'The Lord of The Rings', - '1984', - 'true' - ]); + expect(user.books).to.eql(['The Lord of The Rings', '1984', 'true']); }); it('coerces value to instance of array subclass', () => { const user = new User({ - books: ['The Lord of The Rings', 1984, true] + books: ['The Lord of The Rings', 1984, true], }); expect(user.books).to.be.instanceOf(Collection); @@ -50,12 +46,10 @@ describe('type coercion', () => { const book = new String('A Game of Thrones'); const user = new User({ - books: [book] + books: [book], }); - expect(user.books).to.eql([ - new String('A Game of Thrones') - ]); + expect(user.books).to.eql([new String('A Game of Thrones')]); expect(user.books[0]).to.equal(book); }); }); @@ -63,7 +57,7 @@ describe('type coercion', () => { context('when raw value is a string', () => { it('uses each character as an item', () => { const user = new User({ - books: 'ABC' + books: 'ABC', }); expect(user.books).to.eql(['A', 'B', 'C']); @@ -71,7 +65,7 @@ describe('type coercion', () => { it('coerces empty string to empty array', () => { const user = new User({ - books: '' + books: '', }); expect(user.books).to.eql([]); @@ -81,12 +75,12 @@ describe('type coercion', () => { const Library = attributes({ bookIds: { type: Array, - itemType: Number - } + itemType: Number, + }, })(class Library {}); const library = new Library({ - bookIds: '123' + bookIds: '123', }); expect(library.bookIds).to.eql([1, 2, 3]); @@ -94,7 +88,7 @@ describe('type coercion', () => { it('coerces value to instance of array subclass', () => { const user = new User({ - books: 'ABC' + books: 'ABC', }); expect(user.books).to.be.instanceOf(Collection); @@ -104,7 +98,7 @@ describe('type coercion', () => { context('when raw value is an array-like', () => { it('loops using #length property', () => { const user = new User({ - books: { 0: 'Stonehenge', 1: 1984, length: 2 } + books: { 0: 'Stonehenge', 1: 1984, length: 2 }, }); expect(user.books).to.eql(['Stonehenge', '1984']); @@ -112,7 +106,7 @@ describe('type coercion', () => { it('coerces value to instance of array subclass', () => { const user = new User({ - books: { 0: 'Stonehenge', 1: 1984, length: 2 } + books: { 0: 'Stonehenge', 1: 1984, length: 2 }, }); expect(user.books).to.be.instanceOf(Collection); @@ -122,14 +116,13 @@ describe('type coercion', () => { context('when raw value implements Symbol.iterator', () => { const books = { *[Symbol.iterator]() { - for(let i = 0; i < 3; i++) { + for (let i = 0; i < 3; i++) { yield i; } - } + }, }; it('converts to array then uses each index', () => { - const user = new User({ books }); expect(user.books).to.eql(['0', '1', '2']); @@ -146,7 +139,7 @@ describe('type coercion', () => { it('throws an error', () => { expect(() => { new User({ - books: 123 + books: 123, }); }).to.throw(TypeError, /^Value must be iterable or array-like\.$/); }); diff --git a/test/unit/coercion/boolean.spec.js b/test/unit/coercion/boolean.spec.js index fef3912..89c7e47 100644 --- a/test/unit/coercion/boolean.spec.js +++ b/test/unit/coercion/boolean.spec.js @@ -10,14 +10,14 @@ describe('type coercion', () => { isAdmin: Boolean, hasAccepted: { type: Boolean, - nullable: true - } + nullable: true, + }, })(class User {}); }); it('does not coerces undefined', () => { const user = new User({ - isAdmin: undefined + isAdmin: undefined, }); expect(user.isAdmin).to.be.undefined; @@ -25,7 +25,7 @@ describe('type coercion', () => { it('does not coerces null when nullable', () => { const user = new User({ - hasAccepted: null + hasAccepted: null, }); expect(user.hasAccepted).to.be.null; @@ -33,7 +33,7 @@ describe('type coercion', () => { it('coerces string to boolean', () => { const user = new User({ - isAdmin: '10' + isAdmin: '10', }); expect(user.isAdmin).to.equal(true); @@ -41,7 +41,7 @@ describe('type coercion', () => { it('coerces empty string to false', () => { const user = new User({ - isAdmin: '' + isAdmin: '', }); expect(user.isAdmin).to.equal(false); @@ -49,7 +49,7 @@ describe('type coercion', () => { it('coerces null to false', () => { const user = new User({ - isAdmin: null + isAdmin: null, }); expect(user.isAdmin).to.be.false; @@ -57,7 +57,7 @@ describe('type coercion', () => { it('coerces positive number to true', () => { const user = new User({ - isAdmin: 1 + isAdmin: 1, }); expect(user.isAdmin).to.be.true; @@ -65,7 +65,7 @@ describe('type coercion', () => { it('coerces negative number to true', () => { const user = new User({ - isAdmin: -1 + isAdmin: -1, }); expect(user.isAdmin).to.be.true; @@ -73,7 +73,7 @@ describe('type coercion', () => { it('coerces zero to false', () => { const user = new User({ - isAdmin: 0 + isAdmin: 0, }); expect(user.isAdmin).to.be.false; @@ -83,7 +83,7 @@ describe('type coercion', () => { const date = new Date(); const user = new User({ - isAdmin: date + isAdmin: date, }); expect(user.isAdmin).to.be.true; @@ -91,7 +91,7 @@ describe('type coercion', () => { it('coerces object to true', () => { const user = new User({ - isAdmin: {} + isAdmin: {}, }); expect(user.isAdmin).to.be.true; diff --git a/test/unit/coercion/coercion.spec.js b/test/unit/coercion/coercion.spec.js index c063e9f..52bd8b5 100644 --- a/test/unit/coercion/coercion.spec.js +++ b/test/unit/coercion/coercion.spec.js @@ -16,14 +16,18 @@ describe('Coercion', () => { }); it('returns undefined', () => { - const executionResponse = Coercion.execute(value, coercion, typeDescriptor); + const executionResponse = Coercion.execute( + value, + coercion, + typeDescriptor + ); expect(executionResponse).to.be.undefined; }); }); context('when value is null', () => { - beforeEach(() => value = null); + beforeEach(() => (value = null)); context('and attribute is nullable', () => { beforeEach(() => { @@ -32,7 +36,11 @@ describe('Coercion', () => { }); it('returns null', () => { - const executionResponse = Coercion.execute(value, coercion, typeDescriptor); + const executionResponse = Coercion.execute( + value, + coercion, + typeDescriptor + ); expect(executionResponse).to.be.null; }); @@ -45,7 +53,11 @@ describe('Coercion', () => { }); it('returns default value present on Coercion object', () => { - const executionResponse = Coercion.execute(value, coercion, typeDescriptor); + const executionResponse = Coercion.execute( + value, + coercion, + typeDescriptor + ); expect(executionResponse).to.be.eq(0); }); @@ -58,13 +70,21 @@ describe('Coercion', () => { }); it('returns default value present on Coercion object', () => { - const executionResponse = Coercion.execute(value, coercion, typeDescriptor); + const executionResponse = Coercion.execute( + value, + coercion, + typeDescriptor + ); expect(executionResponse).to.be.eql(new Date('1970-01-01T00:00:00Z')); }); it('creates a new object instance for default value', () => { - const executionResponse = Coercion.execute(value, coercion, typeDescriptor); + const executionResponse = Coercion.execute( + value, + coercion, + typeDescriptor + ); expect(executionResponse).not.to.be.eq(coercion.nullValue()); }); @@ -83,7 +103,11 @@ describe('Coercion', () => { afterEach(() => CoercionNumber.coerce.restore()); it('returns default value present on Coercion object', () => { - const executionResponse = Coercion.execute(value, coercion, typeDescriptor); + const executionResponse = Coercion.execute( + value, + coercion, + typeDescriptor + ); expect(executionResponse).to.be.eq(42); }); @@ -103,7 +127,11 @@ describe('Coercion', () => { }); it('returns default value present on Coercion object', () => { - const executionResponse = Coercion.execute(value, coercion, typeDescriptor); + const executionResponse = Coercion.execute( + value, + coercion, + typeDescriptor + ); expect(executionResponse).to.be.eq(1008); }); diff --git a/test/unit/coercion/date.spec.js b/test/unit/coercion/date.spec.js index c8db555..e34027a 100644 --- a/test/unit/coercion/date.spec.js +++ b/test/unit/coercion/date.spec.js @@ -10,8 +10,8 @@ describe('type coercion', () => { birth: Date, death: { type: Date, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -26,7 +26,7 @@ describe('type coercion', () => { it('does not coerces undefined', () => { const user = new User({ - birth: undefined + birth: undefined, }); expect(user.birth).to.be.undefined; @@ -34,7 +34,7 @@ describe('type coercion', () => { it('does not coerces null when nullable', () => { const user = new User({ - death: null + death: null, }); expect(user.death).to.be.null; @@ -42,7 +42,7 @@ describe('type coercion', () => { it('coerces string to date', () => { const user = new User({ - birth: 'Feb 3, 1892' + birth: 'Feb 3, 1892', }); expect(user.birth).to.eql(new Date('Feb 3, 1892')); @@ -50,7 +50,7 @@ describe('type coercion', () => { it('coerces null to first date on Unix time', () => { const user = new User({ - birth: null + birth: null, }); expect(user.birth).to.eql(new Date('1970-01-01T00:00:00Z')); diff --git a/test/unit/coercion/number.spec.js b/test/unit/coercion/number.spec.js index 7bf2500..af5e28c 100644 --- a/test/unit/coercion/number.spec.js +++ b/test/unit/coercion/number.spec.js @@ -10,8 +10,8 @@ describe('type coercion', () => { age: Number, earnings: { type: Number, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -27,7 +27,7 @@ describe('type coercion', () => { it('does not coerces undefined', () => { const user = new User({ - age: undefined + age: undefined, }); expect(user.age).to.be.undefined; @@ -35,7 +35,7 @@ describe('type coercion', () => { it('does not coerce null when nullable', () => { const user = new User({ - earnings: null + earnings: null, }); expect(user.earnings).to.be.null; @@ -43,7 +43,7 @@ describe('type coercion', () => { it('coerces string to number', () => { const user = new User({ - age: '10' + age: '10', }); expect(user.age).to.equal(10); @@ -51,7 +51,7 @@ describe('type coercion', () => { it('coerces null to zero', () => { const user = new User({ - age: null + age: null, }); expect(user.age).to.equal(0); @@ -59,7 +59,7 @@ describe('type coercion', () => { it('coerces true to one', () => { const user = new User({ - age: true + age: true, }); expect(user.age).to.equal(1); @@ -67,7 +67,7 @@ describe('type coercion', () => { it('coerces false to zero', () => { const user = new User({ - age: false + age: false, }); expect(user.age).to.equal(0); @@ -77,7 +77,7 @@ describe('type coercion', () => { const date = new Date(); const user = new User({ - age: date + age: date, }); expect(user.age).to.equal(date.valueOf()); @@ -89,7 +89,7 @@ describe('type coercion', () => { const objectWithoutValueOf = { data: 42 }; const user = new User({ - age: objectWithoutValueOf + age: objectWithoutValueOf, }); expect(Number.isNaN(user.age)).to.be.true; @@ -100,11 +100,13 @@ describe('type coercion', () => { it('coerces object to value returned by #valueOf()', () => { const objectWithValueOf = { data: '42', - valueOf() { return this.data; } + valueOf() { + return this.data; + }, }; const user = new User({ - age: objectWithValueOf + age: objectWithValueOf, }); expect(user.age).to.equal(42); diff --git a/test/unit/coercion/pojo.spec.js b/test/unit/coercion/pojo.spec.js index 1510ad9..c4e0b2e 100644 --- a/test/unit/coercion/pojo.spec.js +++ b/test/unit/coercion/pojo.spec.js @@ -15,12 +15,12 @@ describe('type coercion', () => { }; User = attributes({ - location: Location + location: Location, })(class User {}); }); it('does not coerce if raw value is an instance of class', () => { - const location = new Location({ x: 1, y: 2}); + const location = new Location({ x: 1, y: 2 }); const user = new User({ location }); @@ -29,7 +29,7 @@ describe('type coercion', () => { it('instantiates class with raw value', () => { const user = new User({ - location: { x: 1, y: 2} + location: { x: 1, y: 2 }, }); expect(user.location).to.be.instanceOf(Location); @@ -39,7 +39,7 @@ describe('type coercion', () => { it('does not coerce undefined', () => { const user = new User({ - location: undefined + location: undefined, }); expect(user.location).to.be.undefined; diff --git a/test/unit/coercion/string.spec.js b/test/unit/coercion/string.spec.js index 0d2014d..c3b0e97 100644 --- a/test/unit/coercion/string.spec.js +++ b/test/unit/coercion/string.spec.js @@ -10,8 +10,8 @@ describe('type coercion', () => { name: String, fatherName: { type: String, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -28,7 +28,7 @@ describe('type coercion', () => { it('does not coerces undefined', () => { const user = new User({ - name: undefined + name: undefined, }); expect(user.name).to.be.undefined; @@ -36,7 +36,7 @@ describe('type coercion', () => { it('coerces integer to string', () => { const user = new User({ - name: 10 + name: 10, }); expect(user.name).to.equal('10'); @@ -44,7 +44,7 @@ describe('type coercion', () => { it('coerces float to string', () => { const user = new User({ - name: 10.42 + name: 10.42, }); expect(user.name).to.equal('10.42'); @@ -52,7 +52,7 @@ describe('type coercion', () => { it('coerces null to empty string', () => { const user = new User({ - name: null + name: null, }); expect(user.name).to.equal(''); @@ -60,7 +60,7 @@ describe('type coercion', () => { it('coerces boolean to string', () => { const user = new User({ - name: false + name: false, }); expect(user.name).to.equal('false'); @@ -70,7 +70,7 @@ describe('type coercion', () => { const date = new Date(); const user = new User({ - name: date + name: date, }); expect(user.name).to.equal(date.toString()); @@ -90,7 +90,7 @@ describe('type coercion', () => { it('does not coerces undefined', () => { const user = new User({ - fatherName: undefined + fatherName: undefined, }); expect(user.fatherName).to.be.undefined; @@ -98,7 +98,7 @@ describe('type coercion', () => { it('does not coerces null', () => { const user = new User({ - fatherName: null + fatherName: null, }); expect(user.fatherName).to.be.null; @@ -106,7 +106,7 @@ describe('type coercion', () => { it('coerces integer to string', () => { const user = new User({ - fatherName: 10 + fatherName: 10, }); expect(user.fatherName).to.equal('10'); @@ -114,7 +114,7 @@ describe('type coercion', () => { it('coerces float to string', () => { const user = new User({ - fatherName: 10.42 + fatherName: 10.42, }); expect(user.fatherName).to.equal('10.42'); @@ -122,7 +122,7 @@ describe('type coercion', () => { it('coerces boolean to string', () => { const user = new User({ - fatherName: false + fatherName: false, }); expect(user.fatherName).to.equal('false'); @@ -132,7 +132,7 @@ describe('type coercion', () => { const date = new Date(); const user = new User({ - fatherName: date + fatherName: date, }); expect(user.fatherName).to.equal(date.toString()); @@ -145,7 +145,7 @@ describe('type coercion', () => { const objectWithoutToString = { data: 42 }; const user = new User({ - name: objectWithoutToString + name: objectWithoutToString, }); expect(user.name).to.equal('[object Object]'); @@ -156,11 +156,13 @@ describe('type coercion', () => { it('coerces object to value returned from #toString()', () => { const objectWithToString = { data: 42, - toString() { return this.data; } + toString() { + return this.data; + }, }; const user = new User({ - name: objectWithToString + name: objectWithToString, }); expect(user.name).to.equal('42'); diff --git a/test/unit/coercion/structure.spec.js b/test/unit/coercion/structure.spec.js index 168fcd3..9c54721 100644 --- a/test/unit/coercion/structure.spec.js +++ b/test/unit/coercion/structure.spec.js @@ -9,15 +9,15 @@ describe('type coercion', () => { beforeEach(() => { Location = attributes({ x: Number, - y: Number + y: Number, })(class Location {}); User = attributes({ location: Location, destination: { type: Location, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -39,7 +39,7 @@ describe('type coercion', () => { beforeEach(() => { user = new User({ - location: { x: 1, y: 2 } + location: { x: 1, y: 2 }, }); }); @@ -55,7 +55,7 @@ describe('type coercion', () => { beforeEach(() => { user = new User({ - location: { x: '1', y: '2' } + location: { x: '1', y: '2' }, }); }); @@ -69,7 +69,7 @@ describe('type coercion', () => { context('when value is undefined', () => { var user; - beforeEach(() => user = new User({ location: undefined })); + beforeEach(() => (user = new User({ location: undefined }))); it('does not coerce', () => { expect(user.location).to.be.undefined; @@ -80,7 +80,7 @@ describe('type coercion', () => { var user; context('and attribute is nullable', () => { - beforeEach(() => user = new User({ destination: null })); + beforeEach(() => (user = new User({ destination: null }))); it('assigns null', () => { expect(user.destination).to.be.null; @@ -88,7 +88,7 @@ describe('type coercion', () => { }); context('and attribute is not nullable', () => { - beforeEach(() => user = new User({ location: null })); + beforeEach(() => (user = new User({ location: null }))); it('assigns undefined', () => { expect(user.location).to.be.undefined; @@ -115,15 +115,15 @@ describe('type coercion', () => { friends: [], favoriteBook: { name: 'The Silmarillion', - owner: {} + owner: {}, }, - nextBook: null + nextBook: null, }); userTwo = new CircularUser({ name: 'Circular user two', friends: [userOne], - nextBook: null + nextBook: null, }); }); @@ -142,13 +142,13 @@ describe('type coercion', () => { const user = new CircularUser({ favoriteBook: { name: 'The Silmarillion', - owner: {} - } + owner: {}, + }, }); user.favoriteBook = { name: 'The World of Ice & Fire', - owner: { name: 'New name' } + owner: { name: 'New name' }, }; expect(user.favoriteBook).to.be.instanceOf(CircularBook); diff --git a/test/unit/coercion/typeCoercion.spec.js b/test/unit/coercion/typeCoercion.spec.js index 1d93240..5bb3cb5 100644 --- a/test/unit/coercion/typeCoercion.spec.js +++ b/test/unit/coercion/typeCoercion.spec.js @@ -6,7 +6,7 @@ describe('type coercion', () => { beforeEach(() => { User = attributes({ - name: String + name: String, })(class User {}); }); diff --git a/test/unit/creatingStructureClass.spec.js b/test/unit/creatingStructureClass.spec.js index 59167c2..ee8fcc9 100644 --- a/test/unit/creatingStructureClass.spec.js +++ b/test/unit/creatingStructureClass.spec.js @@ -20,7 +20,10 @@ describe('creating a structure class', () => { expect(() => { attributes({}, anonymousClass); - }).to.throw(Error, /^You passed the structure class.*\(StructureClass\)`\./); + }).to.throw( + Error, + /^You passed the structure class.*\(StructureClass\)`\./ + ); }); }); }); @@ -38,7 +41,7 @@ describe('creating a structure class', () => { RawUser.staticProperty = 'I am a static property'; User = attributes({ - name: String + name: String, })(RawUser); }); @@ -54,7 +57,7 @@ describe('creating a structure class', () => { beforeEach(() => { User = attributes({ - age: { type: Number, default: () => 18 } + age: { type: Number, default: () => 18 }, })(class User {}); }); @@ -65,33 +68,36 @@ describe('creating a structure class', () => { }); }); - context('when the function default value uses another class attribute', () => { - var User; - - beforeEach(() => { - User = attributes({ - name: String, - surname: String, - fullname: { - type: String, - default: (self) => `${self.name} ${self.surname}` - } - })(class User {}); - }); - - it('defines the attribute with the default value executing the function', () => { - const user = new User({ name: 'Jack', surname: 'Sparrow' }); - - expect(user.fullname).to.equal('Jack Sparrow'); - }); - }); + context( + 'when the function default value uses another class attribute', + () => { + var User; + + beforeEach(() => { + User = attributes({ + name: String, + surname: String, + fullname: { + type: String, + default: (self) => `${self.name} ${self.surname}`, + }, + })(class User {}); + }); + + it('defines the attribute with the default value executing the function', () => { + const user = new User({ name: 'Jack', surname: 'Sparrow' }); + + expect(user.fullname).to.equal('Jack Sparrow'); + }); + } + ); context('when the provided default value is a property', () => { var User; beforeEach(() => { User = attributes({ - age: { type: Number, default: 18 } + age: { type: Number, default: 18 }, })(class User {}); }); diff --git a/test/unit/instanceAndUpdate.spec.js b/test/unit/instanceAndUpdate.spec.js index 194e666..1347715 100644 --- a/test/unit/instanceAndUpdate.spec.js +++ b/test/unit/instanceAndUpdate.spec.js @@ -8,41 +8,43 @@ describe('instantiating a structure', () => { User = attributes({ name: { type: String, - default: 'Name' + default: 'Name', }, password: { type: String, - required: true + required: true, }, nickname: { type: String, - default: (instance) => instance.name + default: (instance) => instance.name, }, uuid: { type: String, - default: (instance) => instance.getUuid() + default: (instance) => instance.getUuid(), }, attrUsingMethodUsingAttr: { type: String, - default: (instance) => instance.someMethod() - } - })(class User { - constructor() { - this.userInstanceStuff = 'Stuff value'; - } - - userMethod() { - return 'I am a user'; - } - - getUuid() { - return 10; - } - - someMethod() { - return `Method => ${this.name}`; + default: (instance) => instance.someMethod(), + }, + })( + class User { + constructor() { + this.userInstanceStuff = 'Stuff value'; + } + + userMethod() { + return 'I am a user'; + } + + getUuid() { + return 10; + } + + someMethod() { + return `Method => ${this.name}`; + } } - }); + ); }); it('has access to instance methods', () => { @@ -59,7 +61,7 @@ describe('instantiating a structure', () => { it('has attributes passed to constructor assigned to the object', () => { const user = new User({ - password: 'My password' + password: 'My password', }); expect(user.password).to.equal('My password'); @@ -75,7 +77,7 @@ describe('instantiating a structure', () => { it('ignores invalid attributes passed to constructor', () => { const user = new User({ - invalid: 'I will be ignored' + invalid: 'I will be ignored', }); expect(user.invalid).to.be.undefined; @@ -83,7 +85,7 @@ describe('instantiating a structure', () => { it('reflects instance attributes to #attributes', () => { const user = new User({ - password: 'The password' + password: 'The password', }); expect(user.password).to.equal('The password'); @@ -108,31 +110,45 @@ describe('instantiating a structure', () => { }); }); - context('when attribute dynamic default uses a static defaultable attribute', () => { - context('when static defaultable attribute uses default value', () => { - it('allows to access the value of that attribute', () => { - const user = new User(); - - expect(user.nickname).to.equal('Name'); - }); - }); - - context('when static defaultable attribute has a value passed to it', () => { - it('allows to access the value of that attribute', () => { - const user = new User({ name: 'This is my name' }); + context( + 'when attribute dynamic default uses a static defaultable attribute', + () => { + context( + 'when static defaultable attribute uses default value', + () => { + it('allows to access the value of that attribute', () => { + const user = new User(); - expect(user.nickname).to.equal('This is my name'); - }); - }); + expect(user.nickname).to.equal('Name'); + }); + } + ); - context('when dynamic default uses a method that uses an attribute with default', () => { - it('generates the default value properly', () => { - const user = new User(); + context( + 'when static defaultable attribute has a value passed to it', + () => { + it('allows to access the value of that attribute', () => { + const user = new User({ name: 'This is my name' }); - expect(user.attrUsingMethodUsingAttr).to.equal('Method => Name'); - }); - }); - }); + expect(user.nickname).to.equal('This is my name'); + }); + } + ); + + context( + 'when dynamic default uses a method that uses an attribute with default', + () => { + it('generates the default value properly', () => { + const user = new User(); + + expect(user.attrUsingMethodUsingAttr).to.equal( + 'Method => Name' + ); + }); + } + ); + } + ); it('overrides default value with passed value', () => { const user = new User({ name: 'Not the default' }); @@ -145,14 +161,19 @@ describe('instantiating a structure', () => { context('when object is invalid', () => { context('when using default error class', () => { it('throws a default error', () => { - let errorDetails = [{ - message: '"password" is required', - path: 'password' - }]; + let errorDetails = [ + { + message: '"password" is required', + path: 'password', + }, + ]; expect(() => { User.buildStrict(); - }).to.throw(Error, 'Invalid Attributes').with.property('details').that.deep.equals(errorDetails); + }) + .to.throw(Error, 'Invalid Attributes') + .with.property('details') + .that.deep.equals(errorDetails); }); }); @@ -168,20 +189,23 @@ describe('instantiating a structure', () => { } }; - UserWithCustomError = attributes({ - name: { - type: String, - minLength: 3 + UserWithCustomError = attributes( + { + name: { + type: String, + minLength: 3, + }, + }, + { + strictValidationErrorClass: InvalidUser, } - }, { - strictValidationErrorClass: InvalidUser - })(class UserWithCustomError {}); + )(class UserWithCustomError {}); }); it('throws a custom error', () => { expect(() => { UserWithCustomError.buildStrict({ - name: 'JJ' + name: 'JJ', }); }).to.throw(InvalidUser, 'There is something wrong with this user'); }); @@ -191,7 +215,7 @@ describe('instantiating a structure', () => { context('when object is valid', () => { it('return an intance', () => { const user = User.buildStrict({ - password: 'My password' + password: 'My password', }); expect(user.password).to.equal('My password'); @@ -216,13 +240,13 @@ describe('instantiating a structure with dynamic attribute types', () => { friends: [], favoriteBook: new CircularBook({ name: 'Brave new world', - owner: new CircularUser() - }) + owner: new CircularUser(), + }), }); const userTwo = new CircularUser({ name: 'Circular user two', - friends: [userOne] + friends: [userOne], }); expect(userOne).to.be.instanceOf(CircularUser); @@ -235,10 +259,12 @@ describe('instantiating a structure with dynamic attribute types', () => { describe('with buildStrict', () => { context('when object is invalid', () => { it('throw an error', () => { - let errorDetails = [{ - message: '"pages" must be a number', - path: 'favoriteBook.pages' - }]; + let errorDetails = [ + { + message: '"pages" must be a number', + path: 'favoriteBook.pages', + }, + ]; expect(() => { CircularUser.buildStrict({ @@ -246,11 +272,13 @@ describe('instantiating a structure with dynamic attribute types', () => { friends: [], favoriteBook: new CircularBook({ name: 'Brave new world', - pages: 'twenty' - }) + pages: 'twenty', + }), }); - - }).to.throw(Error, 'Invalid Attributes').with.property('details').that.deep.equals(errorDetails); + }) + .to.throw(Error, 'Invalid Attributes') + .with.property('details') + .that.deep.equals(errorDetails); }); }); }); @@ -261,15 +289,13 @@ describe('updating an instance', () => { beforeEach(() => { User = attributes({ - name: String - })(class User { - - }); + name: String, + })(class User {}); }); it('updates instance attribute value when assigned a new value', () => { const user = new User({ - name: 'My name' + name: 'My name', }); user.name = 'New name'; @@ -279,7 +305,7 @@ describe('updating an instance', () => { it('reflects new value assigned to attribute on #attributes', () => { const user = new User({ - name: 'My name' + name: 'My name', }); user.name = 'New name'; @@ -289,11 +315,11 @@ describe('updating an instance', () => { it('reflects new value assigned to #attributes on instance attribute', () => { const user = new User({ - name: 'My name' + name: 'My name', }); user.attributes = { - name: 'New name' + name: 'New name', }; expect(user.name).to.equal('New name'); @@ -307,7 +333,7 @@ describe('updating an instance', () => { it('throws if value assigned to #attributes is not an object', () => { const user = new User({ - name: 'My name' + name: 'My name', }); expect(() => { @@ -329,13 +355,13 @@ describe('updating a structure with dynamic attribute types', () => { const user = new CircularUser({ favoriteBook: new CircularBook({ name: 'Brave new world', - owner: new CircularUser() - }) + owner: new CircularUser(), + }), }); user.favoriteBook = new CircularBook({ name: '1984', - owner: user + owner: user, }); expect(user.favoriteBook).to.be.instanceOf(CircularBook); diff --git a/test/unit/schema/normalization.spec.js b/test/unit/schema/normalization.spec.js index a754cd4..383dccd 100644 --- a/test/unit/schema/normalization.spec.js +++ b/test/unit/schema/normalization.spec.js @@ -5,7 +5,7 @@ describe('schema normalization', () => { context('when passed attribute is the type itself', () => { it('normalizes to an object with the type field being equal to the passed type', () => { const schema = { - name: String + name: String, }; expect(normalize(schema).name.type).to.equal(String); @@ -16,7 +16,7 @@ describe('schema normalization', () => { context('when attribute object has the field type', () => { it('normalizes to an object with the type field being equal to the passed type', () => { const schema = { - name: { type: String } + name: { type: String }, }; expect(normalize(schema).name.type).to.equal(String); @@ -31,23 +31,32 @@ describe('schema normalization', () => { expect(() => { normalize(schema); - }).to.throw(TypeError, /^Attribute type must be a constructor or the name of a dynamic type: name\.$/); + }).to.throw( + TypeError, + /^Attribute type must be a constructor or the name of a dynamic type: name\.$/ + ); }); }); - context('when attribute descriptor is complete but #type is not a constructor', () => { - it('throws an error', () => { - const schema = { - name: { - type: true - } - }; + context( + 'when attribute descriptor is complete but #type is not a constructor', + () => { + it('throws an error', () => { + const schema = { + name: { + type: true, + }, + }; - expect(() => { - normalize(schema); - }).to.throw(TypeError, /^Attribute type must be a constructor or the name of a dynamic type: name\.$/); - }); - }); + expect(() => { + normalize(schema); + }).to.throw( + TypeError, + /^Attribute type must be a constructor or the name of a dynamic type: name\.$/ + ); + }); + } + ); }); context('when attribute has itemType', () => { @@ -56,8 +65,8 @@ describe('schema normalization', () => { const schema = { name: { type: Array, - itemType: { type: String } - } + itemType: { type: String }, + }, }; expect(normalize(schema).name.itemType.type).to.eql(String); @@ -69,8 +78,8 @@ describe('schema normalization', () => { const schema = { name: { type: Array, - itemType: String - } + itemType: String, + }, }; expect(normalize(schema).name.itemType.type).to.eql(String); diff --git a/test/unit/serialization/array.spec.js b/test/unit/serialization/array.spec.js index 705741c..c4a40a1 100644 --- a/test/unit/serialization/array.spec.js +++ b/test/unit/serialization/array.spec.js @@ -8,15 +8,15 @@ describe('serialization', () => { beforeEach(() => { Book = attributes({ - name: String + name: String, })(class Book {}); User = attributes({ name: String, books: { type: Array, - itemType: Book - } + itemType: Book, + }, })(class User {}); }); @@ -24,16 +24,12 @@ describe('serialization', () => { it('include all data defined on schema', () => { const user = new User({ name: 'Something', - books: [ - new Book({ name: 'The Hobbit' }) - ] + books: [new Book({ name: 'The Hobbit' })], }); expect(user.toJSON()).to.eql({ name: 'Something', - books: [ - { name: 'The Hobbit' } - ] + books: [{ name: 'The Hobbit' }], }); }); }); @@ -45,8 +41,8 @@ describe('serialization', () => { books: [ new Book({ name: 'The Silmarillion' }), undefined, - new Book({ name: 'The Lord of the Rings' }) - ] + new Book({ name: 'The Lord of the Rings' }), + ], }); const serializedUser = user.toJSON(); @@ -56,8 +52,8 @@ describe('serialization', () => { books: [ { name: 'The Silmarillion' }, undefined, - { name: 'The Lord of the Rings' } - ] + { name: 'The Lord of the Rings' }, + ], }); expect(serializedUser).to.have.all.keys(['name', 'books']); diff --git a/test/unit/serialization/jsonStringifyCompatibility.js b/test/unit/serialization/jsonStringifyCompatibility.js index d6f8245..cd54a36 100644 --- a/test/unit/serialization/jsonStringifyCompatibility.js +++ b/test/unit/serialization/jsonStringifyCompatibility.js @@ -6,20 +6,20 @@ describe('JSON.stringify compatibility', () => { it('calls .toJSON() method', () => { const Location = attributes({ x: Number, - y: Number - })(class Location { }); + y: Number, + })(class Location {}); const User = attributes({ name: String, - location: Location - })(class User { }); + location: Location, + })(class User {}); const user = new User({ name: 'Some name', location: new Location({ x: 1, - y: 2 - }) + y: 2, + }), }); expect(JSON.parse(JSON.stringify(user))).to.eql(user.toJSON()); diff --git a/test/unit/serialization/nestedStructure.spec.js b/test/unit/serialization/nestedStructure.spec.js index 7c63020..061a43c 100644 --- a/test/unit/serialization/nestedStructure.spec.js +++ b/test/unit/serialization/nestedStructure.spec.js @@ -9,12 +9,12 @@ describe('serialization', () => { beforeEach(() => { Location = attributes({ longitude: Number, - latitude: Number + latitude: Number, })(class Location {}); User = attributes({ name: String, - location: Location + location: Location, })(class User {}); }); @@ -22,20 +22,20 @@ describe('serialization', () => { it('include all data defined on schema', () => { const location = new Location({ longitude: 123, - latitude: 321 + latitude: 321, }); const user = new User({ name: 'Something', - location + location, }); expect(user.toJSON()).to.eql({ name: 'Something', location: { longitude: 123, - latitude: 321 - } + latitude: 321, + }, }); }); }); @@ -43,7 +43,7 @@ describe('serialization', () => { context('when nested structure is missing', () => { it('does not set a key for missing structure', () => { const user = new User({ - name: 'Some name' + name: 'Some name', }); const serializedUser = user.toJSON(); @@ -59,12 +59,12 @@ describe('serialization', () => { context('when some attribute on nested structure is missing', () => { it('does not set a key for missing nested attribute', () => { const location = new Location({ - longitude: 123 + longitude: 123, }); const user = new User({ name: 'Name', - location + location, }); const serializedUser = user.toJSON(); @@ -72,8 +72,8 @@ describe('serialization', () => { expect(serializedUser).to.eql({ name: 'Name', location: { - longitude: 123 - } + longitude: 123, + }, }); expect(serializedUser.location).to.have.all.keys(['longitude']); @@ -97,14 +97,14 @@ describe('serialization', () => { friends: [ new CircularUser({ name: 'Friend 1', - favoriteBook: new CircularBook({ name: 'Book 1' }) + favoriteBook: new CircularBook({ name: 'Book 1' }), }), new CircularUser({ name: 'Friend 2', - favoriteBook: new CircularBook({ name: 'Book 2'}) - }) + favoriteBook: new CircularBook({ name: 'Book 2' }), + }), ], - favoriteBook: new CircularBook({ name: 'The Book'}) + favoriteBook: new CircularBook({ name: 'The Book' }), }); expect(user.toJSON()).to.eql({ @@ -112,14 +112,14 @@ describe('serialization', () => { friends: [ { name: 'Friend 1', - favoriteBook: { name: 'Book 1' } + favoriteBook: { name: 'Book 1' }, }, { name: 'Friend 2', - favoriteBook: { name: 'Book 2'} - } + favoriteBook: { name: 'Book 2' }, + }, ], - favoriteBook: { name: 'The Book' } + favoriteBook: { name: 'The Book' }, }); }); }); @@ -127,11 +127,11 @@ describe('serialization', () => { context('when nested structure is missing', () => { it('does not set a key for missing structure', () => { const user = new CircularUser({ - name: 'Something' + name: 'Something', }); expect(user.toJSON()).to.eql({ - name: 'Something' + name: 'Something', }); }); }); @@ -140,12 +140,12 @@ describe('serialization', () => { it('does not set a key for missing nested attribute', () => { const user = new CircularUser({ name: 'Something', - favoriteBook: new CircularBook({}) + favoriteBook: new CircularBook({}), }); expect(user.toJSON()).to.eql({ name: 'Something', - favoriteBook: {} + favoriteBook: {}, }); }); }); diff --git a/test/unit/serialization/structure.spec.js b/test/unit/serialization/structure.spec.js index fbf0f91..8b3d69e 100644 --- a/test/unit/serialization/structure.spec.js +++ b/test/unit/serialization/structure.spec.js @@ -8,7 +8,7 @@ describe('serialization', () => { beforeEach(() => { User = attributes({ name: String, - age: Number + age: Number, })(class User {}); }); @@ -16,12 +16,12 @@ describe('serialization', () => { it('include all data defined on schema', () => { const user = new User({ name: 'Something', - age: 42 + age: 42, }); expect(user.toJSON()).to.eql({ name: 'Something', - age: 42 + age: 42, }); }); }); @@ -30,7 +30,7 @@ describe('serialization', () => { it('does not set a key for missing attribute', () => { const user = new User({ name: 'Some name', - age: undefined + age: undefined, }); const serializedUser = user.toJSON(); @@ -43,7 +43,7 @@ describe('serialization', () => { }); }); - context('when attribute\'s value is null', () => { + context("when attribute's value is null", () => { var City; context('and is not nullable', () => { @@ -53,7 +53,7 @@ describe('serialization', () => { it('serializes with default value', () => { const city = new City({ - name: null + name: null, }); const serializedCity = city.toJSON(); @@ -68,8 +68,8 @@ describe('serialization', () => { City = attributes({ name: { type: String, - nullable: true - } + nullable: true, + }, })(class City {}); }); @@ -93,8 +93,8 @@ describe('serialization', () => { City = attributes({ country: { type: Country, - nullable: true - } + nullable: true, + }, })(class City {}); }); diff --git a/test/unit/subclassingStructureClass.spec.js b/test/unit/subclassingStructureClass.spec.js index 1eb91a6..cd81675 100644 --- a/test/unit/subclassingStructureClass.spec.js +++ b/test/unit/subclassingStructureClass.spec.js @@ -7,17 +7,19 @@ describe('subclassing a structure with a POJO class', () => { beforeEach(() => { User = attributes({ - name: String - })(class User { - constructor(attrs, userValue) { - this.userValue = userValue; - this.userStuff = 'User Stuff'; - } + name: String, + })( + class User { + constructor(attrs, userValue) { + this.userValue = userValue; + this.userStuff = 'User Stuff'; + } - userMethod() { - return 'I am a user'; + userMethod() { + return 'I am a user'; + } } - }); + ); Admin = class Admin extends User { constructor(attrs, userValue, otherValue) { @@ -34,7 +36,7 @@ describe('subclassing a structure with a POJO class', () => { describe('instantiating a structure subclass', () => { it('is instance of class and superclass', () => { const admin = new Admin({ - name: 'The Admin' + name: 'The Admin', }); expect(admin).to.be.instanceOf(Admin); @@ -43,7 +45,7 @@ describe('subclassing a structure with a POJO class', () => { it('has access to class and superclass methods', () => { const admin = new Admin({ - name: 'Me' + name: 'Me', }); expect(admin.userMethod()).to.equal('I am a user'); @@ -51,9 +53,13 @@ describe('subclassing a structure with a POJO class', () => { }); it('has access to class and superclass properties', () => { - const admin = new Admin({ - name: 'Me' - }, 'User Value', 'Admin Value'); + const admin = new Admin( + { + name: 'Me', + }, + 'User Value', + 'Admin Value' + ); expect(admin.userStuff).to.equal('User Stuff'); expect(admin.userValue).to.equal('User Value'); @@ -62,7 +68,7 @@ describe('subclassing a structure with a POJO class', () => { it('has attributes passed to constructor assigned to the object', () => { const admin = new Admin({ - name: 'Me' + name: 'Me', }); expect(admin.name).to.equal('Me'); @@ -71,7 +77,7 @@ describe('subclassing a structure with a POJO class', () => { it('ignores invalid attributes passed to constructor', () => { const admin = new Admin({ name: 'Myself', - invalid: 'I will be ignored' + invalid: 'I will be ignored', }); expect(admin.invalid).to.be.undefined; @@ -79,7 +85,7 @@ describe('subclassing a structure with a POJO class', () => { it('reflects instance attributes to #attributes', () => { const admin = new Admin({ - name: 'Self' + name: 'Self', }); expect(admin.name).to.equal('Self'); @@ -90,7 +96,7 @@ describe('subclassing a structure with a POJO class', () => { describe('updating an instance of structure subclass', () => { it('updates instance attribute value when assigned a new value', () => { const admin = new Admin({ - name: 'My name' + name: 'My name', }); admin.name = 'New name'; @@ -100,7 +106,7 @@ describe('subclassing a structure with a POJO class', () => { it('reflects new value assigned to attribute on #attributes', () => { const admin = new Admin({ - name: 'My name' + name: 'My name', }); admin.name = 'New name'; @@ -110,7 +116,7 @@ describe('subclassing a structure with a POJO class', () => { it('reflects new value assigned to #attributes on instance attribute', () => { const admin = new Admin({ - name: 'My name' + name: 'My name', }); admin.attributes.name = 'New name'; @@ -132,7 +138,7 @@ describe('subclassing a structure with a POJO class', () => { RawUser.staticProperty = 'I am a static property'; const UserStructure = attributes({ - name: String + name: String, })(RawUser); AdminStructure = class AdminStructure extends UserStructure { @@ -147,8 +153,12 @@ describe('subclassing a structure with a POJO class', () => { it('has access to static methods and properties', () => { expect(AdminStructure.staticMethod()).to.equal('I am on a static method'); expect(AdminStructure.staticProperty).to.equal('I am a static property'); - expect(AdminStructure.staticAdminMethod()).to.equal('I am also on a static method'); - expect(AdminStructure.staticAdminProperty).to.equal('I am also a static property'); + expect(AdminStructure.staticAdminMethod()).to.equal( + 'I am also on a static method' + ); + expect(AdminStructure.staticAdminProperty).to.equal( + 'I am also a static property' + ); }); }); }); @@ -162,23 +172,23 @@ describe('subclassing a structure with another structure', () => { name: String, uuid: { type: String, - default: () => 123 - } + default: () => 123, + }, })(class User {}); Admin = attributes({ level: Number, identifier: { type: String, - default: (instance) => `Admin-${instance.uuid}` - } + default: (instance) => `Admin-${instance.uuid}`, + }, })(class Admin extends User {}); }); it('uses the extended schema', () => { const admin = new Admin({ name: 'The admin', - level: 3 + level: 3, }); expect(admin.name).to.equal('The admin'); @@ -187,7 +197,7 @@ describe('subclassing a structure with another structure', () => { it('is instance of class and superclass', () => { const admin = new Admin({ - name: 'The Admin' + name: 'The Admin', }); expect(admin).to.be.instanceOf(Admin); @@ -195,22 +205,28 @@ describe('subclassing a structure with another structure', () => { }); context('default value', () => { - context('when subclass uses an attribute from superclass to generate a default value', () => { - context('when superclass uses default', () => { - it('allows to access it properly', () => { - const admin = new Admin(); - - expect(admin.identifier).to.equal('Admin-123'); + context( + 'when subclass uses an attribute from superclass to generate a default value', + () => { + context('when superclass uses default', () => { + it('allows to access it properly', () => { + const admin = new Admin(); + + expect(admin.identifier).to.equal('Admin-123'); + }); }); - }); - context('when a value is passed to superclass defaultable attribute', () => { - it('allows to access it properly', () => { - const admin = new Admin({ uuid: '321' }); + context( + 'when a value is passed to superclass defaultable attribute', + () => { + it('allows to access it properly', () => { + const admin = new Admin({ uuid: '321' }); - expect(admin.identifier).to.equal('Admin-321'); - }); - }); - }); + expect(admin.identifier).to.equal('Admin-321'); + }); + } + ); + } + ); }); }); diff --git a/test/unit/validation/array.spec.js b/test/unit/validation/array.spec.js index 09de158..a1cb9fb 100644 --- a/test/unit/validation/array.spec.js +++ b/test/unit/validation/array.spec.js @@ -1,5 +1,8 @@ const { attributes } = require('../../../src'); -const { assertValid, assertInvalid } = require('../../support/validationMatchers'); +const { + assertValid, + assertInvalid, +} = require('../../support/validationMatchers'); describe('validation', () => { describe('Array', () => { @@ -10,15 +13,15 @@ describe('validation', () => { User = attributes({ books: { type: Array, - itemType: String - } + itemType: String, + }, })(class User {}); }); context('when value is present', () => { it('is valid', () => { const user = new User({ - books: [] + books: [], }); assertValid(user); @@ -28,7 +31,7 @@ describe('validation', () => { context('when value is not present', () => { it('is valid', () => { const user = new User({ - books: undefined + books: undefined, }); assertValid(user); @@ -44,15 +47,15 @@ describe('validation', () => { books: { type: Array, itemType: String, - required: true - } + required: true, + }, })(class User {}); }); context('when value is present', () => { it('is valid', () => { const user = new User({ - books: [] + books: [], }); assertValid(user); @@ -62,7 +65,7 @@ describe('validation', () => { context('when value is not present', () => { it('is not valid and has errors set', () => { const user = new User({ - books: undefined + books: undefined, }); assertInvalid(user, 'books'); @@ -78,8 +81,8 @@ describe('validation', () => { books: { type: Array, itemType: String, - required: false - } + required: false, + }, })(class User {}); }); @@ -94,7 +97,7 @@ describe('validation', () => { context('when value is not present', () => { it('is valid', () => { const user = new User({ - books: undefined + books: undefined, }); assertValid(user, 'books'); @@ -111,15 +114,15 @@ describe('validation', () => { books: { type: Array, itemType: String, - sparse: false - } + sparse: false, + }, })(class User {}); }); context('when all items are defined', () => { it('is valid', () => { const user = new User({ - books: ['Poetic Edda', 'Prose Edda'] + books: ['Poetic Edda', 'Prose Edda'], }); assertValid(user); @@ -129,7 +132,7 @@ describe('validation', () => { context('when some item is undefined', () => { it('is not valid and has errors set', () => { const user = new User({ - books: ['The Lusiads', undefined] + books: ['The Lusiads', undefined], }); assertInvalid(user, 'books.1'); @@ -145,15 +148,15 @@ describe('validation', () => { books: { type: Array, itemType: String, - sparse: true - } + sparse: true, + }, })(class User {}); }); context('when all items are defined', () => { it('is valid', () => { const user = new User({ - books: ['Poetic Edda', 'Prose Edda'] + books: ['Poetic Edda', 'Prose Edda'], }); assertValid(user); @@ -163,7 +166,7 @@ describe('validation', () => { context('when some item is undefined', () => { it('is valid', () => { const user = new User({ - books: ['The Lusiads', undefined] + books: ['The Lusiads', undefined], }); assertValid(user); @@ -180,16 +183,16 @@ describe('validation', () => { Book = attributes({ name: { type: String, - required: true - } + required: true, + }, })(class Book {}); User = attributes({ books: { type: Array, itemType: Book, - required: true - } + required: true, + }, })(class User {}); }); @@ -198,8 +201,8 @@ describe('validation', () => { const user = new User({ books: [ new Book({ name: 'The Silmarillion' }), - new Book({ name: 'The Lord of the Rings' }) - ] + new Book({ name: 'The Lord of the Rings' }), + ], }); assertValid(user); @@ -211,8 +214,8 @@ describe('validation', () => { const user = new User({ books: [ new Book({ name: 'The Hobbit' }), - new Book({ name: undefined }) - ] + new Book({ name: undefined }), + ], }); assertInvalid(user, 'books.1.name'); @@ -232,10 +235,10 @@ describe('validation', () => { const user = new CircularUser({ friends: [ new CircularUser({ - favoriteBook: {} - }) + favoriteBook: {}, + }), ], - favoriteBook: {} + favoriteBook: {}, }); assertValid(user); @@ -247,11 +250,11 @@ describe('validation', () => { const user = new CircularUser({ friends: [ new CircularUser({ - favoriteBook: {} + favoriteBook: {}, }), - new CircularUser() + new CircularUser(), ], - favoriteBook: {} + favoriteBook: {}, }); assertInvalid(user, 'friends.1.favoriteBook'); @@ -267,18 +270,15 @@ describe('validation', () => { books: { type: Array, itemType: String, - minLength: 2 - } + minLength: 2, + }, })(class User {}); }); context('when array has minimum length', () => { it('is valid', () => { const user = new User({ - books: [ - 'The Name of the Wind', - 'The Wise Man\'s Fear' - ] + books: ['The Name of the Wind', "The Wise Man's Fear"], }); assertValid(user); @@ -288,9 +288,7 @@ describe('validation', () => { context('when array has minimum length', () => { it('is not valid and has errors set', () => { const user = new User({ - books: [ - '1984' - ] + books: ['1984'], }); assertInvalid(user, 'books'); @@ -306,17 +304,15 @@ describe('validation', () => { books: { type: Array, itemType: String, - maxLength: 2 - } + maxLength: 2, + }, })(class User {}); }); context('when array has less than maximum length', () => { it('is valid', () => { const user = new User({ - books: [ - 'The Name of the Wind' - ] + books: ['The Name of the Wind'], }); assertValid(user); @@ -326,11 +322,7 @@ describe('validation', () => { context('when array has more than maximum length', () => { it('is not valid and has errors set', () => { const user = new User({ - books: [ - '1984', - 'The Game of Thrones', - 'Dragons of Ether' - ] + books: ['1984', 'The Game of Thrones', 'Dragons of Ether'], }); assertInvalid(user, 'books'); @@ -346,18 +338,15 @@ describe('validation', () => { books: { type: Array, itemType: String, - exactLength: 2 - } + exactLength: 2, + }, })(class User {}); }); context('when array has exactly the expected length', () => { it('is valid', () => { const user = new User({ - books: [ - 'The Gunslinger', - 'The Drawing of the Three' - ] + books: ['The Gunslinger', 'The Drawing of the Three'], }); assertValid(user); @@ -367,9 +356,7 @@ describe('validation', () => { context('when array has less than exact length', () => { it('is not valid and has errors set', () => { const user = new User({ - books: [ - 'The Wastelands' - ] + books: ['The Wastelands'], }); assertInvalid(user, 'books'); @@ -382,8 +369,8 @@ describe('validation', () => { books: [ 'Wizard and Glass', 'The Wind Through the Keyhole', - 'Wolves of the Calla' - ] + 'Wolves of the Calla', + ], }); assertInvalid(user, 'books'); diff --git a/test/unit/validation/boolean.spec.js b/test/unit/validation/boolean.spec.js index f3fbcf0..b6144e9 100644 --- a/test/unit/validation/boolean.spec.js +++ b/test/unit/validation/boolean.spec.js @@ -1,5 +1,8 @@ const { attributes } = require('../../../src'); -const { assertValid, assertInvalid } = require('../../support/validationMatchers'); +const { + assertValid, + assertInvalid, +} = require('../../support/validationMatchers'); describe('validation', () => { describe('Boolean', () => { @@ -9,19 +12,19 @@ describe('validation', () => { beforeEach(() => { User = attributes({ isAdmin: { - type: Boolean + type: Boolean, }, hasAccepted: { type: Boolean, - nullable: true - } + nullable: true, + }, })(class User {}); }); context('when value is present', () => { it('is valid', () => { const user = new User({ - isAdmin: true + isAdmin: true, }); assertValid(user); @@ -31,7 +34,7 @@ describe('validation', () => { context('when value is not present', () => { it('is valid with undefined', () => { const user = new User({ - isAdmin: undefined + isAdmin: undefined, }); assertValid(user); @@ -39,7 +42,7 @@ describe('validation', () => { it('is valid with null when nullable', () => { const user = new User({ - hasAccepted: null + hasAccepted: null, }); assertValid(user); @@ -55,14 +58,14 @@ describe('validation', () => { User = attributes({ isAdmin: { type: Boolean, - required: true - } + required: true, + }, })(class User {}); }); it('is valid', () => { const user = new User({ - isAdmin: true + isAdmin: true, }); assertValid(user); @@ -74,14 +77,14 @@ describe('validation', () => { User = attributes({ isAdmin: { type: Boolean, - required: true - } + required: true, + }, })(class User {}); }); it('is not valid and has errors set', () => { const user = new User({ - isAdmin: undefined + isAdmin: undefined, }); assertInvalid(user, 'isAdmin'); @@ -95,8 +98,8 @@ describe('validation', () => { isAdmin: { type: Boolean, required: true, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -113,8 +116,8 @@ describe('validation', () => { isAdmin: { type: Boolean, required: true, - nullable: false - } + nullable: false, + }, })(class User {}); }); @@ -134,8 +137,8 @@ describe('validation', () => { User = attributes({ isAdmin: { type: Boolean, - required: false - } + required: false, + }, })(class User {}); }); @@ -155,15 +158,15 @@ describe('validation', () => { User = attributes({ isAdmin: { type: Boolean, - equal: true - } + equal: true, + }, })(class User {}); }); context('when value is equal', () => { it('is valid', () => { const user = new User({ - isAdmin: true + isAdmin: true, }); assertValid(user); @@ -173,7 +176,7 @@ describe('validation', () => { context('when value is different', () => { it('is not valid and has errors set', () => { const user = new User({ - isAdmin: false + isAdmin: false, }); assertInvalid(user, 'isAdmin'); diff --git a/test/unit/validation/date.spec.js b/test/unit/validation/date.spec.js index b12e225..ca17da9 100644 --- a/test/unit/validation/date.spec.js +++ b/test/unit/validation/date.spec.js @@ -1,5 +1,8 @@ const { attributes } = require('../../../src'); -const { assertValid, assertInvalid } = require('../../support/validationMatchers'); +const { + assertValid, + assertInvalid, +} = require('../../support/validationMatchers'); describe('validation', () => { describe('Date', () => { @@ -9,19 +12,19 @@ describe('validation', () => { beforeEach(() => { User = attributes({ birth: { - type: Date + type: Date, }, death: { type: Date, - nullable: true - } + nullable: true, + }, })(class User {}); }); context('when value is present', () => { it('is valid', () => { const user = new User({ - birth: new Date() + birth: new Date(), }); assertValid(user); @@ -31,7 +34,7 @@ describe('validation', () => { context('when value is not present', () => { it('is valid with undefined', () => { const user = new User({ - birth: undefined + birth: undefined, }); assertValid(user); @@ -39,7 +42,7 @@ describe('validation', () => { it('is valid with null when nullable', () => { const user = new User({ - death: null + death: null, }); assertValid(user); @@ -55,14 +58,14 @@ describe('validation', () => { User = attributes({ birth: { type: Date, - required: true - } + required: true, + }, })(class User {}); }); it('is valid', () => { const user = new User({ - birth: new Date() + birth: new Date(), }); assertValid(user); @@ -74,14 +77,14 @@ describe('validation', () => { User = attributes({ birth: { type: Date, - required: true - } + required: true, + }, })(class User {}); }); it('is not valid and has errors set', () => { const user = new User({ - birth: undefined + birth: undefined, }); assertInvalid(user, 'birth'); @@ -95,8 +98,8 @@ describe('validation', () => { birth: { type: Date, required: true, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -113,8 +116,8 @@ describe('validation', () => { birth: { type: Date, required: true, - nullable: false - } + nullable: false, + }, })(class User {}); }); @@ -134,8 +137,8 @@ describe('validation', () => { User = attributes({ birth: { type: Date, - required: false - } + required: false, + }, })(class User {}); }); @@ -158,8 +161,8 @@ describe('validation', () => { User = attributes({ birth: { type: Date, - equal: now - } + equal: now, + }, })(class User {}); }); @@ -168,7 +171,7 @@ describe('validation', () => { const nowCopy = new Date(now.toISOString()); const user = new User({ - birth: nowCopy + birth: nowCopy, }); assertValid(user); @@ -180,7 +183,7 @@ describe('validation', () => { const otherTime = new Date(10); const user = new User({ - birth: otherTime + birth: otherTime, }); assertInvalid(user, 'birth'); @@ -201,8 +204,8 @@ describe('validation', () => { User = attributes({ birth: { type: Date, - max: now - } + max: now, + }, })(class User {}); }); @@ -211,7 +214,7 @@ describe('validation', () => { const before = new Date(10); const user = new User({ - birth: before + birth: before, }); assertValid(user); @@ -223,7 +226,7 @@ describe('validation', () => { const after = new Date(); const user = new User({ - birth: after + birth: after, }); assertInvalid(user, 'birth'); @@ -243,11 +246,11 @@ describe('validation', () => { User = attributes({ createdAt: { type: Date, - max: { attr: 'updatedAt' } + max: { attr: 'updatedAt' }, }, updatedAt: { - type: Date - } + type: Date, + }, })(class User {}); }); @@ -257,7 +260,7 @@ describe('validation', () => { const user = new User({ createdAt: before, - updatedAt: now + updatedAt: now, }); assertValid(user); @@ -270,7 +273,7 @@ describe('validation', () => { const user = new User({ createdAt: after, - updatedAt: now + updatedAt: now, }); assertInvalid(user, 'createdAt'); @@ -288,8 +291,8 @@ describe('validation', () => { User = attributes({ birth: { type: Date, - min: now - } + min: now, + }, })(class User {}); }); @@ -298,7 +301,7 @@ describe('validation', () => { const after = new Date(); const user = new User({ - birth: after + birth: after, }); assertValid(user); @@ -310,7 +313,7 @@ describe('validation', () => { const before = new Date(10); const user = new User({ - birth: before + birth: before, }); assertInvalid(user, 'birth'); diff --git a/test/unit/validation/nestedPojo.spec.js b/test/unit/validation/nestedPojo.spec.js index f00cfc1..c4d3a8e 100644 --- a/test/unit/validation/nestedPojo.spec.js +++ b/test/unit/validation/nestedPojo.spec.js @@ -1,5 +1,8 @@ const { attributes } = require('../../../src'); -const { assertValid, assertInvalid } = require('../../support/validationMatchers'); +const { + assertValid, + assertInvalid, +} = require('../../support/validationMatchers'); describe('validation', () => { describe('Nested with POJO class', () => { @@ -12,19 +15,19 @@ describe('validation', () => { User = attributes({ lastLocation: { - type: Location + type: Location, }, nextLocation: { type: Location, - nullable: true - } + nullable: true, + }, })(class User {}); }); context('when value is present', () => { it('is valid', () => { const user = new User({ - lastLocation: new Location() + lastLocation: new Location(), }); assertValid(user); @@ -34,7 +37,7 @@ describe('validation', () => { context('when value is not present', () => { it('is valid with undefined', () => { const user = new User({ - lastLocation: undefined + lastLocation: undefined, }); assertValid(user); @@ -42,7 +45,7 @@ describe('validation', () => { it('is valid with null when nullable', () => { const user = new User({ - nextLocation: null + nextLocation: null, }); assertValid(user); @@ -54,21 +57,21 @@ describe('validation', () => { var Location; var User; - beforeEach(() => Location = class Location {}); + beforeEach(() => (Location = class Location {})); context('when value is present', () => { beforeEach(() => { User = attributes({ lastLocation: { type: Location, - required: true - } + required: true, + }, })(class User {}); }); it('is valid', () => { const user = new User({ - lastLocation: new Location() + lastLocation: new Location(), }); assertValid(user); @@ -80,14 +83,14 @@ describe('validation', () => { User = attributes({ lastLocation: { type: Location, - required: true - } + required: true, + }, })(class User {}); }); it('is not valid and has errors set', () => { const user = new User({ - lastLocation: undefined + lastLocation: undefined, }); assertInvalid(user, 'lastLocation'); @@ -101,8 +104,8 @@ describe('validation', () => { lastLocation: { type: Location, required: true, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -119,8 +122,8 @@ describe('validation', () => { lastLocation: { type: Location, required: true, - nullable: false - } + nullable: false, + }, })(class User {}); }); @@ -143,8 +146,8 @@ describe('validation', () => { User = attributes({ lastLocation: { type: Location, - required: false - } + required: false, + }, })(class User {}); }); diff --git a/test/unit/validation/nestedStructure.spec.js b/test/unit/validation/nestedStructure.spec.js index 4bf0ce9..315ddef 100644 --- a/test/unit/validation/nestedStructure.spec.js +++ b/test/unit/validation/nestedStructure.spec.js @@ -1,5 +1,8 @@ const { attributes } = require('../../../src'); -const { assertValid, assertInvalid } = require('../../support/validationMatchers'); +const { + assertValid, + assertInvalid, +} = require('../../support/validationMatchers'); describe('validation', () => { describe('Nested with structure class', () => { @@ -10,24 +13,24 @@ describe('validation', () => { beforeEach(() => { Location = attributes({ x: { - type: Number + type: Number, }, y: { - type: Number - } + type: Number, + }, })(class Location {}); User = attributes({ lastLocation: { - type: Location - } + type: Location, + }, })(class User {}); }); context('when value is present', () => { it('is valid', () => { const user = new User({ - lastLocation: new Location() + lastLocation: new Location(), }); assertValid(user); @@ -37,7 +40,7 @@ describe('validation', () => { context('when value is not present', () => { it('is valid', () => { const user = new User({ - lastLocation: undefined + lastLocation: undefined, }); assertValid(user); @@ -52,7 +55,7 @@ describe('validation', () => { beforeEach(() => { Location = attributes({ x: { type: Number }, - y: { type: Number } + y: { type: Number }, })(class Location {}); }); @@ -61,14 +64,14 @@ describe('validation', () => { User = attributes({ lastLocation: { type: Location, - required: true - } + required: true, + }, })(class User {}); }); it('is valid', () => { const user = new User({ - lastLocation: new Location() + lastLocation: new Location(), }); assertValid(user); @@ -80,14 +83,14 @@ describe('validation', () => { User = attributes({ lastLocation: { type: Location, - required: true - } + required: true, + }, })(class User {}); }); it('is not valid and has errors set', () => { const user = new User({ - lastLocation: undefined + lastLocation: undefined, }); assertInvalid(user, 'lastLocation'); @@ -101,8 +104,8 @@ describe('validation', () => { lastLocation: { type: Location, required: true, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -119,8 +122,8 @@ describe('validation', () => { lastLocation: { type: Location, required: true, - nullable: false - } + nullable: false, + }, })(class User {}); }); @@ -140,18 +143,18 @@ describe('validation', () => { beforeEach(() => { Location = attributes({ x: { - type: Number + type: Number, }, y: { - type: Number - } + type: Number, + }, })(class Location {}); User = attributes({ lastLocation: { type: Location, - required: false - } + required: false, + }, })(class User {}); }); @@ -172,17 +175,17 @@ describe('validation', () => { beforeEach(() => { Location = attributes({ x: { type: Number, required: true }, - y: { type: Number, required: true } + y: { type: Number, required: true }, })(class Location {}); - User = attributes( - { lastLocation: { type: Location, required: true } } - )(class User {}); + User = attributes({ + lastLocation: { type: Location, required: true }, + })(class User {}); }); it('is valid', () => { const user = new User({ - lastLocation: new Location({ x: 1, y: 2 }) + lastLocation: new Location({ x: 1, y: 2 }), }); assertValid(user); @@ -193,17 +196,17 @@ describe('validation', () => { beforeEach(() => { Location = attributes({ x: { type: Number, required: true }, - y: { type: Number, required: true } + y: { type: Number, required: true }, })(class Location {}); - User = attributes( - { lastLocation: { type: Location, required: true } } - )(class User {}); + User = attributes({ + lastLocation: { type: Location, required: true }, + })(class User {}); }); it('is not valid and has errors set', () => { const user = new User({ - lastLocation: new Location({ x: 1, y: undefined }) + lastLocation: new Location({ x: 1, y: undefined }), }); assertInvalid(user, 'lastLocation.y'); @@ -215,17 +218,17 @@ describe('validation', () => { beforeEach(() => { Location = attributes({ x: { type: Number, required: true }, - y: { type: Number, required: true, nullable: true } + y: { type: Number, required: true, nullable: true }, })(class Location {}); - User = attributes( - { lastLocation: { type: Location, required: true } } - )(class User {}); + User = attributes({ + lastLocation: { type: Location, required: true }, + })(class User {}); }); it('is valid', () => { const user = new User({ - lastLocation: new Location({ x: 1, y: null }) + lastLocation: new Location({ x: 1, y: null }), }); assertValid(user); @@ -236,17 +239,17 @@ describe('validation', () => { beforeEach(() => { Location = attributes({ x: { type: Number, required: true }, - y: { type: Number, required: true, nullable: false } + y: { type: Number, required: true, nullable: false }, })(class Location {}); - User = attributes( - { lastLocation: { type: Location, required: true } } - )(class User {}); + User = attributes({ + lastLocation: { type: Location, required: true }, + })(class User {}); }); it('is not valid and has errors set', () => { const user = new User({ - lastLocation: new Location({ x: 1, y: null }) + lastLocation: new Location({ x: 1, y: null }), }); assertInvalid(user, 'lastLocation.y'); @@ -270,7 +273,7 @@ describe('validation', () => { it('is valid', () => { const user = new CircularUser({ friends: [], - favoriteBook: {} + favoriteBook: {}, }); assertValid(user); @@ -280,7 +283,7 @@ describe('validation', () => { context('when value is not present', () => { it('is valid', () => { const user = new CircularUser({ - favoriteBook: {} + favoriteBook: {}, }); assertValid(user); @@ -292,7 +295,7 @@ describe('validation', () => { context('when value is present', () => { it('is valid', () => { const user = new CircularUser({ - favoriteBook: {} + favoriteBook: {}, }); assertValid(user); @@ -313,8 +316,8 @@ describe('validation', () => { it('is valid', () => { const book = new CircularBook({ owner: { - favoriteBook: new CircularBook() - } + favoriteBook: new CircularBook(), + }, }); assertValid(book); @@ -324,7 +327,7 @@ describe('validation', () => { context('when value is not present', () => { it('is invalid', () => { const book = new CircularBook({ - owner: new CircularUser() + owner: new CircularUser(), }); assertInvalid(book, 'owner.favoriteBook'); diff --git a/test/unit/validation/number.spec.js b/test/unit/validation/number.spec.js index 40828b4..4b9ed1e 100644 --- a/test/unit/validation/number.spec.js +++ b/test/unit/validation/number.spec.js @@ -1,5 +1,8 @@ const { attributes } = require('../../../src'); -const { assertValid, assertInvalid } = require('../../support/validationMatchers'); +const { + assertValid, + assertInvalid, +} = require('../../support/validationMatchers'); describe('validation', () => { describe('Number', () => { @@ -9,19 +12,19 @@ describe('validation', () => { beforeEach(() => { User = attributes({ age: { - type: Number + type: Number, }, earnings: { type: Number, nullable: true, - } + }, })(class User {}); }); context('when value is present', () => { it('is valid', () => { const user = new User({ - age: 42 + age: 42, }); assertValid(user); @@ -31,7 +34,7 @@ describe('validation', () => { context('when value is not present', () => { it('is valid with undefined', () => { const user = new User({ - age: undefined + age: undefined, }); assertValid(user); @@ -39,7 +42,7 @@ describe('validation', () => { it('is valid with null when nullable', () => { const user = new User({ - earnings: null + earnings: null, }); assertValid(user); @@ -55,14 +58,14 @@ describe('validation', () => { User = attributes({ age: { type: Number, - required: true - } + required: true, + }, })(class User {}); }); it('is valid', () => { const user = new User({ - age: 42 + age: 42, }); assertValid(user); @@ -74,14 +77,14 @@ describe('validation', () => { User = attributes({ age: { type: Number, - required: true - } + required: true, + }, })(class User {}); }); it('is not valid and has errors set', () => { const user = new User({ - age: undefined + age: undefined, }); assertInvalid(user, 'age'); @@ -95,8 +98,8 @@ describe('validation', () => { age: { type: Number, required: true, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -113,8 +116,8 @@ describe('validation', () => { age: { type: Number, required: true, - nullable: false - } + nullable: false, + }, })(class User {}); }); @@ -134,8 +137,8 @@ describe('validation', () => { User = attributes({ age: { type: Number, - required: false - } + required: false, + }, })(class User {}); }); @@ -156,15 +159,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - equal: 2 - } + equal: 2, + }, })(class User {}); }); context('when value is equal', () => { it('is valid', () => { const user = new User({ - age: 2 + age: 2, }); assertValid(user); @@ -174,7 +177,7 @@ describe('validation', () => { context('when value is different', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 1 + age: 1, }); assertInvalid(user, 'age'); @@ -188,12 +191,12 @@ describe('validation', () => { beforeEach(() => { User = attributes({ startAge: { - type: Number + type: Number, }, currentAge: { type: Number, - equal: [{ attr: 'startAge' }, 3] - } + equal: [{ attr: 'startAge' }, 3], + }, })(class User {}); }); @@ -201,7 +204,7 @@ describe('validation', () => { it('is valid', () => { const user = new User({ startAge: 2, - currentAge: 2 + currentAge: 2, }); assertValid(user); @@ -212,7 +215,7 @@ describe('validation', () => { it('is valid', () => { const user = new User({ startAge: 2, - currentAge: 3 + currentAge: 3, }); assertValid(user); @@ -223,7 +226,7 @@ describe('validation', () => { it('is not valid and has errors set', () => { const user = new User({ startAge: 1, - currentAge: 2 + currentAge: 2, }); assertInvalid(user, 'currentAge'); @@ -237,12 +240,12 @@ describe('validation', () => { beforeEach(() => { User = attributes({ startAge: { - type: Number + type: Number, }, currentAge: { type: Number, - equal: { attr: 'startAge' } - } + equal: { attr: 'startAge' }, + }, })(class User {}); }); @@ -250,7 +253,7 @@ describe('validation', () => { it('is valid', () => { const user = new User({ startAge: 2, - currentAge: 2 + currentAge: 2, }); assertValid(user); @@ -261,7 +264,7 @@ describe('validation', () => { it('is not valid and has errors set', () => { const user = new User({ startAge: 1, - currentAge: 2 + currentAge: 2, }); assertInvalid(user, 'currentAge'); @@ -278,15 +281,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - min: 2 - } + min: 2, + }, })(class User {}); }); context('when value is equal to min', () => { it('is valid', () => { const user = new User({ - age: 2 + age: 2, }); assertValid(user); @@ -296,7 +299,7 @@ describe('validation', () => { context('when value is greater than min', () => { it('is valid', () => { const user = new User({ - age: 3 + age: 3, }); assertValid(user); @@ -306,7 +309,7 @@ describe('validation', () => { context('when value is less than min', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 1 + age: 1, }); assertInvalid(user, 'age'); @@ -320,12 +323,12 @@ describe('validation', () => { beforeEach(() => { User = attributes({ startAge: { - type: Number + type: Number, }, currentAge: { type: Number, - min: { attr: 'startAge' } - } + min: { attr: 'startAge' }, + }, })(class User {}); }); @@ -333,7 +336,7 @@ describe('validation', () => { it('is valid', () => { const user = new User({ startAge: 2, - currentAge: 2 + currentAge: 2, }); assertValid(user); @@ -344,7 +347,7 @@ describe('validation', () => { it('is valid', () => { const user = new User({ startAge: 2, - currentAge: 3 + currentAge: 3, }); assertValid(user); @@ -355,7 +358,7 @@ describe('validation', () => { it('is not valid and has errors set', () => { const user = new User({ startAge: 3, - currentAge: 2 + currentAge: 2, }); assertInvalid(user, 'currentAge'); @@ -371,15 +374,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - greater: 2 - } + greater: 2, + }, })(class User {}); }); context('when value is equal to greater', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 2 + age: 2, }); assertInvalid(user, 'age'); @@ -389,7 +392,7 @@ describe('validation', () => { context('when value is greater than greater', () => { it('is valid', () => { const user = new User({ - age: 3 + age: 3, }); assertValid(user); @@ -399,7 +402,7 @@ describe('validation', () => { context('when value is less than greater', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 1 + age: 1, }); assertInvalid(user, 'age'); @@ -414,15 +417,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - max: 2 - } + max: 2, + }, })(class User {}); }); context('when value is equal to max', () => { it('is valid', () => { const user = new User({ - age: 2 + age: 2, }); assertValid(user); @@ -432,7 +435,7 @@ describe('validation', () => { context('when value is less than max', () => { it('is valid', () => { const user = new User({ - age: 1 + age: 1, }); assertValid(user); @@ -442,7 +445,7 @@ describe('validation', () => { context('when value is greater than max', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 3 + age: 3, }); assertInvalid(user, 'age'); @@ -457,15 +460,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - less: 2 - } + less: 2, + }, })(class User {}); }); context('when value is equal to less', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 2 + age: 2, }); assertInvalid(user, 'age'); @@ -475,7 +478,7 @@ describe('validation', () => { context('when value is less than less', () => { it('is valid', () => { const user = new User({ - age: 1 + age: 1, }); assertValid(user); @@ -485,7 +488,7 @@ describe('validation', () => { context('when value is greater than less', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 3 + age: 3, }); assertInvalid(user, 'age'); @@ -500,15 +503,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - integer: true - } + integer: true, + }, })(class User {}); }); context('when value is an integer', () => { it('is valid', () => { const user = new User({ - age: 42 + age: 42, }); assertValid(user); @@ -518,7 +521,7 @@ describe('validation', () => { context('when value is not an integer', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 4.2 + age: 4.2, }); assertInvalid(user, 'age'); @@ -533,15 +536,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - precision: 2 - } + precision: 2, + }, })(class User {}); }); context('when value has less than precision decimal places', () => { it('is valid', () => { const user = new User({ - age: 4.20 + age: 4.2, }); assertValid(user); @@ -551,7 +554,7 @@ describe('validation', () => { context('when value has more than precision decimal places', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 0.042 + age: 0.042, }); assertInvalid(user, 'age'); @@ -566,15 +569,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - multiple: 3 - } + multiple: 3, + }, })(class User {}); }); context('when value is multiple of given value', () => { it('is valid', () => { const user = new User({ - age: 6 + age: 6, }); assertValid(user); @@ -584,7 +587,7 @@ describe('validation', () => { context('when value is not multiple of given value', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 7 + age: 7, }); assertInvalid(user, 'age'); @@ -599,15 +602,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - positive: true - } + positive: true, + }, })(class User {}); }); context('when value is positive', () => { it('is valid', () => { const user = new User({ - age: 1 + age: 1, }); assertValid(user); @@ -617,7 +620,7 @@ describe('validation', () => { context('when value is zero', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 0 + age: 0, }); assertInvalid(user, 'age'); @@ -627,7 +630,7 @@ describe('validation', () => { context('when value is negative', () => { it('is not valid and has errors set', () => { const user = new User({ - age: -1 + age: -1, }); assertInvalid(user, 'age'); @@ -642,15 +645,15 @@ describe('validation', () => { User = attributes({ age: { type: Number, - negative: true - } + negative: true, + }, })(class User {}); }); context('when value is negative', () => { it('is valid', () => { const user = new User({ - age: -1 + age: -1, }); assertValid(user); @@ -660,7 +663,7 @@ describe('validation', () => { context('when value is zero', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 0 + age: 0, }); assertInvalid(user, 'age'); @@ -670,7 +673,7 @@ describe('validation', () => { context('when value is positive', () => { it('is not valid and has errors set', () => { const user = new User({ - age: 1 + age: 1, }); assertInvalid(user, 'age'); diff --git a/test/unit/validation/staticMethod.spec.js b/test/unit/validation/staticMethod.spec.js index 567aeb8..4d5f3e8 100644 --- a/test/unit/validation/staticMethod.spec.js +++ b/test/unit/validation/staticMethod.spec.js @@ -11,29 +11,29 @@ describe('validation', () => { name: { type: String, required: true, - nullable: true - } - })(class Book { }); + nullable: true, + }, + })(class Book {}); User = attributes({ name: { type: String, required: true, - nullable: false + nullable: false, }, age: { type: Number, - min: 21 + min: 21, }, - favoriteBook: Book - })(class User { }); + favoriteBook: Book, + })(class User {}); }); context('when required attributes are present', () => { it('returns valid as true and no errors', () => { const { valid, errors } = User.validate({ name: 'The name', - age: 25 + age: 25, }); expect(valid).to.be.true; @@ -44,7 +44,7 @@ describe('validation', () => { context('when required attributes are absent', () => { it('returns valid as false and array of errors', () => { const { valid, errors } = User.validate({ - age: 10 + age: 10, }); expect(valid).to.be.false; @@ -84,8 +84,8 @@ describe('validation', () => { name: 'some name', age: 25, favoriteBook: { - name: 'The Lord of the Rings' - } + name: 'The Lord of the Rings', + }, }); expect(valid).to.be.true; @@ -98,7 +98,7 @@ describe('validation', () => { const { valid, errors } = User.validate({ name: 'some name', age: 25, - favoriteBook: {} + favoriteBook: {}, }); expect(valid).to.be.false; @@ -113,7 +113,7 @@ describe('validation', () => { const { valid, errors } = User.validate({ name: 'some name', age: 25, - favoriteBook: { name: null } + favoriteBook: { name: null }, }); expect(valid).to.be.true; @@ -125,12 +125,12 @@ describe('validation', () => { var Test; before(() => { - Test = attributes({ user: User })(class Test { }); + Test = attributes({ user: User })(class Test {}); }); it('returns valid as false and an array of errors', () => { const { valid, errors } = Test.validate({ - user: { name: null } + user: { name: null }, }); expect(valid).to.be.false; @@ -146,7 +146,7 @@ describe('validation', () => { it('returns valid as true and no errors', () => { const user = new User({ name: 'Something', - age: 21 + age: 21, }); const { valid, errors } = User.validate(user); @@ -159,7 +159,7 @@ describe('validation', () => { context('when required attributes are absent or invalid', () => { it('returns valid as false and array of errors', () => { const user = new User({ - age: 10 + age: 10, }); const { valid, errors } = User.validate(user); @@ -177,7 +177,7 @@ describe('validation', () => { const user = new User({ name: 'Something', age: 21, - favoriteBook: new Book({ name: null }) + favoriteBook: new Book({ name: null }), }); const { valid, errors } = User.validate(user); @@ -191,12 +191,12 @@ describe('validation', () => { var Test; before(() => { - Test = attributes({ user: User })(class Test { }); + Test = attributes({ user: User })(class Test {}); }); it('returns valid as false and an array of errors', () => { const test = new Test({ - user: { name: null } + user: { name: null }, }); const { valid, errors } = Test.validate(test); diff --git a/test/unit/validation/string.spec.js b/test/unit/validation/string.spec.js index 08dfd2d..5f23e7f 100644 --- a/test/unit/validation/string.spec.js +++ b/test/unit/validation/string.spec.js @@ -1,5 +1,8 @@ const { attributes } = require('../../../src'); -const { assertValid, assertInvalid } = require('../../support/validationMatchers'); +const { + assertValid, + assertInvalid, +} = require('../../support/validationMatchers'); describe('validation', () => { describe('String', () => { @@ -9,19 +12,19 @@ describe('validation', () => { beforeEach(() => { User = attributes({ name: { - type: String + type: String, }, fatherName: { type: String, - nullable: true - } + nullable: true, + }, })(class User {}); }); context('when value is present', () => { it('is valid', () => { const user = new User({ - name: 'Some name' + name: 'Some name', }); assertValid(user); @@ -31,7 +34,7 @@ describe('validation', () => { context('when value is not present', () => { it('is valid with undefined', () => { const user = new User({ - name: undefined + name: undefined, }); assertValid(user); @@ -39,7 +42,7 @@ describe('validation', () => { it('is valid with null when nullable', () => { const user = new User({ - fatherName: null + fatherName: null, }); assertValid(user); @@ -55,14 +58,14 @@ describe('validation', () => { User = attributes({ name: { type: String, - required: true - } + required: true, + }, })(class User {}); }); it('is valid', () => { const user = new User({ - name: 'Some name' + name: 'Some name', }); assertValid(user); @@ -74,14 +77,14 @@ describe('validation', () => { User = attributes({ name: { type: String, - required: true - } + required: true, + }, })(class User {}); }); it('is not valid and has errors set', () => { const user = new User({ - name: undefined + name: undefined, }); assertInvalid(user, 'name'); @@ -95,8 +98,8 @@ describe('validation', () => { name: { type: String, required: true, - nullable: true - } + nullable: true, + }, })(class User {}); }); @@ -113,8 +116,8 @@ describe('validation', () => { name: { type: String, required: true, - nullable: false - } + nullable: false, + }, })(class User {}); }); @@ -134,15 +137,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - required: false - } + required: false, + }, })(class User {}); }); context('when value is not present', () => { it('is valid', () => { const user = new User({ - name: undefined + name: undefined, }); assertValid(user); @@ -157,15 +160,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - equal: 'Something' - } + equal: 'Something', + }, })(class User {}); }); context('when value is equal', () => { it('is valid', () => { const user = new User({ - name: 'Something' + name: 'Something', }); assertValid(user); @@ -175,7 +178,7 @@ describe('validation', () => { context('when value is different', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'Another thing' + name: 'Another thing', }); assertInvalid(user, 'name'); @@ -191,15 +194,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - empty: true - } + empty: true, + }, })(class User {}); }); context('when value is not empty', () => { it('is valid', () => { const user = new User({ - name: 'Some name' + name: 'Some name', }); assertValid(user); @@ -209,7 +212,7 @@ describe('validation', () => { context('when value is empty', () => { it('is valid', () => { const user = new User({ - name: '' + name: '', }); assertValid(user); @@ -224,15 +227,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - empty: false - } + empty: false, + }, })(class User {}); }); context('when value is not empty', () => { it('is valid', () => { const user = new User({ - name: 'Some name' + name: 'Some name', }); assertValid(user); @@ -242,7 +245,7 @@ describe('validation', () => { context('when value is empty', () => { it('is not valid and has errors set', () => { const user = new User({ - name: '' + name: '', }); assertInvalid(user, 'name'); @@ -258,15 +261,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - minLength: 3 - } + minLength: 3, + }, })(class User {}); }); context('when value has minimum length', () => { it('is valid', () => { const user = new User({ - name: 'Some name' + name: 'Some name', }); assertValid(user); @@ -276,7 +279,7 @@ describe('validation', () => { context('when value is shorter than minimum length', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'Hi' + name: 'Hi', }); assertInvalid(user, 'name'); @@ -291,15 +294,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - maxLength: 4 - } + maxLength: 4, + }, })(class User {}); }); context('when value has maximum length', () => { it('is valid', () => { const user = new User({ - name: 'Some' + name: 'Some', }); assertValid(user); @@ -309,7 +312,7 @@ describe('validation', () => { context('when value is longer than maximum length', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'Some name' + name: 'Some name', }); assertInvalid(user, 'name'); @@ -324,15 +327,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - exactLength: 4 - } + exactLength: 4, + }, })(class User {}); }); context('when value has exact length', () => { it('is valid', () => { const user = new User({ - name: 'Some' + name: 'Some', }); assertValid(user); @@ -342,7 +345,7 @@ describe('validation', () => { context('when value is longer than exact length', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'Some name' + name: 'Some name', }); assertInvalid(user, 'name'); @@ -352,7 +355,7 @@ describe('validation', () => { context('when value is shorter than exact length', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'Hi' + name: 'Hi', }); assertInvalid(user, 'name'); @@ -367,15 +370,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - regex: /\w\d/ - } + regex: /\w\d/, + }, })(class User {}); }); context('when value matches the regex', () => { it('is valid', () => { const user = new User({ - name: 'A1' + name: 'A1', }); assertValid(user); @@ -385,7 +388,7 @@ describe('validation', () => { context('when value does not match the regex', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'Something' + name: 'Something', }); assertInvalid(user, 'name'); @@ -400,15 +403,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - alphanumeric: true - } + alphanumeric: true, + }, })(class User {}); }); context('when value is alphanumeric', () => { it('is valid', () => { const user = new User({ - name: 'A1B2' + name: 'A1B2', }); assertValid(user); @@ -418,7 +421,7 @@ describe('validation', () => { context('when value is not alphanumeric', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'No alphanumeric $ string' + name: 'No alphanumeric $ string', }); assertInvalid(user, 'name'); @@ -433,15 +436,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - lowerCase: true - } + lowerCase: true, + }, })(class User {}); }); context('when value is lower cased', () => { it('is valid', () => { const user = new User({ - name: 'abc' + name: 'abc', }); assertValid(user); @@ -451,7 +454,7 @@ describe('validation', () => { context('when value has some upper case character', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'Abc' + name: 'Abc', }); assertInvalid(user, 'name'); @@ -466,15 +469,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - upperCase: true - } + upperCase: true, + }, })(class User {}); }); context('when value is upper cased', () => { it('is valid', () => { const user = new User({ - name: 'ABC' + name: 'ABC', }); assertValid(user); @@ -484,7 +487,7 @@ describe('validation', () => { context('when value has some lower case character', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'Abc' + name: 'Abc', }); assertInvalid(user, 'name'); @@ -499,15 +502,15 @@ describe('validation', () => { User = attributes({ name: { type: String, - email: true - } + email: true, + }, })(class User {}); }); context('when value is a valid email', () => { it('is valid', () => { const user = new User({ - name: 'name@host.com' + name: 'name@host.com', }); assertValid(user); @@ -517,7 +520,7 @@ describe('validation', () => { context('when value is shorter than minimum length', () => { it('is not valid and has errors set', () => { const user = new User({ - name: 'Not a valid email' + name: 'Not a valid email', }); assertInvalid(user, 'name'); @@ -533,15 +536,15 @@ describe('validation', () => { User = attributes({ id: { type: String, - guid: true - } + guid: true, + }, })(class User {}); }); context('when value is a valid guid', () => { it('is valid', () => { const user = new User({ - id: '759535af-3314-4ace-81b9-a519c29d0e17' + id: '759535af-3314-4ace-81b9-a519c29d0e17', }); assertValid(user); @@ -551,7 +554,7 @@ describe('validation', () => { context('when value is not a valid guid', () => { it('is not valid and has errors set', () => { const user = new User({ - id: 'Not a valid guid' + id: 'Not a valid guid', }); assertInvalid(user, 'id'); @@ -567,9 +570,9 @@ describe('validation', () => { id: { type: String, guid: { - version: ['uuidv4'] - } - } + version: ['uuidv4'], + }, + }, })(class User {}); }); @@ -578,7 +581,7 @@ describe('validation', () => { const uuidv4 = 'f35e1cf1-4ac9-4fbb-9c06-151dc8ff9107'; const user = new User({ - id: uuidv4 + id: uuidv4, }); assertValid(user); @@ -590,7 +593,7 @@ describe('validation', () => { const uuidv1 = 'c130564e-36d9-11e9-b210-d663bd873d93'; const user = new User({ - id: uuidv1 + id: uuidv1, }); assertInvalid(user, 'id'); diff --git a/test/unit/validation/structureSubclass.spec.js b/test/unit/validation/structureSubclass.spec.js index f2f2452..a70aa43 100644 --- a/test/unit/validation/structureSubclass.spec.js +++ b/test/unit/validation/structureSubclass.spec.js @@ -1,5 +1,8 @@ const { attributes } = require('../../../src'); -const { assertValid, assertInvalid } = require('../../support/validationMatchers'); +const { + assertValid, + assertInvalid, +} = require('../../support/validationMatchers'); describe('validation', () => { describe('structure subclass', () => { @@ -10,22 +13,22 @@ describe('validation', () => { User = attributes({ name: { type: String, - required: true - } + required: true, + }, })(class User {}); Admin = attributes({ level: { type: Number, - required: true - } + required: true, + }, })(class Admin extends User {}); }); context('with invalid superclass schema', () => { it('is invalid', () => { const admin = new Admin({ - level: 3 + level: 3, }); assertInvalid(admin, 'name'); @@ -35,7 +38,7 @@ describe('validation', () => { context('with invalid subclass schema', () => { it('is invalid', () => { const admin = new Admin({ - name: 'The admin' + name: 'The admin', }); assertInvalid(admin, 'level'); @@ -46,7 +49,7 @@ describe('validation', () => { it('is valid', () => { const admin = new Admin({ name: 'The admin', - level: 3 + level: 3, }); assertValid(admin); @@ -63,12 +66,12 @@ describe('validation', () => { name: { type: String, required: true, - nullable: true - } + nullable: true, + }, })(class Vehicle {}); Car = attributes({ - gearbox: String + gearbox: String, })(class Car extends Vehicle {}); }); @@ -85,12 +88,12 @@ describe('validation', () => { name: { type: String, required: true, - nullable: false - } + nullable: false, + }, })(class Vehicle {}); Car = attributes({ - gearbox: String + gearbox: String, })(class Car extends Vehicle {}); }); diff --git a/yarn.lock b/yarn.lock index 039a9dc..4cff476 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1266,6 +1266,13 @@ escodegen@1.8.x: optionalDependencies: source-map "~0.2.0" +eslint-config-prettier@^6.1.0: + version "6.1.0" + resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-6.1.0.tgz#e6f678ba367fbd1273998d5510f76f004e9dce7b" + integrity sha512-k9fny9sPjIBQ2ftFTesJV21Rg4R/7a7t7LCtZVrYQiHEp8Nnuk3EGaDmsKSAnsPj0BYcgB2zxzHa2NTkIxcOLg== + dependencies: + get-stdin "^6.0.0" + eslint-plugin-mocha@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/eslint-plugin-mocha/-/eslint-plugin-mocha-5.0.0.tgz#43946a7ecaf39039eb3ee20635ebd4cc19baf6dd" @@ -1596,6 +1603,11 @@ get-func-name@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41" +get-stdin@^6.0.0: + version "6.0.0" + resolved "https://registry.yarnpkg.com/get-stdin/-/get-stdin-6.0.0.tgz#9e09bf712b360ab9225e812048f71fde9c89657b" + integrity sha512-jp4tHawyV7+fkkSKyvjuLZswblUtz+SQKzSWnBbii16BuZksJlU1wuBYXY75r+duh/llF1ur6oNwi+2ZzjKZ7g== + getpass@^0.1.1: version "0.1.6" resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.6.tgz#283ffd9fc1256840875311c1b60e8c40187110e6" @@ -2700,6 +2712,11 @@ preserve@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b" +prettier@^1.18.2: + version "1.18.2" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea" + integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw== + private@^0.1.6: version "0.1.6" resolved "https://registry.yarnpkg.com/private/-/private-0.1.6.tgz#55c6a976d0f9bafb9924851350fe47b9b5fbb7c1"