Update convict definitions to add custom formats

This commit is contained in:
Nimish Telang
2016-01-13 14:50:10 +00:00
parent 1984ea435f
commit 321e862d5a
2 changed files with 76 additions and 24 deletions
+42 -1
View File
@@ -6,6 +6,39 @@ import validator = require('validator');
// define a schema
// straight from the convict tests
const format : convict.Format = {
name: 'float-percent',
validate: function(val) {
if (val !== 0 && (!val || val > 1 || val < 0)) {
throw new Error('must be a float between 0 and 1, inclusive');
}
},
coerce: function(val) {
return +(<string> val);
}
};
convict.addFormat(format);
convict.addFormats({
prime: {
validate: function(val) {
function isPrime(n: number) {
if (n <= 1) return false; // zero and one are not prime
for (var i=2; i*i <= n; i++) {
if (n % i === 0) return false;
}
return true;
}
if (!isPrime(val)) throw new Error('must be a prime number');
},
coerce: function(val) {
return parseInt(val, 10);
}
}
});
var conf = convict({
env: {
doc: 'The applicaton environment.',
@@ -46,7 +79,15 @@ var conf = convict({
env: 'PORT',
arg: 'port',
}
}
},
primeNumber: {
format: 'prime',
default: 17
},
percentNumber: {
format: 'float-percent',
default: 0.5
},
});
+34 -23
View File
@@ -4,30 +4,41 @@
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module "convict" {
function convict(schema: convict.Schema): convict.Config;
module convict {
module convict {
interface Schema {
[name: string]: convict.Schema | {
default: any;
doc?: string;
format?: any;
env?: string;
arg?: string;
};
}
interface Format {
name?: string;
validate?: (val: any) => void;
coerce?: (val: any) => any;
}
interface Config {
get(name: string): any;
default(name: string): any;
has(name: string): boolean;
set(name: string, value: any): void;
load(conf: Object): void;
loadFile(file: string): void;
loadFile(files: string[]): void;
validate(): void;
}
}
interface Schema {
[name: string]: convict.Schema | {
default: any;
doc?: string;
format?: any;
env?: string;
arg?: string;
};
}
export = convict;
interface Config {
get(name: string): any;
default(name: string): any;
has(name: string): boolean;
set(name: string, value: any): void;
load(conf: Object): void;
loadFile(file: string): void;
loadFile(files: string[]): void;
validate(): void;
}
}
interface convict {
addFormat(format: convict.Format): void;
addFormats(formats: { [name: string]: convict.Format }): void;
(config: convict.Schema): convict.Config;
}
var convict : convict;
export = convict;
}