Permalink
Browse files

Support RegExp as a built-in format

  • Loading branch information...
1 parent 0b11e37 commit 1bd4b5a50b325527bdbbbc7564c5a93c24c92be6 @philbooth philbooth committed Sep 23, 2016
Showing with 17 additions and 4 deletions.
  1. +4 −2 lib/convict.js
  2. +7 −1 test/cases/env_types.js
  3. +2 −1 test/cases/env_types.out
  4. +4 −0 test/cases/schema-built-in-formats.json
View
@@ -20,7 +20,7 @@ function assert(assertion, err_msg) {
// format can be a:
// - predefine type, as seen below
// - an array of enumerated values, e.g. ["production", "development", "testing"]
-// - built-in JavaScript type, i.e. Object, Array, String, Number, Boolean
+// - built-in JavaScript type, i.e. Object, Array, String, Number, Boolean, RegExp
// - or if omitted, the Object.prototype.toString.call of the default value
var types = {
@@ -98,7 +98,8 @@ var BUILT_INS_BY_NAME = {
'Array': Array,
'String': String,
'Number': Number,
- 'Boolean': Boolean
+ 'Boolean': Boolean,
+ 'RegExp': RegExp
};
var BUILT_IN_NAMES = Object.keys(BUILT_INS_BY_NAME);
var BUILT_INS = BUILT_IN_NAMES.map(function(name) {
@@ -296,6 +297,7 @@ function coerce(k, v, schema, instance) {
case 'boolean': v = ((v === 'false') ? false : true); break;
case 'array': v = v.split(','); break;
case 'object': v = JSON.parse(v); break;
+ case 'regexp': v = new RegExp(v); break;
case 'timestamp': v = moment(v).valueOf(); break;
case 'duration':
var split = v.split(' ');
@@ -28,6 +28,11 @@ exports.conf = {
format: Object,
default: {},
env: 'OBJECT'
+ },
+ regexp: {
+ format: RegExp,
+ default: /.*/,
+ env: 'REGEXP'
}
};
@@ -37,5 +42,6 @@ exports.env = {
NAT: 666,
NUM: 789.1011,
ARRAY: 'a,b,c',
- OBJECT: '{"foo": "bar"}'
+ OBJECT: '{"foo": "bar"}',
+ REGEXP: '^foo$'
};
@@ -4,5 +4,6 @@
"nat": 666,
"num": 789.1011,
"array": ["a", "b", "c"],
- "object": {"foo": "bar"}
+ "object": {"foo": "bar"},
+ "regexp": {}
}
@@ -18,5 +18,9 @@
"someString": {
"format": "String",
"default": "foo"
+ },
+ "someRegExp": {
+ "format": "RegExp",
+ "default": ".*"
}
}

0 comments on commit 1bd4b5a

Please sign in to comment.