diff --git a/types/convict/convict-tests.ts b/types/convict/convict-tests.ts index a61c27bdad..0451954ca1 100644 --- a/types/convict/convict-tests.ts +++ b/types/convict/convict-tests.ts @@ -105,6 +105,18 @@ conf.loadFile(['./configs/always.json', './configs/sometimes.json']); // perform validation conf.validate({ strict: true }); +conf.validate({ allowed: 'strict' }); +conf.validate({ allowed: 'warn' }); + +// Chaining + +conf + .loadFile(['./configs/always.json', './configs/sometimes.json']) + .loadFile('./config/' + env + '.json') + .load({ jsonKey: 'jsonValue' }) + .set('key', 'value') + .validate({ allowed: 'warn' }) + .toString(); var port: number = conf.default('port'); diff --git a/types/convict/index.d.ts b/types/convict/index.d.ts index 981bc8148b..ebe5d21490 100644 --- a/types/convict/index.d.ts +++ b/types/convict/index.d.ts @@ -1,11 +1,26 @@ -// Type definitions for node-convict v0.6.0 +// Type definitions for node-convict v3.0.0 // Project: https://github.com/mozilla/node-convict -// Definitions by: Wim Looman +// Definitions by: Wim Looman , Vesa Poikajärvi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace convict { + type ValidationMethod = 'strict' | 'warn'; + + interface ValidateOptions { + /** + * If set to warn, any properties specified in config files that are not declared in + * the schema will print a warning. This is the default behavior. If set to strict, + * any properties specified in config files that are not declared in the schema will + * throw errors. This is to ensure that the schema and the config files are in sync. + */ + allowed?: ValidationMethod; + + /** @deprecated use allowed instead */ + strict?: boolean; + } + interface Format { name?: string; validate?: (val: any) => void; @@ -34,14 +49,52 @@ declare namespace convict { } interface Config { + /** + * @returns the current value of the name property. name can use dot + * notation to reference nested values + */ get(name: string): any; + /** + * @returns the default value of the name property. name can use dot + * notation to reference nested values + */ default(name: string): any; + /** + * @returns true if the property name is defined, or false otherwise + */ has(name: string): boolean; - set(name: string, value: any): void; - load(conf: Object): void; - loadFile(file: string): void; - loadFile(files: string[]): void; - validate(options?: { strict?: boolean }): void; + /** + * Sets the value of name to value. name can use dot notation to reference + * nested values, e.g. "database.port". If objects in the chain don't yet + * exist, they will be initialized to empty objects + * + * @return {Config} instance + */ + set(name: string, value: any): Config; + /** + * Loads and merges a JavaScript object into config + * + * @return {Config} instance + */ + load(conf: Object): Config; + /** + * Loads and merges one JSON configuration file 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; + /** + * Validates config against the schema used to initialize it + * + * @param options + */ + validate(options?: ValidateOptions): Config; /** * Exports all the properties (that is the keys and their current values) as a {JSON} {Object} * @returns {Object} A {JSON} compliant {Object}