Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 8575a1d

Browse files
committed
anyOf like empty schema
1 parent a4dcfa5 commit 8575a1d

File tree

3 files changed

+30
-42
lines changed

3 files changed

+30
-42
lines changed

lib/index.js

+4-15
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function serialize(resource, schema, options = {}) {
3434
}
3535

3636
function findObjectDef(propertyDef) {
37-
const { type, anyOf, properties } = propertyDef;
37+
const { type, properties } = propertyDef;
3838

3939
if (type === 'object' && properties) {
4040
return propertyDef;
@@ -44,20 +44,6 @@ function findObjectDef(propertyDef) {
4444
return findObjectDef(propertyDef.items);
4545
}
4646

47-
if (anyOf) {
48-
const props = anyOf
49-
.map(findObjectDef)
50-
.filter(Boolean)
51-
.map(item => item.properties)
52-
.reduce((acc, curr) => ({ ...acc, ...curr }), {});
53-
if (Object.keys(props).length > 0) {
54-
return {
55-
type: 'object',
56-
properties: props,
57-
};
58-
}
59-
}
60-
6147
return null;
6248
}
6349

@@ -66,6 +52,9 @@ function isObjectLike(value) {
6652
}
6753

6854
function toJSON(value) {
55+
if (Array.isArray(value)) {
56+
return value.map(toJSON);
57+
}
6958
return value && typeof value.toJSON === 'function' ? value.toJSON() : value;
7059
}
7160

tests/all.test.js

+21-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const Sequelize = require('sequelize');
22
const { createModel, DUMMY_VALUES } = require('./init');
33

4-
const sequelize = new Sequelize('sqlite::memory:');
54
const serialize = require('../lib/index');
65

76
describe('Serializers', () => {
@@ -133,7 +132,9 @@ describe('Serializers', () => {
133132

134133
expect(serialize(instanceB, schema)).toEqual({
135134
a: 'x',
136-
modelA: { b: 777, c: true },
135+
modelA: {
136+
a: 'x', b: 777, c: true, id: null,
137+
},
137138
});
138139
});
139140

@@ -183,21 +184,25 @@ describe('Serializers', () => {
183184
};
184185

185186
const instanceA = new ModelA(DUMMY_VALUES);
186-
const instanceB1 = new ModelB(DUMMY_VALUES);
187+
const instanceB1 = new ModelB({ a: 'x' });
187188
const instanceB2 = new ModelB(DUMMY_VALUES);
188189
instanceA.modelsB = [instanceB1, instanceB2];
189190

190191
expect(serialize(instanceA, schema)).toEqual({
191192
a: 'x',
192193
modelsB: [
193-
{ b: 777, c: true },
194-
{ b: 777, c: true },
194+
{
195+
a: 'x', id: null,
196+
},
197+
{
198+
a: 'x', b: 777, c: true, id: null,
199+
},
195200
],
196201
});
197202
});
198203

199204
it('Object schema without properties', () => {
200-
const TestModel = createModel();
205+
const TestModel = createModel('TestModel', { a: Sequelize.DataTypes.JSONB });
201206

202207
const schema = {
203208
type: 'object',
@@ -216,8 +221,14 @@ describe('Serializers', () => {
216221
additionalProperties: false,
217222
};
218223

219-
const instance = new TestModel({ ...DUMMY_VALUES, a: {} });
220-
expect(serialize(instance, schema)).toEqual({ a: {} });
224+
const instance = new TestModel({ ...DUMMY_VALUES, a: DUMMY_VALUES });
225+
expect(serialize(instance, schema)).toEqual({
226+
a: {
227+
a: 'x',
228+
b: 777,
229+
c: true,
230+
},
231+
});
221232
});
222233

223234
it('Resource as empty array', () => {
@@ -242,15 +253,8 @@ describe('Serializers', () => {
242253
});
243254
});
244255

245-
it('anyOf with conflicting subproperties', () => {
246-
class ModelA extends Sequelize.Model { }
247-
248-
ModelA.init(
249-
{
250-
a: Sequelize.DataTypes.JSONB,
251-
},
252-
{ sequelize },
253-
);
256+
it('anyOf with conflicting nested properties', () => {
257+
const ModelA = createModel('ModelA', { a: Sequelize.DataTypes.JSONB });
254258

255259
const schema = {
256260
type: 'object',

tests/init.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
const Sequelize = require('sequelize');
22

3-
const sequelize = new Sequelize('sqlite::memory:');
3+
const sequelize = new Sequelize('sqlite::memory:', { logging: false });
44

55
const DUMMY_VALUES = {
66
a: 'x',
77
b: 777,
88
c: true,
99
};
1010

11-
function createModel() {
12-
class TestModel extends Sequelize.Model {}
13-
14-
TestModel.init(
11+
function createModel(modelName = 'TestModel', props = {}) {
12+
return sequelize.define(modelName,
1513
{
1614
a: Sequelize.DataTypes.STRING,
1715
b: Sequelize.DataTypes.INTEGER,
1816
c: Sequelize.DataTypes.BOOLEAN,
19-
},
20-
{ sequelize },
21-
);
22-
23-
return TestModel;
17+
...props,
18+
});
2419
}
2520

2621
module.exports = {

0 commit comments

Comments
 (0)