Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(optional): allows blank values in optional keys #62

Merged
merged 1 commit into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/inputParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ OPTIONAL:
const parsed = parse(contents);
});

it('Parses document with optional blank value', () => {
const contents = `
CONSTANT: constant
OPTIONAL:
value: ''
optional: true
`;
const parsed = parse(contents);
});

it('Parses valid document with stage', () => {
const contents = `
development:
Expand Down
4 changes: 2 additions & 2 deletions src/inputParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ export function parse(contents: string, stage?: string): InputDocument {
const value = document[key];
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
result[key] = { value: value };
} else if (value.optional && value.value) {
} else if (value.optional && typeof value.value !== 'undefined') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for using typeof here instead of just checking for undefined? Any issue with swapping that out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s the safest way to do this check. undefined can actually be reassigned so this has been established at the best practice.

https://stackoverflow.com/questions/4725603/variable-undefined-vs-typeof-variable-undefined

Happy to change if you want though.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

God I hate JavaScript sometimes lol

result[key] = {
value: value.value,
optional: value.optional
optional: value.optional,
};
} else {
throw new Error(`Parse error reading document. Invalid value: ${JSON.stringify(value)}`);
Expand Down