Skip to content

Commit

Permalink
Merge pull request #6 from aBMania/master
Browse files Browse the repository at this point in the history
fix empty config bug
  • Loading branch information
kferrone committed Feb 11, 2021
2 parents 945d066 + b0f1fd7 commit 3069cf2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Reflector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Reflector {
*/
public setPropertyForClass(target: any, propertyName: string) {
let props = this.getClassProperties(target);
if (props === null) {
if (props.length === 0) {
this._reflect.defineMetadata("tsconvict:properties", [], target.constructor);
props = this.getClassProperties(target);
}
Expand All @@ -112,7 +112,7 @@ class Reflector {
* @param target A constructor of a config class.
*/
public getClassProperties(target: any): string[] {
return this._reflect.getMetadata("tsconvict:properties", target.constructor) || null;
return this._reflect.getMetadata("tsconvict:properties", target.constructor) || [];
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/scenarios/empty_config/EmptyConfig.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default class EmptyConfig {
}
38 changes: 38 additions & 0 deletions src/test/scenarios/empty_config/EmptyConfig.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @module test/models
*/
import {suite, test} from "mocha-typescript";
import * as assert from "assert";
import "mocha";
import EmptyConfig from './EmptyConfig.model';
import {TSConvict} from '../../../index';

let tsConvict: TSConvict<EmptyConfig>;

@suite('Test a config with only simple values')
export class EmptyConfigTest {

/**
* Make a new one each and every test.
*/
public before() {
tsConvict = new TSConvict(EmptyConfig);
}

@test('Test getting an empty config')
public validConfig() {
const myValidConfig: EmptyConfig = tsConvict.load({});
// Make sure we got a proper serialized type back
assert.strictEqual(
(myValidConfig instanceof EmptyConfig),
true,
'Expected the config to be an instance of EmptyConfig'
);

assert.deepEqual(
myValidConfig,
{},
'Expected the config to be empty'
);
}
}

0 comments on commit 3069cf2

Please sign in to comment.