canjs / can-reflect

Operate on unknown data types
https://canjs.com/doc/can-reflect.html
MIT License
4 stars 4 forks source link

Merging schemas #169

Closed matthewp closed 5 years ago

matthewp commented 5 years ago

In https://github.com/canjs/can-type/issues/31 @justinbmeyer said he wants the ability to use an existing schema as the basis for creating a new one.

Since schemas look like this:

{
  type: "map",
  identity: ["id"],
  keys: {
    prop: TypeObject
  }
}

You can't easily merge schema using simply Object.assign(a, b) or { ...a, ...b } style coding.


This issue proposes to solve the above problem by adding a Reflect.mergeSchemas(a, b) method to can-reflect. It could be used like so:

canReflect.mergeSchemas(TypeOrSchema, {
  keys: {
    dueDate: {
      [Symbol.for("can.new")](value) {
        if( date === "yesterday" ) {
            return new Date()- 1000*24
        }
        return date;
      }
    }
  }
});

This method would:

  1. Smartly merge identity arrays, if they exist.
  2. Merge keys, ensuring that each key has both a can.new and can.isMember symbol (the above example only provides a new can.new.
  3. Return a new schema.

Note that the primary motivate for this new feature is https://github.com/canjs/can-fixture/issues/175. can-fixture would still need updating to be able to take schemas (it currently only takes queryLogics).

matthewp commented 5 years ago

Punting