Skip to content

Commit 091d240

Browse files
committed
Fix #17 Dynamic module load
1 parent fa46bc5 commit 091d240

File tree

2 files changed

+19
-33
lines changed

2 files changed

+19
-33
lines changed

lib/modify.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
const fs = require('fs');
2+
const directory = 'modifiers';
13

2-
// Add modifier names here
3-
const modifiers = ['default'];
4-
5-
// Turn modifier names to modifier instances.
6-
for (let i = 0; i < modifiers.length; i++) {
7-
modifiers[i] = modifier(modifiers[i]);
8-
}
4+
// Add validator names here
5+
const modifiers = fs.readdirSync(directory).map(function(file) {
6+
const name = file.replace('.js', '');
7+
return {
8+
name: name,
9+
check: require('./' + directory + '/' + name),
10+
};
11+
});
912

1013
/**
1114
* Validate
@@ -22,16 +25,6 @@ function modify(data, model) {
2225
return true;
2326
}
2427

25-
/**
26-
* Create new modifier instances
27-
*/
28-
function modifier(name) {
29-
return {
30-
name: name,
31-
check: require('./modifiers/' + name),
32-
};
33-
}
34-
3528
module.exports = {
3629
modifiers: modifiers,
3730
check: modify

lib/validate.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
const fs = require('fs');
2+
const directory = 'validators';
13

24
// Add validator names here
3-
const validators = ['type', 'length', 'value', 'func', 'model', 'date', 'email', 'regex'];
4-
5-
// Turn validator names to validator instances.
6-
for (let i = 0; i < validators.length; i++) {
7-
validators[i] = validator(validators[i]);
8-
}
5+
const validators = fs.readdirSync(directory).map(function(file) {
6+
const name = file.replace('.js', '');
7+
return {
8+
name: name,
9+
check: require('./' + directory + '/' + name),
10+
};
11+
});
912

1013
/**
1114
* Validate
@@ -24,16 +27,6 @@ function validate(data, model) {
2427
return true;
2528
}
2629

27-
/**
28-
* Create new validator instances
29-
*/
30-
function validator(name) {
31-
return {
32-
name: name,
33-
check: require('./validators/' + name),
34-
};
35-
}
36-
3730
module.exports = {
3831
validators: validators,
3932
check: validate

0 commit comments

Comments
 (0)