Skip to content

Commit

Permalink
Merge pull request #83 from hckrnews/feature/validate-object-with-fun…
Browse files Browse the repository at this point in the history
…ctions

Test objects with custom objects
  • Loading branch information
w3nl authored Apr 15, 2021
2 parents 7e697af + af6e9a6 commit 5548389
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@hckrnews/objects",
"description": "Hack JavaScript objects",
"version": "2.2.0",
"version": "2.2.1",
"author": {
"name": "Pieter Wigboldus",
"url": "https://hckr.news/"
Expand Down Expand Up @@ -78,6 +78,6 @@
"flat"
],
"dependencies": {
"@hckrnews/validator": "^4.2.0"
"@hckrnews/validator": "^4.2.2"
}
}
16 changes: 16 additions & 0 deletions src/__tests__/validate-objects.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-new */
import Obj from '../objects';
import test1Schema from '../schemas/test1';
import Test2 from '../schemas/test2';

describe('Object test', () => {
const addressSchema = {
Expand All @@ -12,6 +14,8 @@ describe('Object test', () => {
};

const Address = Obj({ schema: addressSchema });
const Test = Obj({ schema: test1Schema });
const test2 = new Test2('me');

it('It should validate the input and set the original object', () => {
const build = () => {};
Expand Down Expand Up @@ -65,4 +69,16 @@ describe('Object test', () => {
});
}).toThrowError('The field country should be a String');
});

it('It should valid with this custom type', () => {
const data = {
name: 'test',
test: test2
}
const test = Test.create(data)
expect(test).toEqual({
name: 'test',
test: test2
});
});
});
5 changes: 4 additions & 1 deletion src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ const ObjectGenerator = ({ schema } = {}) => {
index = [this.prefix, originalRowIndex].join('.');
}

if (typeof originalRow === 'object') {
if (
originalRow.constructor === Object ||
originalRow.constructor === Array
) {
const childRows = new Obj(originalRow, index).flat;

this.flatObject = Object.assign(
Expand Down
6 changes: 6 additions & 0 deletions src/schemas/test1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Test2 from './test2';

export default {
name: String,
test: Test2,
};
5 changes: 5 additions & 0 deletions src/schemas/test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class Test2 {
constructor(name) {
this.name = name;
}
}

0 comments on commit 5548389

Please sign in to comment.