All files extensionParameterValidator.ts

100% Statements 94/94
100% Branches 34/34
100% Functions 7/7
100% Lines 94/94

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 941x 1x 1x 1x 1x 29x 29x 29x 1x 47x 47x 18x 9x 9x 9x 9x 47x 29x 29x 29x 12x 29x 9x 29x 7x 29x 1x 29x 29x 47x 1x 3x 3x 3x 4x 4x 3x 3x 4x 3x 1x 3x 2x 2x 3x 1x 4x 4x 4x 3x 4x 1x 1x 4x 1x 12x 12x 10x 6x 6x 10x 6x 6x 1x 7x 7x 7x 1x 1x 7x 3x 3x 3x 3x 3x 1x 9x 9x 2x 2x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x
import { ParameterValues } from "@exasol/extension-manager-interface";
import { Parameter, SelectParameter, StringParameter } from "@exasol/extension-manager-interface/dist/parameters";
 
const SUCCESS_RESULT: ValidationResultSuccess = { success: true };
 
function validationError(errorMessage: string): ValidationResultFailure {
    return { success: false, message: errorMessage }
}
 
export function validateParameter(definition: Parameter, value: string | undefined): ValidationResult {
    if (value === undefined || value === null || value === "") {
        if (definition.required) {
            return validationError("This is a required parameter.")
        } else {
            return SUCCESS_RESULT;
        }
    } else {
        const definitionType = definition.type
        switch (definition.type) {
            case "string":
                return validateStringParameter(definition, value);
            case "boolean":
                return validateBooleanParameter(value);
            case "select":
                return validateSelectParameter(definition, value)
            default:
                return validationError(`unsupported parameter type '${definitionType}'`);
        }
    }
}
 
export function validateParameters(definitions: Parameter[], values: ParameterValues): ValidationResult {
    const findings: string[] = []
    for (const definition of definitions) {
        const singleResult = validateParameter(definition, getValue(definition.id, values))
        if (singleResult.success === false) {
            findings.push(`${definition.name}: ${singleResult.message}`)
        }
    }
    if (findings.length == 0) {
        return SUCCESS_RESULT
    } else {
        return validationError(findings.join("\n"))
    }
}
 
function getValue(id: string, values: ParameterValues): string | undefined {
    const value = values.values.find(v => v.name === id);
    if (value) {
        return value.value
    } else {
        return undefined
    }
}
 
function validateStringParameter(definition: StringParameter, value: string) {
    if (definition.regex) {
        if (!new RegExp(definition.regex).test(value)) {
            return validationError("The value has an invalid format.")
        }
    }
    return SUCCESS_RESULT
}
 
function validateSelectParameter(definition: SelectParameter, value: string) {
    const possibleValues = definition.options.map(option => option.id)
    if (possibleValues.length === 0) {
        return validationError("No option available for this parameter.")
    }
    if (possibleValues.includes(value)) {
        return SUCCESS_RESULT
    }
    const quotedValues = possibleValues.map(value => `'${value}'`).join(', ')
    return validationError(`The value is not allowed. Possible values are ${quotedValues}.`)
}
 
function validateBooleanParameter(value: string) {
    if (value === "true" || value === "false") {
        return SUCCESS_RESULT
    }
    return validationError("Boolean value must be 'true' or 'false'.")
}
 
export type ValidationResult = ValidationResultSuccess | ValidationResultFailure
 
export interface ValidationResultSuccess {
    success: true
}
 
export interface ValidationResultFailure {
    success: false
    /** Validation error description. If multiples errors were found they are separated by \n. */
    message: string
}