-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Review and update prop validation logic for accuracy #14986
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
Merged
adolfo-pd
merged 8 commits into
master
from
adolfo/money-1953-submit-button-not-enabled-despite-all-props-configured
Dec 13, 2024
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
042904c
Review and update all prop value validation for accuracy
adolfo-pd a84b407
Ignore string prop values that don't match string type
adolfo-pd 678cc14
Merge branch 'master' into adolfo/money-1953-submit-button-not-enable…
adolfo-pd 98a2706
bad error check
adolfo-pd 1cdf77f
CR suggestions
adolfo-pd c2ef751
Another CR suggestion
adolfo-pd 6805a60
Versioning
adolfo-pd 7c6e6c9
Another bug with translating options
adolfo-pd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import type { | ||
| App, ConfigurableProp, ConfigurablePropApp, ConfigurablePropBoolean, ConfigurablePropInteger, ConfigurablePropString, ConfigurablePropStringArray, PropValue, | ||
| } from "@pipedream/sdk"; | ||
|
|
||
| export type PropOptionValue<T> = { | ||
| __lv: { | ||
| value: T | ||
| } | ||
| } | ||
|
|
||
| export function valueFromOption<T>(value: T | PropOptionValue<T>): T | undefined | null { | ||
| if (typeof value === "object" && value && "__lv" in value) { | ||
| return (value as PropOptionValue<T>).__lv.value | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| export type PropOption<T> = { | ||
| emitValue: T | PropOptionValue<T> | ||
| } | ||
| export type PropOptions<T> = { | ||
| selectedOptions: PropOption<T>[] | ||
| } | ||
|
|
||
| export function valuesFromOptions<T>(value: unknown | T[] | PropOptions<T>): T[] { | ||
| if (typeof value === "object" && value && "selectedOptions" in value && Array.isArray(value.selectedOptions)) { | ||
| const selectedOptions = value.selectedOptions as PropOption<T>[] | ||
| const results = [] | ||
| for (const so of selectedOptions) { | ||
| if (typeof so === "object" && so && "emitValue" in so) { | ||
| const emitValue = so.emitValue as T | PropOptionValue<T> | ||
| if (typeof emitValue === "object" && emitValue && "__lv" in emitValue) { | ||
| results.push(emitValue.__lv.value) | ||
| } | ||
| results.push(emitValue) | ||
| } else { | ||
| throw "unexpected value" | ||
| } | ||
| } | ||
| } | ||
| if (!Array.isArray(value)) | ||
| throw "unexpected value" | ||
| return value as T[] | ||
| } | ||
|
|
||
| export type ValidationOpts<T extends ConfigurableProp> = { | ||
| prop: T | ||
| value: unknown | ||
| app?: App | ||
| } | ||
|
|
||
| export function arrayPropErrors(opts: ValidationOpts<ConfigurablePropStringArray>): string[] | undefined { | ||
| const _values = valuesFromOptions(opts.value) | ||
| if (!opts.prop.default && typeof _values === "undefined") { | ||
| return [ | ||
| "required", | ||
| ] | ||
| } | ||
| if (!opts.prop.default && Array.isArray(_values) && !_values.length) return [ | ||
| "empty array", | ||
| ] | ||
| } | ||
adolfo-pd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export function booleanPropErrors(opts: ValidationOpts<ConfigurablePropBoolean>): string[] | undefined { | ||
| const _value = valueFromOption(opts.value) | ||
| if (_value == null || typeof _value === "undefined") return [ | ||
| "required", | ||
| ] | ||
| } | ||
adolfo-pd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export function integerPropErrors(opts: ValidationOpts<ConfigurablePropInteger>): string[] | undefined { | ||
| const { | ||
| prop, value: valueOpt, | ||
| } = opts | ||
| const value = valueFromOption(valueOpt) | ||
|
|
||
| if (!prop.default && value == null || typeof value === "undefined") return [ | ||
| "required", | ||
| ] | ||
adolfo-pd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const _value: number = typeof value === "number" | ||
| ? value | ||
| : parseInt(String(value)) | ||
|
|
||
| if (_value !== _value) return [ | ||
| "not a number", | ||
| ] // NaN | ||
adolfo-pd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const errs = [] | ||
| if (typeof prop.min === "number" && _value < prop.min) errs.push("number too small") | ||
| if (typeof prop.max === "number" && _value > prop.max) errs.push("number too big") | ||
| return errs | ||
| } | ||
|
|
||
| export function stringPropErrors(opts: ValidationOpts<ConfigurablePropString>): string[] | undefined { | ||
| const _value = valueFromOption(opts.value) | ||
|
|
||
| if (!opts.prop.default) { | ||
| if (typeof _value === "undefined" || _value == null) return [ | ||
| "required", | ||
| ] | ||
| if (!String(_value).length) return [ | ||
| "string must not be empty", | ||
| ] | ||
| } | ||
| } | ||
adolfo-pd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| type AppWithExtractedCustomFields = App & { | ||
| extracted_custom_fields_names: string[] | ||
| } | ||
|
|
||
| type AppCustomField = { | ||
| name: string | ||
| optional?: boolean | ||
| } | ||
|
|
||
| type OauthAppPropValue = PropValue<"app"> & { | ||
| oauth_access_token: string | ||
| } | ||
|
|
||
| type AppPropValueWithCustomFields<T extends AppCustomField[]> = PropValue<"app"> & { | ||
| [K in T[number]["name"]]: T[number] | ||
| } | ||
|
|
||
| function getCustomFields(app: App): AppCustomField[] { | ||
| const isOauth = app.auth_type === "oauth" | ||
| const userDefinedCustomFields = JSON.parse(app.custom_fields_json || "[]") | ||
| if ("extracted_custom_fields_names" in app && app.extracted_custom_fields_names) { | ||
| const extractedCustomFields = ((app as AppWithExtractedCustomFields).extracted_custom_fields_names || []).map( | ||
| (name) => ({ | ||
| name, | ||
| }), | ||
| ) | ||
| userDefinedCustomFields.push(...extractedCustomFields) | ||
| } | ||
| return userDefinedCustomFields.map((cf: AppCustomField) => { | ||
| return { | ||
| ...cf, | ||
| // if oauth, treat all as optional (they are usually needed for getting access token) | ||
| optional: cf.optional || isOauth, | ||
| } | ||
| }) | ||
| } | ||
adolfo-pd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export function appPropErrors(opts: ValidationOpts<ConfigurablePropApp>): string[] | undefined { | ||
| const { | ||
| app, value, | ||
| } = opts | ||
| if (!app) { | ||
| return [ | ||
| "app field not registered", | ||
| ] | ||
| } | ||
| if (!value) { | ||
| return [ | ||
| "no app configured", | ||
| ] | ||
| } | ||
| if (typeof value !== "object") { | ||
| return [ | ||
| "not an app", | ||
| ] | ||
| } | ||
| const _value = value as PropValue<"app"> | ||
| if ("authProvisionId" in _value && !_value.authProvisionId) { | ||
| if (app.auth_type) { | ||
| const errs = [] | ||
| if (app.auth_type === "oauth" && !(_value as OauthAppPropValue).oauth_access_token) { | ||
| errs.push("missing oauth token") | ||
| } | ||
| if (app.auth_type === "oauth" || app.auth_type === "keys") { | ||
| const customFields = getCustomFields(app) | ||
| const _valueWithCustomFields = _value as AppPropValueWithCustomFields<typeof customFields> | ||
| for (const cf of customFields) { | ||
| if (!cf.optional && !_valueWithCustomFields[cf.name]) { | ||
| errs.push(`missing custom field: ${cf.name}`) | ||
| } | ||
| } | ||
| } | ||
| if (app.auth_type !== "none") | ||
| errs.push("no auth provision configured") | ||
| return errs | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.