Skip to content
This repository was archived by the owner on Mar 20, 2022. It is now read-only.

Possibility to provide default values for the entity #117

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ const article = new Schema('articles', { assignEntity: function (output, key, va
obj[key] = value;
}
}})

// You can specify default values for the entity
const article = new Schema('articles', { defaults: { likes: 0 } });
```

### `Schema.prototype.define(nestedSchema)`
Expand Down Expand Up @@ -302,6 +305,17 @@ slugArticle.getIdAttribute();
// slug
```

### `Schema.prototype.getDefaults()`

Returns the default values of the schema.

```javascript
const article = new Schema('articles', { defaults: { likes: 0 } });

article.getDefaults();
// { likes: 0 }
```

### `arrayOf(schema, [options])`

Describes an array of the schema passed as argument.
Expand Down
5 changes: 5 additions & 0 deletions src/EntitySchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class EntitySchema {
this._getId = typeof idAttribute === 'function' ? idAttribute : x => x[idAttribute];
this._idAttribute = idAttribute;
this._meta = options.meta;
this._defaults = options.defaults;
}

getAssignEntity() {
Expand All @@ -35,6 +36,10 @@ export default class EntitySchema {
}
return this._meta && this._meta[prop];
}

getDefaults() {
return this._defaults;
}

define(nestedSchema) {
for (let key in nestedSchema) {
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ function defaultAssignEntity(normalized, key, entity) {
function visitObject(obj, schema, bag, options) {
const { assignEntity = defaultAssignEntity } = options;

let normalized = {};
const defaults = schema && schema.getDefaults && schema.getDefaults();
const schemaAssignEntity = schema && schema.getAssignEntity && schema.getAssignEntity();
let normalized = isObject(defaults) ? { ...defaults } : {};
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
const schemaAssignEntity = schema && schema.getAssignEntity && schema.getAssignEntity();
const entity = visit(obj[key], schema[key], bag, options);
assignEntity.call(null, normalized, key, entity, obj, schema);
if (schemaAssignEntity) {
Expand Down
83 changes: 83 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,56 @@ describe('normalizr', function () {
});
});

it('can provide default values for a single entity', function () {
var article = new Schema('articles', {defaults: {isFavorite: false}}),
input;

input = {
id: 1,
title: 'Some Article'
};

Object.freeze(input);

normalize(input, article).should.eql({
result: 1,
entities: {
articles: {
1: {
id: 1,
title: 'Some Article',
isFavorite: false
}
}
}
});
});

it('does not overwrite the default', function () {
var article = new Schema('articles', {defaults: {isFavorite: false}}),
input;

input = {
id: 1
};

Object.freeze(input);

normalize({ id: 2, title: 'foo' }, article);

normalize(input, article).should.eql({
result: 1,
entities: {
articles: {
1: {
id: 1,
isFavorite: false
}
}
}
});
});

it('can normalize nested entity and delete an existing key using custom function', function () {
var article = new Schema('articles'),
type = new Schema('types'),
Expand Down Expand Up @@ -636,6 +686,39 @@ describe('normalizr', function () {
});
});

it('can provide default values for an array', function () {
var article = new Schema('articles', {defaults: {isFavorite: false}}),
input;

input = [{
id: 1,
title: 'Some Article'
}, {
id: 2,
title: 'Other Article'
}];

Object.freeze(input);

normalize(input, arrayOf(article)).should.eql({
result: [1, 2],
entities: {
articles: {
1: {
id: 1,
title: 'Some Article',
isFavorite: false
},
2: {
id: 2,
title: 'Other Article',
isFavorite: false
}
}
}
});
});

it('can normalize a polymorphic array with schema attribute', function () {
var article = new Schema('articles'),
tutorial = new Schema('tutorials'),
Expand Down