From 321e862d5a3618f2ce96de0b4cc4075f4e93fad9 Mon Sep 17 00:00:00 2001 From: Nimish Telang Date: Wed, 13 Jan 2016 14:48:03 +0000 Subject: [PATCH] Update convict definitions to add custom formats --- convict/convict-tests.ts | 43 +++++++++++++++++++++++++++++- convict/convict.d.ts | 57 ++++++++++++++++++++++++---------------- 2 files changed, 76 insertions(+), 24 deletions(-) diff --git a/convict/convict-tests.ts b/convict/convict-tests.ts index 6cde3b38c0..54a5701410 100644 --- a/convict/convict-tests.ts +++ b/convict/convict-tests.ts @@ -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 +( 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 + }, }); diff --git a/convict/convict.d.ts b/convict/convict.d.ts index 74ed100389..332441b207 100644 --- a/convict/convict.d.ts +++ b/convict/convict.d.ts @@ -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; } +