-
-
Notifications
You must be signed in to change notification settings - Fork 18
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
base: master
Are you sure you want to change the base?
Changes from all commits
b5609ce
0387ef1
b878f17
c1e1494
8ff9cc7
f88b7be
b5c5348
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,37 @@ class Schema extends SchemaMetadata { | |
return null; | ||
} | ||
|
||
static concat(...sources) { | ||
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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
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(); | ||
}, | ||
); |
There was a problem hiding this comment.
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 fromtoJSON