Skip to content

kbn-config-schema: allow parsing of array and object types from string#57189

Merged
pgayvallet merged 5 commits intoelastic:masterfrom
pgayvallet:kbn-56951-schema-parse-strings
Feb 11, 2020
Merged

kbn-config-schema: allow parsing of array and object types from string#57189
pgayvallet merged 5 commits intoelastic:masterfrom
pgayvallet:kbn-56951-schema-parse-strings

Conversation

@pgayvallet
Copy link
Copy Markdown
Contributor

@pgayvallet pgayvallet commented Feb 10, 2020

Summary

Fix #56951

Allow array, object, map and record types to accept string representation of their value and parse them

This was already the case for other primitive types such as boolean and number, and allows object-type parameters to be properly parsed / validated when used in query string or in multipart type payloads.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@pgayvallet pgayvallet added Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// Feature:New Platform v8.0.0 release_note:skip Skip the PR/issue when compiling release notes v7.7.0 labels Feb 10, 2020
@pgayvallet pgayvallet requested a review from a team as a code owner February 10, 2020 12:00
@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/kibana-platform (Team:Platform)

@pgayvallet pgayvallet changed the title kbn-config-schema: allow parsing of arrays and object types from string kbn-config-schema: allow parsing of array and object types from string Feb 10, 2020
if (isPlainObject(parsed)) {
return parsed;
}
return this.createError('object.base', { value: parsed }, state, options);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm using the parsed value to create the error message here. "[1,2,3]" will fails with an error message stating that it received an array instead of an object, even if the actual raw input was a string.I think using the parsed value type makes more sense / is more clear, as least for route validation where the parsing is implicit for query and or multipart/form-data data.

Comment on lines -34 to +57
expect(() => type.validate('test', {}, 'foo-namespace')).toThrowErrorMatchingSnapshot();
expect(() => type.validate('test', {}, 'foo-namespace')).toThrowErrorMatchingInlineSnapshot(
`"[foo-namespace]: could not parse array value from [test]"`
);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I switched to inline snapshots. Should probably have done this in a separate commit though. No snapshot did change except the ones that were using string data as invalid type, as the error changed to could not parse.... I did adapt the data on these tests though.

if (isPlainObject(value)) {
return new Map(Object.entries(value));
}
if (options.convert && typeof value === 'string') {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note: I'm checking options.convert here to see if we should try to convert the value, However this option is never set in our library, and will always default to the default joi value which is true. It was more like a futur-proof thing if we want to implement this option at some point.

@pgayvallet pgayvallet requested a review from a team as a code owner February 10, 2020 13:10
Copy link
Copy Markdown
Contributor

@jportner jportner left a comment

Choose a reason for hiding this comment

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

Security changes LGTM.

Comment on lines 95 to 97
).toThrowErrorMatchingInlineSnapshot(
`"[disabledFeatures]: expected value of type [array] but got [string]"`
`"[disabledFeatures]: could not parse array value from [foo]"`
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Copy Markdown
Contributor

@joshdover joshdover left a comment

Choose a reason for hiding this comment

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

Can we add some details about this feature to the README.md for this package?

@kibanamachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

@pgayvallet pgayvallet merged commit c13b075 into elastic:master Feb 11, 2020
pgayvallet added a commit to pgayvallet/kibana that referenced this pull request Feb 11, 2020
elastic#57189)

* allow parsing from string for object-ish and array types

* update snapshots

* fix FTR assertion

* add documentation note about using a json string as input
pgayvallet added a commit that referenced this pull request Feb 11, 2020
#57189) (#57298)

* allow parsing from string for object-ish and array types

* update snapshots

* fix FTR assertion

* add documentation note about using a json string as input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Feature:New Platform release_note:skip Skip the PR/issue when compiling release notes Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// v7.7.0 v8.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NP] route validation does not properly parse multipart/form-data payloads

5 participants