isvalid 4.1.30 → 4.1.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/plugins.js +21 -5
- package/package.json +1 -1
- package/test/index.js +1 -1
package/lib/plugins.js
CHANGED
|
@@ -6,15 +6,31 @@
|
|
|
6
6
|
// See license in LICENSE
|
|
7
7
|
//
|
|
8
8
|
|
|
9
|
-
|
|
10
9
|
import { isSameType, instanceTypeName, typeName } from './utils.js';
|
|
11
10
|
|
|
12
|
-
const plugins =
|
|
11
|
+
const plugins = {};
|
|
12
|
+
|
|
13
|
+
export function use(identifier, plugin, { conflict = 'fail' } = {}) {
|
|
14
|
+
|
|
15
|
+
if (typeof identifier !== 'string') {
|
|
16
|
+
throw new Error('Identifier must be a string.');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (plugins[identifier]) {
|
|
20
|
+
switch (conflict) {
|
|
21
|
+
case 'replace':
|
|
22
|
+
break;
|
|
23
|
+
case 'ignore':
|
|
24
|
+
return;
|
|
25
|
+
default:
|
|
26
|
+
throw new Error(`Plugin with identifier "${identifier}" already exists.`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
plugins[identifier] = plugin({ isSameType, instanceTypeName, typeName });
|
|
13
31
|
|
|
14
|
-
export function use(plugin) {
|
|
15
|
-
plugins.push(plugin({ isSameType, instanceTypeName, typeName }));
|
|
16
32
|
}
|
|
17
33
|
|
|
18
34
|
export function all() {
|
|
19
|
-
return plugins;
|
|
35
|
+
return Object.values(plugins);
|
|
20
36
|
}
|
package/package.json
CHANGED
package/test/index.js
CHANGED
|
@@ -13,7 +13,7 @@ import { use } from '../lib/plugins.js';
|
|
|
13
13
|
|
|
14
14
|
chaiUse(chaiAsPromised);
|
|
15
15
|
|
|
16
|
-
use(
|
|
16
|
+
use('utils', (utils) => {
|
|
17
17
|
return {
|
|
18
18
|
supportsType: (type) => utils.isSameType(type, String),
|
|
19
19
|
validatorsForType: () => { return { ensureCase: String }; },
|