Skip to content

Commit 41c8df3

Browse files
committed
Updated README to reflect recent changes
1 parent dedcbf3 commit 41c8df3

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

README.md

+19-27
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class A extends AdvancedConfigurationManagement {
1010
super({
1111
foo: {
1212
types: ['string'],
13-
default: '',
1413
value: 'bar',
14+
validator: (value) => value.length > 0,
1515
},
1616
});
1717
}
@@ -52,8 +52,8 @@ class A extends AdvancedConfigurationManagement {
5252
super({
5353
foo: {
5454
types: ['string'],
55-
default: '',
5655
value: 'bar',
56+
validator: (value) => value.length > 0,
5757
},
5858
});
5959
}
@@ -73,42 +73,41 @@ b.setConfig('foo', 'baz');
7373

7474
## Configuring the properties
7575

76+
| Property | Optionnal | Description |
77+
| :---------- | :-------- | :------------------------------------------------------------------------------------------------------------- |
78+
| `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+
7682
In order to configure the properties, simply call the package class constructor from inside your constructor using `super`:
7783

7884
```javascript
7985
class A extends AdvancedConfigurationManagement {
8086
constructor() {
8187
super({
82-
// Detailled version
8388
foo: {
8489
types: ['string'], // Optionnal, if not present, inferred from typeof `value`
85-
default: '', // Optionnal, if not present, inferred from value of `value`
8690
value: 'bar',
91+
validator: (value) => value.length > 0, // Optionnal, if not present, always validate the value to `true`
8792
},
8893

89-
// Detailed version with multiple types
9094
spaces: {
9195
types: ['string', 'number'],
92-
default: '',
9396
value: 2,
9497
},
95-
96-
// Short version
97-
baz: true,
9898
});
9999
}
100100
}
101101
```
102102

103103
Create a class `A` that has those configuration properties:
104104

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`
108107

109108
> **Warning!**
110109
>
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.
112111
113112
## Accessing the configuration
114113

@@ -147,34 +146,27 @@ b.setConfig({
147146
148147
> Updating a configuration property with an invalid type will throw a `TypeError`.
149148
149+
> Updating a configuration property with a value that does not validate will throw a `ValidationError`.
150+
150151
## Using the configuration with a root class that cannot be extended
151152

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:
153154

154155
```javascript
155156
// Import the main class
156157
import AdvancedConfigurationManagement from 'advanced-configuration-management';
157158

158-
// Create an adapter class
159-
class ConfigurationAdapter extends AdvancedConfigurationManagement {
159+
// Your own class that cannot be extended
160+
class MyClass {
160161
constructor() {
161-
// Specify the configuration entries
162-
super({
162+
// Mount the configuration adapter onto a property
163+
this.configuration = AdvancedConfigurationManagement.CreateAdapter({
163164
foo: {
164165
types: ['string'],
165-
default: '',
166166
value: 'bar',
167167
},
168168
});
169169
}
170-
}
171-
172-
// Your own class that cannot be extended
173-
class MyClass {
174-
constructor() {
175-
// Mount the configuration adapter onto a property
176-
this.configuration = new ConfigurationAdapter();
177-
}
178170

179171
// Your class logic here...
180172
}

0 commit comments

Comments
 (0)