Skip to content

Commit

Permalink
Rename items attribute to itemType
Browse files Browse the repository at this point in the history
  • Loading branch information
talyssonoc committed Dec 28, 2016
1 parent 659fa4c commit 065414e
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/schemaNormalization.js
Expand Up @@ -13,12 +13,12 @@ function normalizeAttribute(attribute, attributeName) {
throw new Error(`Attribute type must be a constructor: ${ attributeName }.`);
}

if(attribute.items) {
attribute.items = normalizeAttribute(attribute.items, 'items');
if(attribute.itemType) {
attribute.itemType = normalizeAttribute(attribute.itemType, 'itemType');
}

return Object.assign({}, attribute, {
coerce: coercionFor(attribute, attribute.items),
coerce: coercionFor(attribute, attribute.itemType),
validation: validationForAttribute(attribute)
});

Expand Down
2 changes: 1 addition & 1 deletion src/serialization.js
Expand Up @@ -17,7 +17,7 @@ function serialize(structure) {

let serializedValue;

if(schema[attrName].items && schema[attrName].items.type[SCHEMA] !== undefined) {
if(schema[attrName].itemType && schema[attrName].itemType.type[SCHEMA] !== undefined) {
serializedValue = attribute.map(serialize);
} else if(schema[attrName].type[SCHEMA] !== undefined) {
serializedValue = serialize(attribute);
Expand Down
4 changes: 2 additions & 2 deletions src/typeCoercion/array.js
@@ -1,4 +1,4 @@
module.exports = function arrayCoercionFor(typeDescriptor, itemsTypeDescriptor) {
module.exports = function arrayCoercionFor(typeDescriptor, itemTypeDescriptor) {
return function coerceArray(value) {
if(value === undefined) {
return;
Expand All @@ -15,7 +15,7 @@ module.exports = function arrayCoercionFor(typeDescriptor, itemsTypeDescriptor)
const coercedValue = new typeDescriptor.type();

for(let i = 0; i < value.length; i++) {
coercedValue.push(itemsTypeDescriptor.coerce(value[i]));
coercedValue.push(itemTypeDescriptor.coerce(value[i]));
}

return coercedValue;
Expand Down
6 changes: 3 additions & 3 deletions src/typeCoercion/index.js
Expand Up @@ -7,9 +7,9 @@ const types = [
require('./boolean')
];

function coercionFor(typeDescriptor, itemsTypeDescriptor) {
if(itemsTypeDescriptor) {
return arrayCoercionFor(typeDescriptor, itemsTypeDescriptor);
function coercionFor(typeDescriptor, itemTypeDescriptor) {
if(itemTypeDescriptor) {
return arrayCoercionFor(typeDescriptor, itemTypeDescriptor);
}

const coercion = types.find((c) => c.type === typeDescriptor.type);
Expand Down
4 changes: 2 additions & 2 deletions src/validation/array.js
Expand Up @@ -8,8 +8,8 @@ const joiMappings = [
['exactLength', 'length', true]
];

module.exports = function arrayValidation(typeDescriptor, itemsTypeDescriptor) {
var joiSchema = joi.array().items(itemsTypeDescriptor.validation);
module.exports = function arrayValidation(typeDescriptor, itemTypeDescriptor) {
var joiSchema = joi.array().items(itemTypeDescriptor.validation);
const canBeSparse = typeDescriptor.sparse === undefined || typeDescriptor.sparse;

joiSchema = joiSchema.sparse(canBeSparse);
Expand Down
4 changes: 2 additions & 2 deletions src/validation/index.js
Expand Up @@ -11,8 +11,8 @@ const nestedValidation = require('./nested');
const arrayValidation = require('./array');

function validationForAttribute(typeDescriptor) {
if(typeDescriptor.items !== undefined) {
return arrayValidation(typeDescriptor, typeDescriptor.items);
if(typeDescriptor.itemType !== undefined) {
return arrayValidation(typeDescriptor, typeDescriptor.itemType);
}

const validation = validations.find((v) => v.type === typeDescriptor.type);
Expand Down
18 changes: 9 additions & 9 deletions test/unit/schemaNormalization.spec.js
Expand Up @@ -62,30 +62,30 @@ describe('schema normalization', () => {
});
});

context('when attribute has items type', () => {
context('when items type is an object with type attribute', () => {
it('does not change the items type object', () => {
context('when attribute has itemType', () => {
context('when itemType is an object with type attribute', () => {
it('does not change the itemType object', () => {
const schema = {
name: {
type: Array,
items: { type: String }
itemType: { type: String }
}
};

expect(normalizeSchema(schema).name.items.type).to.eql(String);
expect(normalizeSchema(schema).name.itemType.type).to.eql(String);
});
});

context('when items type is an constructor', () => {
it('normalizes items type to an object with type field being equal to passed constructor', () => {
context('when itemType is an constructor', () => {
it('normalizes itemType to an object with type field being equal to passed constructor', () => {
const schema = {
name: {
type: Array,
items: String
itemType: String
}
};

expect(normalizeSchema(schema).name.items.type).to.eql(String);
expect(normalizeSchema(schema).name.itemType.type).to.eql(String);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/serialization/array.spec.js
Expand Up @@ -12,7 +12,7 @@ describe('serialization', () => {
name: String,
books: {
type: Array,
items: Book
itemType: Book
}
})(class User {});

Expand Down
4 changes: 2 additions & 2 deletions test/unit/typeCoercion/array.spec.js
Expand Up @@ -6,7 +6,7 @@ describe('type coercion', () => {
const User = attributes({
books: {
type: Array,
items: String
itemType: String
}
})(class User {});

Expand Down Expand Up @@ -66,7 +66,7 @@ describe('type coercion', () => {
const Library = attributes({
bookIds: {
type: Array,
items: Number
itemType: Number
}
})(class Library {});

Expand Down
4 changes: 2 additions & 2 deletions test/unit/typeCoercion/arraySubclass.spec.js
Expand Up @@ -8,7 +8,7 @@ describe('type coercion', () => {
const User = attributes({
books: {
type: Collection,
items: String
itemType: String
}
})(class User {});

Expand Down Expand Up @@ -76,7 +76,7 @@ describe('type coercion', () => {
const Library = attributes({
bookIds: {
type: Array,
items: Number
itemType: Number
}
})(class Library {});

Expand Down
16 changes: 8 additions & 8 deletions test/unit/validation/array.spec.js
Expand Up @@ -7,7 +7,7 @@ describe('validation', () => {
const User = attributes({
books: {
type: Array,
items: String
itemType: String
}
})(class User {});

Expand Down Expand Up @@ -38,7 +38,7 @@ describe('validation', () => {
const User = attributes({
books: {
type: Array,
items: String,
itemType: String,
required: true
}
})(class User {});
Expand Down Expand Up @@ -73,7 +73,7 @@ describe('validation', () => {
const User = attributes({
books: {
type: Array,
items: String,
itemType: String,
sparse: false
}
})(class User {});
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('validation', () => {
const User = attributes({
books: {
type: Array,
items: String,
itemType: String,
sparse: true
}
})(class User {});
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('validation', () => {
const User = attributes({
books: {
type: Array,
items: Book,
itemType: Book,
required: true
}
})(class User {});
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('validation', () => {
const User = attributes({
books: {
type: Array,
items: String,
itemType: String,
minLength: 2
}
})(class User {});
Expand Down Expand Up @@ -226,7 +226,7 @@ describe('validation', () => {
const User = attributes({
books: {
type: Array,
items: String,
itemType: String,
maxLength: 2
}
})(class User {});
Expand Down Expand Up @@ -266,7 +266,7 @@ describe('validation', () => {
const User = attributes({
books: {
type: Array,
items: String,
itemType: String,
exactLength: 2
}
})(class User {});
Expand Down

0 comments on commit 065414e

Please sign in to comment.