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

Feature request: Allow validator to accept async functions #142

Open
Ayc0 opened this issue Mar 20, 2023 · 0 comments
Open

Feature request: Allow validator to accept async functions #142

Ayc0 opened this issue Mar 20, 2023 · 0 comments

Comments

@Ayc0
Copy link
Contributor

Ayc0 commented Mar 20, 2023

Hello 👋

I'm using clipanion in a repositories with a lot of CLIs. To avoid 1 CLI to slow down all others, we follow the guidelines here https://mael.dev/clipanion/docs/tips#lazy-evaluation: we moved all our top level imports within the Command classes (except for node builtins and for clipanion).

But when we have to use enums that come from other files for the validator, we have to use a top level import, something like:

import { Command, Option } from 'clipanion';
import * as t from 'typanion';

import { PossibleValues } from './other-file';

class MyCommand extends Command {
    static paths = [['my', 'command']];
    entries: string[] = Option.Array('--entries', [], {
        validator: t.isArray(t.isEnum(PossibleValues)),
    });
    // ...
}

But now, we don't have any control anymore on other-file, and this file could block the main thread without realizing it on load.

Could we introduce the ability for clipanion to support async functions in validator?
This would allow to re-write this code this way:

import { Command, Option } from 'clipanion';
import * as t from 'typanion';

class MyCommand extends Command {
    static paths = [['my', 'command']];
    entries: string[] = Option.Array('--entries', [], {
        validator: async (value) => {
            const { PossibleValues } = await import('./other-file');
            return t.isArray(t.isEnum(PossibleValues))
        },
    });
    // ...
}

Which is also compliant with the recommendation in the doc for lazy evaluations.

If this is accepted, I can try writing a PR for it

@Ayc0 Ayc0 changed the title Allow validator to accept async functions Feature request: Allow validator to accept async functions Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant