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

Update Validator Input Options Behavior #1579

Merged
merged 3 commits into from
Feb 18, 2022
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
5 changes: 5 additions & 0 deletions .changeset/angry-windows-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/ea-bootstrap': patch
---

Update Validator behavior with input options sets
91 changes: 52 additions & 39 deletions packages/core/bootstrap/src/lib/modules/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class Validator {
? this.validateRequiredParam(this.input.data[key], key, options)
: this.validateOptionalParam(this.input.data[key], key, options)
} else {
this.validateObjectParam(key)
this.validateObjectParam(key, this.validatorOptions.shouldThrowError)
}
}
} catch (e) {
Expand Down Expand Up @@ -207,7 +207,7 @@ export class Validator {
throw new AdapterError({ jobRunID: this.validated.id, statusCode: 400, message })
}

validateObjectParam(key: string): void {
validateObjectParam(key: string, shouldThrowError = true): void {
const inputConfig = this.inputConfigs[key] as InputParameter

const usedKey = this.getUsedKey(key, inputConfig.aliases ?? [])
Expand All @@ -216,52 +216,65 @@ export class Validator {
? this.input.data[usedKey as string] ?? inputConfig.default
: inputConfig.default

const paramIsDefined = !(param === undefined || param === null || param === '')
if (shouldThrowError) {
const paramIsDefined = !(param === undefined || param === null || param === '')

if (inputConfig.required && !paramIsDefined)
this.throwInvalid(`Required parameter ${key} must be non-null and non-empty`)
if (inputConfig.required && !paramIsDefined)
this.throwInvalid(`Required parameter ${key} must be non-null and non-empty`)

if (paramIsDefined) {
if (inputConfig.type) {
const primitiveTypes = ['boolean', 'number', 'bigint', 'string']
if (paramIsDefined) {
if (inputConfig.type) {
const primitiveTypes = ['boolean', 'number', 'bigint', 'string']

if (![...primitiveTypes, 'array', 'object'].includes(inputConfig.type))
this.throwInvalid(`${key} parameter has unrecognized type ${inputConfig.type}`)
if (![...primitiveTypes, 'array', 'object'].includes(inputConfig.type))
this.throwInvalid(`${key} parameter has unrecognized type ${inputConfig.type}`)

if (primitiveTypes.includes(inputConfig.type) && typeof param !== inputConfig.type)
this.throwInvalid(`${key} parameter must be of type ${inputConfig.type}`)
if (primitiveTypes.includes(inputConfig.type) && typeof param !== inputConfig.type)
this.throwInvalid(`${key} parameter must be of type ${inputConfig.type}`)

if (inputConfig.type === 'array' && (!Array.isArray(param) || param.length === 0))
this.throwInvalid(`${key} parameter must be a non-empty array`)
if (inputConfig.type === 'array' && (!Array.isArray(param) || param.length === 0))
this.throwInvalid(`${key} parameter must be a non-empty array`)

if (
inputConfig.type === 'object' &&
(!param ||
Array.isArray(param) ||
typeof param !== inputConfig.type ||
Object.keys(param).length === 0)
)
this.throwInvalid(`${key} parameter must be an object with at least one property`)
}
if (
inputConfig.type === 'object' &&
(!param ||
Array.isArray(param) ||
typeof param !== inputConfig.type ||
Object.keys(param).length === 0)
)
this.throwInvalid(`${key} parameter must be an object with at least one property`)
}

if (inputConfig.options && !inputConfig.options.includes(param))
this.throwInvalid(`${key} parameter is not in the set of available options`)
if (inputConfig.options) {
const tolcase = (o: any) => (typeof o === 'string' ? o.toLowerCase() : o)

for (const dependency of inputConfig.dependsOn ?? []) {
const usedDependencyKey = this.getUsedKey(
dependency,
(this.inputConfigs[dependency] as InputParameter).aliases ?? [],
)
if (!usedDependencyKey) this.throwInvalid(`${key} dependency ${dependency} not supplied`)
}
const formattedOptions = inputConfig.options.map(tolcase)
const formattedParam = tolcase(param)

for (const exclusive of inputConfig.exclusive ?? []) {
const usedExclusiveKey = this.getUsedKey(
exclusive,
(this.inputConfigs[exclusive] as InputParameter).aliases ?? [],
)
if (usedExclusiveKey)
this.throwInvalid(`${key} cannot be supplied concurrently with ${exclusive}`)
if (!formattedOptions.includes(formattedParam))
this.throwInvalid(
`${key} parameter '${formattedParam}' is not in the set of available options: ${formattedOptions.join(
',',
)}`,
)
}

for (const dependency of inputConfig.dependsOn ?? []) {
const usedDependencyKey = this.getUsedKey(
dependency,
(this.inputConfigs[dependency] as InputParameter).aliases ?? [],
)
if (!usedDependencyKey) this.throwInvalid(`${key} dependency ${dependency} not supplied`)
}

for (const exclusive of inputConfig.exclusive ?? []) {
const usedExclusiveKey = this.getUsedKey(
exclusive,
(this.inputConfigs[exclusive] as InputParameter).aliases ?? [],
)
if (usedExclusiveKey)
this.throwInvalid(`${key} cannot be supplied concurrently with ${exclusive}`)
}
}
}

Expand Down
Loading