Skip to content

Commit

Permalink
Support RegExp as a built-in format
Browse files Browse the repository at this point in the history
  • Loading branch information
philbooth committed Sep 23, 2016
1 parent 0b11e37 commit 1bd4b5a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/convict.js
Expand Up @@ -20,7 +20,7 @@ function assert(assertion, err_msg) {
// format can be a: // format can be a:
// - predefine type, as seen below // - predefine type, as seen below
// - an array of enumerated values, e.g. ["production", "development", "testing"] // - 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 // - or if omitted, the Object.prototype.toString.call of the default value


var types = { var types = {
Expand Down Expand Up @@ -98,7 +98,8 @@ var BUILT_INS_BY_NAME = {
'Array': Array, 'Array': Array,
'String': String, 'String': String,
'Number': Number, 'Number': Number,
'Boolean': Boolean 'Boolean': Boolean,
'RegExp': RegExp
}; };
var BUILT_IN_NAMES = Object.keys(BUILT_INS_BY_NAME); var BUILT_IN_NAMES = Object.keys(BUILT_INS_BY_NAME);
var BUILT_INS = BUILT_IN_NAMES.map(function(name) { var BUILT_INS = BUILT_IN_NAMES.map(function(name) {
Expand Down Expand Up @@ -296,6 +297,7 @@ function coerce(k, v, schema, instance) {
case 'boolean': v = ((v === 'false') ? false : true); break; case 'boolean': v = ((v === 'false') ? false : true); break;
case 'array': v = v.split(','); break; case 'array': v = v.split(','); break;
case 'object': v = JSON.parse(v); break; case 'object': v = JSON.parse(v); break;
case 'regexp': v = new RegExp(v); break;
case 'timestamp': v = moment(v).valueOf(); break; case 'timestamp': v = moment(v).valueOf(); break;
case 'duration': case 'duration':
var split = v.split(' '); var split = v.split(' ');
Expand Down
8 changes: 7 additions & 1 deletion test/cases/env_types.js
Expand Up @@ -28,6 +28,11 @@ exports.conf = {
format: Object, format: Object,
default: {}, default: {},
env: 'OBJECT' env: 'OBJECT'
},
regexp: {
format: RegExp,
default: /.*/,
env: 'REGEXP'
} }
}; };


Expand All @@ -37,5 +42,6 @@ exports.env = {
NAT: 666, NAT: 666,
NUM: 789.1011, NUM: 789.1011,
ARRAY: 'a,b,c', ARRAY: 'a,b,c',
OBJECT: '{"foo": "bar"}' OBJECT: '{"foo": "bar"}',
REGEXP: '^foo$'
}; };
3 changes: 2 additions & 1 deletion test/cases/env_types.out
Expand Up @@ -4,5 +4,6 @@
"nat": 666, "nat": 666,
"num": 789.1011, "num": 789.1011,
"array": ["a", "b", "c"], "array": ["a", "b", "c"],
"object": {"foo": "bar"} "object": {"foo": "bar"},
"regexp": {}
} }
4 changes: 4 additions & 0 deletions test/cases/schema-built-in-formats.json
Expand Up @@ -18,5 +18,9 @@
"someString": { "someString": {
"format": "String", "format": "String",
"default": "foo" "default": "foo"
},
"someRegExp": {
"format": "RegExp",
"default": ".*"
} }
} }

0 comments on commit 1bd4b5a

Please sign in to comment.