You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`types`|`true`| An array of `string` to enforce types (from `typeof`). <br> Defaults to the type of `value`. |
79
+
|`value`|`false`| The inital value to assign to the property |
80
+
|`validator`|`true`| A validator function used to validate the value, must returns a boolean. <br> Defaults to validate any values. |
81
+
76
82
In order to configure the properties, simply call the package class constructor from inside your constructor using `super`:
77
83
78
84
```javascript
79
85
classAextendsAdvancedConfigurationManagement {
80
86
constructor() {
81
87
super({
82
-
// Detailled version
83
88
foo: {
84
89
types: ['string'], // Optionnal, if not present, inferred from typeof `value`
85
-
default:'', // Optionnal, if not present, inferred from value of `value`
86
90
value:'bar',
91
+
validator: (value) =>value.length>0, // Optionnal, if not present, always validate the value to `true`
87
92
},
88
93
89
-
// Detailed version with multiple types
90
94
spaces: {
91
95
types: ['string', 'number'],
92
-
default:'',
93
96
value:2,
94
97
},
95
-
96
-
// Short version
97
-
baz:true,
98
98
});
99
99
}
100
100
}
101
101
```
102
102
103
103
Create a class `A` that has those configuration properties:
104
104
105
-
- A configuration property named `foo` that can be a `string`, has a value of `'bar'` and a default value of `''` (empty string)
106
-
- A configuration property named `spaces` that can be either a `string` or `number`, has a value of `2`, and a default value of `''` (empty string)
107
-
- A configuration property named `baz` that can be a `boolean`, has a value of `true`, and a default value of `true`
105
+
- A configuration property named `foo` that can be a `string`, has a value of `'bar'` and must not be an empty string
106
+
- A configuration property named `spaces` that can be either a `string` or `number`, has a value of `2`
108
107
109
108
> **Warning!**
110
109
>
111
-
> Even tho having a configuration property as an object is allowed, it is not recommended since it's impossible to assure that the required properties are actually present.
110
+
> Even tho having a configuration property as an object is allowed, it is not recommended since it's impossible to assure that the required properties are actually present. If you need to have an object, use a validator function alongside it.
112
111
113
112
## Accessing the configuration
114
113
@@ -147,34 +146,27 @@ b.setConfig({
147
146
148
147
> Updating a configuration property with an invalid type will throw a `TypeError`.
149
148
149
+
> Updating a configuration property with a value that does not validate will throw a `ValidationError`.
150
+
150
151
## Using the configuration with a root class that cannot be extended
151
152
152
-
If your root class cannot be extended (because it already extend another class for example), you can use an adapter class that will be mounted as a property onto your object:
153
+
If your root class cannot be extended (because it already extend another class for example), you can call the `AdvancedConfigurationManagement.CreateAdapter` with the coonfiguration to mount the configuration onto a property:
0 commit comments