Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Feature/concat schemas #432

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,37 @@ class Schema extends SchemaMetadata {
return null;
}

static concat(...sources) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to use Schema.protoype.toJSON to reduce amount of code. You can create new Schema with just typings from toJSON

const defs = {};
const schemas = sources.map((s) => Schema.from(s));
const kind = schemas[0]?.kind;
for (const schema of schemas) {
if (kind !== schema.kind) {
throw new Error(
`Schemas have different kinds: "${kind}", "${schema.kind}"`,
);
}
for (const [key, value] of Object.entries(schema.fields)) {
const { type, required } = value;
if (
defs[key] !== undefined &&
(type !== defs[key].type || required !== defs[key].required)
) {
throw new Error(`Schema concat conflicts with key "${key}"`);
}
defs[key] = value;
}
}
const resultSchema = Schema.from(defs);
// Q: How to merge options?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to merge options and concat validate functions

for (const schema of schemas) {
resultSchema.updateFromSchema(schema);
// Q: Can we have conflicts with indexes?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need examples of it, write tests please

Object.assign(resultSchema.indexes, schema.indexes);
}
return resultSchema;
}

constructor(name, raw, namespaces = []) {
if (isInstanceOf(raw, 'Schema')) return raw;
super();
Expand Down
76 changes: 76 additions & 0 deletions test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,79 @@ metatests.testSync('Schema: toString, JSON.stringify', (test) => {
);
test.end();
});

metatests.test('Schema: concat schemas with different attrs', (test) => {
const schema1 = Schema.from({ a: 'string', b: 'number' });
const schema2 = Schema.from({ c: 'string', d: 'number' });
const schema3 = Schema.from({ e: 'string', f: 'number' });
test.strictEqual(
Schema.concat(schema1, schema2, schema3).toString(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use toJSON to check typings

JSON.stringify({
a: { required: true, type: 'string' },
b: { required: true, type: 'number' },
c: { required: true, type: 'string' },
d: { required: true, type: 'number' },
e: { required: true, type: 'string' },
f: { required: true, type: 'number' },
}),
);
test.end();
});

metatests.test(
'Schema: concat schemas with common attrs fails, when conflict exists',
(test) => {
const schema1 = Schema.from({ a: 'string', b: 'number' });
const schema2 = Schema.from({ b: 'string', c: 'number' });
try {
Schema.concat(schema1, schema2);
throw new Error("Shouldn't reach this point");
} catch (e) {
test.strictEqual(e.message, 'Schema concat conflicts with key "b"');
}
const schema3 = Schema.from({ Custom: {}, type: 'string' });
const schema4 = Schema.from({ Custom: {}, type: 'number' });
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use test.throws(() => fn(), new Error(your error message))

Schema.concat(schema3, schema4);
throw new Error("Shouldn't reach this point");
} catch (e) {
test.strictEqual(e.message, 'Schema concat conflicts with key "type"');
}
test.end();
},
);

metatests.test(
'Schema: concat schemas with common attrs succeeds, when common fields are identical',
(test) => {
const schema1 = Schema.from({ a: 'string', b: 'number' });
const schema2 = Schema.from({ b: 'number', c: 'string' });
test.strictEqual(
Schema.concat(schema1, schema2).toString(),
JSON.stringify({
a: { required: true, type: 'string' },
b: { required: true, type: 'number' },
c: { required: true, type: 'string' },
}),
);
test.end();
},
);

metatests.test(
'Schema: concat schemas should fail, when schemas have different kinds',
(test) => {
const schema1 = Schema.from({ a: 'string', b: 'number' });
const schema2 = Schema.from({ Custom: {}, type: 'string' });
try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use test.throws

Schema.concat(schema1, schema2);
throw new Error("Shouldn't reach this point");
} catch (e) {
test.strictEqual(
e.message,
'Schemas have different kinds: "struct", "custom"',
);
}
test.end();
},
);