Skip to content

Commit

Permalink
Prevent config.parse() output from being used as input (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrnola authored Jun 13, 2020
1 parent 9a56bd5 commit 7f50b46
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@walmartlabs/cookie-cutter-core",
"version": "1.3.0-beta.4",
"version": "1.3.0-beta.5",
"license": "Apache-2.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ export function parse<T>(TRoot: IClassType<T>, actual: any, base?: Partial<T>):
}
}

if (isSection(actual)) {
throw new Error(
"The value of `actual` has no enumerable properties. Make sure the object being " +
"passed is not the output of `config.parse<T>()` (i.e. does not have the '@section' decorator)."
);
}

const instance = new TRoot();
if (verifyIsSection(instance)) {
const config: T & ISection = new TRoot() as any;
Expand Down Expand Up @@ -303,8 +310,12 @@ interface ISection {
readonly __extensible?: boolean;
}

function isSection(obj: any): obj is ISection {
return obj && obj.__assignedProperties;
}

function verifyIsSection(obj: any): obj is ISection {
if (obj && obj.__assignedProperties) {
if (isSection(obj)) {
return true;
}

Expand Down

0 comments on commit 7f50b46

Please sign in to comment.