mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-09 11:13:57 +08:00
Merge pull request #7606 from nimish/update-convict
Update convict definitions to add custom formats
This commit is contained in:
@@ -6,6 +6,42 @@ 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 +82,15 @@ var conf = convict({
|
||||
env: 'PORT',
|
||||
arg: 'port',
|
||||
}
|
||||
}
|
||||
},
|
||||
primeNumber: {
|
||||
format: 'prime',
|
||||
default: 17
|
||||
},
|
||||
percentNumber: {
|
||||
format: 'float-percent',
|
||||
default: 0.5
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
@@ -72,4 +116,10 @@ if (conf.has('key')) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
conf.getSchema();
|
||||
conf.getProperties();
|
||||
conf.getSchemaString();
|
||||
conf.toString();
|
||||
|
||||
// vim:et:sw=2:ts=2
|
||||
|
||||
Vendored
+67
-23
@@ -4,30 +4,74 @@
|
||||
// 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;
|
||||
/**
|
||||
* From the implementation:
|
||||
*
|
||||
* 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
|
||||
* - or if omitted, the Object.prototype.toString.call of the default value
|
||||
*
|
||||
* The docs also state that any function that validates is ok too
|
||||
*/
|
||||
format?: string | Array<any> | Function;
|
||||
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;
|
||||
/**
|
||||
* Exports all the properties (that is the keys and their current values) as a {JSON} {Object}
|
||||
* @returns {Object} A {JSON} compliant {Object}
|
||||
*/
|
||||
getProperties() : Object;
|
||||
/**
|
||||
* Exports the schema as a {JSON} {Object}
|
||||
* @returns {Object} A {JSON} compliant {Object}
|
||||
*/
|
||||
getSchema() : Object;
|
||||
|
||||
/**
|
||||
* Exports all the properties (that is the keys and their current values) as a JSON string.
|
||||
* @returns {String} a string representing this object
|
||||
*/
|
||||
toString() : string;
|
||||
|
||||
/**
|
||||
* Exports the schema as a JSON string.
|
||||
* @returns {String} a string representing the schema of this {Config}
|
||||
*/
|
||||
getSchemaString() : string;
|
||||
}
|
||||
}
|
||||
interface convict {
|
||||
addFormat(format: convict.Format): void;
|
||||
addFormats(formats: { [name: string]: convict.Format }): void;
|
||||
(config: convict.Schema): convict.Config;
|
||||
}
|
||||
var convict : convict;
|
||||
export = convict;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user