Skip to content

Commit

Permalink
Add toJSON() method to structures
Browse files Browse the repository at this point in the history
  • Loading branch information
talyssonoc committed Dec 30, 2016
1 parent 477a553 commit 068426a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
8 changes: 7 additions & 1 deletion src/attributesDecorator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const { normalizeSchema } = require('./schemaNormalization');
const { getInitialValues } = require('./initialValueCreation');
const { SCHEMA } = require('./symbols');
const { attributesDescriptor, validationDescriptor } = require('./propertyDescriptors');
const {
attributesDescriptor,
validationDescriptor,
serializationDescriptor
} = require('./propertyDescriptors');

const define = Object.defineProperty;

Expand Down Expand Up @@ -55,6 +59,8 @@ function attributesDecorator(declaredSchema, ErroneousPassedClass) {

define(WrapperClass.prototype, 'validate', validationDescriptor);

define(WrapperClass.prototype, 'toJSON', serializationDescriptor);

return WrapperClass;
};
}
Expand Down
8 changes: 7 additions & 1 deletion src/propertyDescriptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ exports.attributesDescriptor = {
exports.validationDescriptor = {
value: function validate() {
const validation = this[SCHEMA][VALIDATE];
const serializedStructure = serialize(this);
const serializedStructure = this.toJSON();

const errors = validation.validate(serializedStructure);

Expand All @@ -47,3 +47,9 @@ exports.validationDescriptor = {
return { valid: true };
}
};

exports.serializationDescriptor = {
value: function toJSON() {
return serialize(this);
}
};
5 changes: 2 additions & 3 deletions test/unit/serialization/array.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { expect } = require('chai');
const { attributes } = require('../../../src');
const { serialize } = require('../../../src/serialization');

describe('serialization', () => {
describe('Array', () => {
Expand All @@ -25,7 +24,7 @@ describe('serialization', () => {
]
});

expect(serialize(user)).to.eql({
expect(user.toJSON()).to.eql({
name: 'Something',
books: [
{ name: 'The Hobbit' }
Expand All @@ -45,7 +44,7 @@ describe('serialization', () => {
]
});

const serializedUser = serialize(user);
const serializedUser = user.toJSON();

expect(serializedUser).to.eql({
name: 'Some name',
Expand Down
7 changes: 3 additions & 4 deletions test/unit/serialization/nestedStructure.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { expect } = require('chai');
const { attributes } = require('../../../src');
const { serialize } = require('../../../src/serialization');

describe('serialization', () => {
describe('Nested structure', () => {
Expand All @@ -26,7 +25,7 @@ describe('serialization', () => {
location
});

expect(serialize(user)).to.eql({
expect(user.toJSON()).to.eql({
name: 'Something',
location: {
longitude: 123,
Expand All @@ -42,7 +41,7 @@ describe('serialization', () => {
name: 'Some name'
});

const serializedUser = serialize(user);
const serializedUser = user.toJSON();

expect(serializedUser).to.eql({
name: 'Some name',
Expand All @@ -63,7 +62,7 @@ describe('serialization', () => {
location
});

const serializedUser = serialize(user);
const serializedUser = user.toJSON();

expect(serializedUser).to.eql({
name: 'Name',
Expand Down
5 changes: 2 additions & 3 deletions test/unit/serialization/structure.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { expect } = require('chai');
const { attributes } = require('../../../src');
const { serialize } = require('../../../src/serialization');

describe('serialization', () => {
describe('Structure', () => {
Expand All @@ -16,7 +15,7 @@ describe('serialization', () => {
age: 42
});

expect(serialize(user)).to.eql({
expect(user.toJSON()).to.eql({
name: 'Something',
age: 42
});
Expand All @@ -30,7 +29,7 @@ describe('serialization', () => {
age: undefined
});

const serializedUser = serialize(user);
const serializedUser = user.toJSON();

expect(serializedUser).to.eql({
name: 'Some name',
Expand Down

0 comments on commit 068426a

Please sign in to comment.