[convict] Clean up type definitions and tests

This commit is contained in:
Eli Young
2017-06-22 13:51:01 -07:00
parent 96306a10b1
commit c2ada0503c
3 changed files with 35 additions and 38 deletions
+16 -21
View File
@@ -1,48 +1,41 @@
/// <reference types="validator" />
import convict = require('convict');
import validator = require('validator');
// define a schema
// straight from the convict tests
const format : convict.Format = {
const format: convict.Format = {
name: 'float-percent',
validate: function(val) {
validate(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);
coerce(val) {
return parseFloat(val);
}
};
convict.addFormat(format);
convict.addFormats({
prime: {
validate: function(val) {
validate(val) {
function isPrime(n: number) {
if (n <= 1) return false; // zero and one are not prime
for (var i=2; i*i <= n; i++) {
for (let 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) {
coerce(val) {
return parseInt(val, 10);
}
}
});
var conf = convict({
let conf = convict({
env: {
doc: 'The applicaton environment.',
format: ['production', 'development', 'test'],
@@ -65,7 +58,11 @@ var conf = convict({
},
key: {
doc: "API key",
format: (val: string) => validator.isUUID(val),
format: (val: string) => {
if (!validator.isUUID(val)) {
throw new Error('must be a valid UUID');
}
},
default: '01527E56-8431-11E4-AF91-47B661C210CA'
},
db: {
@@ -97,14 +94,12 @@ var conf = convict({
format: 'float-percent',
default: 0.5
},
});
// load environment dependent configuration
var env = conf.get('env');
var dbip = conf.get('db.ip');
let env = conf.get('env');
let dbip = conf.get('db.ip');
conf.loadFile('./config/' + env + '.json');
conf.loadFile(['./configs/always.json', './configs/sometimes.json']);
@@ -124,7 +119,7 @@ conf
.validate({ allowed: 'warn' })
.toString();
var port: number = conf.default('port');
let port: number = conf.default('port');
if (conf.has('key')) {
conf.set('the.awesome', true);
+12 -17
View File
@@ -1,11 +1,11 @@
// Type definitions for convict 4.0
// Project: https://github.com/mozilla/node-convict
// Definitions by: Wim Looman <https://github.com/Nemo157>, Vesa Poikajärvi <https://github.com/vesse>
// Definitions by: Wim Looman <https://github.com/Nemo157>
// Vesa Poikajärvi <https://github.com/vesse>
// Eli Young <https://github.com/elyscape>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace convict {
type ValidationMethod = 'strict' | 'warn';
interface ValidateOptions {
@@ -23,8 +23,8 @@ declare namespace convict {
interface Format {
name?: string;
validate?: (val: any) => void;
coerce?: (val: any) => any;
validate?(val: any): void;
coerce?(val: any): any;
}
interface Schema {
@@ -35,14 +35,15 @@ declare namespace convict {
* From the implementation:
*
* format can be a:
* - predefine type, as seen below
* - predefined 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
* - or if omitted, the Object.prototype.toString.call of the default value
* - function that performs validation and throws an Error on failure
*
* The docs also state that any function that validates is ok too
* If omitted, format will be set to the value of Object.prototype.toString.call
* for the default value
*/
format?: string | Array<any> | Function;
format?: string | any[] | ((val: any) => void);
env?: string;
arg?: string;
sensitive?: boolean;
@@ -79,17 +80,11 @@ declare namespace convict {
*/
load(conf: Object): Config;
/**
* Loads and merges one JSON configuration file into config
* Loads and merges JSON configuration file(s) into config
*
* @return {Config} instance
*/
loadFile(file: string): Config;
/**
* Loads and merges multiple JSON configuration files into config
*
* @return {Config} instance
*/
loadFile(files: string[]): Config;
loadFile(files: string | string[]): Config;
/**
* Validates config against the schema used to initialize it
*
+7
View File
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dt.json",
"rules": {
// ban-types needs to be disabled to support TypeScript <2.2
"ban-types": false
}
}