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

Commit b880e4c

Browse files
smashercosmopaularmstrong
authored andcommitted
Possibility to provide default values for the entity
closes gh-117
1 parent acd3ad0 commit b880e4c

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ const article = new Schema('articles', { assignEntity: function (output, key, va
262262
output[key] = value;
263263
}
264264
}})
265+
266+
// You can specify default values for the entity
267+
const article = new Schema('articles', { defaults: { likes: 0 } });
265268
```
266269

267270
### `Schema.prototype.define(nestedSchema)`
@@ -302,6 +305,17 @@ slugArticle.getIdAttribute();
302305
// slug
303306
```
304307

308+
### `Schema.prototype.getDefaults()`
309+
310+
Returns the default values of the schema.
311+
312+
```javascript
313+
const article = new Schema('articles', { defaults: { likes: 0 } });
314+
315+
article.getDefaults();
316+
// { likes: 0 }
317+
```
318+
305319
### `arrayOf(schema, [options])`
306320

307321
Describes an array of the schema passed as argument.

src/EntitySchema.js

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default class EntitySchema {
1111
this._getId = typeof idAttribute === 'function' ? idAttribute : x => x[idAttribute];
1212
this._idAttribute = idAttribute;
1313
this._meta = options.meta;
14+
this._defaults = options.defaults;
1415
}
1516

1617
getAssignEntity() {
@@ -35,6 +36,10 @@ export default class EntitySchema {
3536
}
3637
return this._meta && this._meta[prop];
3738
}
39+
40+
getDefaults() {
41+
return this._defaults;
42+
}
3843

3944
define(nestedSchema) {
4045
for (let key in nestedSchema) {

src/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ function defaultAssignEntity(normalized, key, entity) {
1111
function visitObject(obj, schema, bag, options) {
1212
const { assignEntity = defaultAssignEntity } = options;
1313

14-
let normalized = {};
14+
const defaults = schema && schema.getDefaults && schema.getDefaults();
15+
const schemaAssignEntity = schema && schema.getAssignEntity && schema.getAssignEntity();
16+
let normalized = isObject(defaults) ? { ...defaults } : {};
1517
for (let key in obj) {
1618
if (obj.hasOwnProperty(key)) {
17-
const schemaAssignEntity = schema && schema.getAssignEntity && schema.getAssignEntity();
1819
const entity = visit(obj[key], schema[key], bag, options);
1920
assignEntity.call(null, normalized, key, entity, obj, schema);
2021
if (schemaAssignEntity) {

test/index.js

+83
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,56 @@ describe('normalizr', function () {
8686
});
8787
});
8888

89+
it('can provide default values for a single entity', function () {
90+
var article = new Schema('articles', {defaults: {isFavorite: false}}),
91+
input;
92+
93+
input = {
94+
id: 1,
95+
title: 'Some Article'
96+
};
97+
98+
Object.freeze(input);
99+
100+
normalize(input, article).should.eql({
101+
result: 1,
102+
entities: {
103+
articles: {
104+
1: {
105+
id: 1,
106+
title: 'Some Article',
107+
isFavorite: false
108+
}
109+
}
110+
}
111+
});
112+
});
113+
114+
it('does not overwrite the default', function () {
115+
var article = new Schema('articles', {defaults: {isFavorite: false}}),
116+
input;
117+
118+
input = {
119+
id: 1
120+
};
121+
122+
Object.freeze(input);
123+
124+
normalize({ id: 2, title: 'foo' }, article);
125+
126+
normalize(input, article).should.eql({
127+
result: 1,
128+
entities: {
129+
articles: {
130+
1: {
131+
id: 1,
132+
isFavorite: false
133+
}
134+
}
135+
}
136+
});
137+
});
138+
89139
it('can normalize nested entity and delete an existing key using custom function', function () {
90140
var article = new Schema('articles'),
91141
type = new Schema('types'),
@@ -636,6 +686,39 @@ describe('normalizr', function () {
636686
});
637687
});
638688

689+
it('can provide default values for an array', function () {
690+
var article = new Schema('articles', {defaults: {isFavorite: false}}),
691+
input;
692+
693+
input = [{
694+
id: 1,
695+
title: 'Some Article'
696+
}, {
697+
id: 2,
698+
title: 'Other Article'
699+
}];
700+
701+
Object.freeze(input);
702+
703+
normalize(input, arrayOf(article)).should.eql({
704+
result: [1, 2],
705+
entities: {
706+
articles: {
707+
1: {
708+
id: 1,
709+
title: 'Some Article',
710+
isFavorite: false
711+
},
712+
2: {
713+
id: 2,
714+
title: 'Other Article',
715+
isFavorite: false
716+
}
717+
}
718+
}
719+
});
720+
});
721+
639722
it('can normalize a polymorphic array with schema attribute', function () {
640723
var article = new Schema('articles'),
641724
tutorial = new Schema('tutorials'),

0 commit comments

Comments
 (0)