structure
structure copied to clipboard
[Feature Proposal] Allow users to extend default options
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
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/orAggregateStructure
) that would always have anid
field of the typeNumber
.
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 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.
Yep, it does make sense!