Skip to content

Parameter validation #662

@joe307bad

Description

@joe307bad

Hey there, I am building a CLI tool with Gluegun and I was wondering if there is any best practice around validating parameters. Right now I am implementing something like this:

export const enum VALIDATOR {
    string = 0,
    required = 1
}

export type TCommandValidators = { [key: string]: [string, VALIDATOR[]][] }

const deployCommandValidators: TCommandValidators =
    {
        arguments: [
            [
                'filePath',
                [
                    VALIDATOR.string,
                    VALIDATOR.required
                ]
            ]
        ]
    };

const deploy: GluegunCommand = {
    name: 'deploy',
    alias: 'd',
    run: toolbox => validator(
        deployCommandValidators,
        toolbox
    )((toolbox: Toolbox) => {
        const {print} = toolbox;
        const {filePath, fileName} = toolbox.parameters.options;

        new CdnDeploy()
            .upload(filePath, fileName)
            .then(response => print.info(response[1]))
            .catch(error => print.error(error));
    })
};

And validator is a function that loops through the given validators and produces a collection of errors to display to the user.

Questions:

  • Should I build an extension for this?
  • Is there any built in parameter validation for Gluegun?

Thanks! I love the project!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplannedWe intend to add this feature or refactor in the future.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions