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

Feat: validate function should provide values as 2nd argument #273

Open
alexilyaev opened this issue Sep 6, 2020 · 2 comments · May be fixed by #382
Open

Feat: validate function should provide values as 2nd argument #273

alexilyaev opened this issue Sep 6, 2020 · 2 comments · May be fixed by #382

Comments

@alexilyaev
Copy link

Problem

Prompts chain of Start/End dates, End date should not be before Start date.

const responses = await prompts([
  {
    type: 'date',
    name: 'startDate',
    message: 'Start Date',
    initial: new Date(),
  },
  {
    type: 'date',
    name: 'endDate',
    message: 'End Date',
    initial: new Date(),
    validate(date) {
      // `endDate` should not be before `startDate`
    },
  },
]);

Solution

  {
    type: 'date',
    name: 'endDate',
    message: 'End Date',
    initial: new Date(),
    validate(date, values) {
      return date >= values.startDate || 'End Date should not be before Start Date';
    },
  },

Describe alternatives you've considered

Multiple prompts or onSubmit hacks as mentioned in #2.
If we already have validate, makes sense to allow access to previous answers.

I was also thinking about min and max values that could make sense (additionally)...
e.g. Once startDate is selected, endDate could have min: (prev, values) => values.startDate and then the user can't select a date before that date.

Additional context

Maybe it makes sense, for consistency, to add prompt as the 3rd argument, just like some of the other props https://github.com/terkelg/prompts#-prompt-objects. Though I'm not sure what would be the use case for that.
Btw, format doesn't get prompt as a 3rd argument but the docs in the same section say:

The function signature is (prev, values, prompt)

@mercmobily
Copy link

mercmobily commented Jan 10, 2021

I just wanted to confirm that I really badly need this too. I tried to look into the code, and got a little lost (although I am drowning in my own code right now)

My solution (hopefully temporary, to be fixed once this issue is addressed) was to do this:

  let globalPrev

  const questions = [
    {
      type: 'select',
      name: 'destination',
      message: 'Destination element',
      choices: anchorPoints()
    },
    {
      type: 'text',
      name: 'subPath',
      message: (prev) => { globalPrev = prev; return `Nested URL, nested in ${prev}` },
      validate: (value) => {
        // The actual validator will have access to 'prev'
        return utils.pagePathValidator(config, value, globalPrev)
      }
    }
  ]

Not ideal but it does the job...

@DesignByOnyx DesignByOnyx linked a pull request Jan 7, 2023 that will close this issue
@DesignByOnyx
Copy link

DesignByOnyx commented Jan 7, 2023

I too need this badly - I took a stab at it and have tested it on my current project. Please provide some feedback as I'd love to get this in (see auto-linked message above).

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

Successfully merging a pull request may close this issue.

3 participants