structure icon indicating copy to clipboard operation
structure copied to clipboard

[Feature Proposal] Allow users to extend default options

Open wenderjean opened this issue 7 years ago • 3 comments

Description

  • We should allow users to extend defaults and set generic options for their own Structure instance.

Use cases

  • Empty strings: The default behavior for Strings is not to allow empty values, we can set empty: true for each domain but it would be great if we can do a generic configuration.
  • It can also prepare the codebase for this feature.

Example

const MyStructure = Structure.extends({
  validations: [
    {
      type: String,
      empty: true
    }
  ]
});

module.exports = MyStructure;

cc/ @talyssonoc

wenderjean avatar Feb 22 '18 12:02 wenderjean

Also, it would be a great fit to support #12.

Other use cases that could be added is having different defaults for each use of Structure in the app, for example:

  • A base ValidationStructure that would have some set of preset validations that would be used only to validate the body of HTTP requests;
  • A base EntityStructure (and/or AggregateStructure) that would always have an id field of the type Number.

As a suggestion, we could have an internal "structure provider" that receives the attributes and return a new custom Structure lib, and the default one (that is returned when calling `require('structure')) would be just this provider called with a set of default values and the user would extend from that. The idea would be that every custom Structure can be extended to generate a new one inheriting the settings from the parent one, so we could do this:

const MyStructure = Structure.extends({
  validations: [
    {
      type: String,
      empty: true
    }
  ]
});

const AnotherStructure = MyStructure.extends({
  fields: [
    {
      type: Number,
      name: 'id'
    }
  ]
});

talyssonoc avatar Feb 22 '18 12:02 talyssonoc

@talyssonoc We can also add this one as related using the fields approach.

We need the plugin system yet but using field options we could have the defaults timestamp.

wenderjean avatar Feb 22 '18 13:02 wenderjean

Yep, it does make sense!

talyssonoc avatar Feb 22 '18 13:02 talyssonoc