vadimdemedes / pastel

🎨 Next.js-like framework for CLIs made with Ink
https://term.ink/pastel
MIT License
2.2k stars 36 forks source link

PropTypes functional type errors do not stop rendering #25

Closed jacobfike closed 1 year ago

jacobfike commented 5 years ago

When using the functional form of propTypes to do some conditional prop checking, errors result in a warning printed but do not prevent rendering like when a .isRequired prop is missing.

As an example, I want the serviceName and newSchema props to be linked such that if you provide neither it is ok, but if you provide one of them, you MUST provide both. I tried the following:

Component.propTypes = {
  serviceName: function (props, propName) {
    if (props['newSchema'] !== undefined && props[propName] === undefined) {
      return new Error('Please provide a --service-name value!');
    }
  },
  newSchema: function (props, propName) {
    if (props['serviceName'] !== undefined && props[propName] === undefined) {
      return new Error('Please provide a --new-schema value!');
    }
  }
}

But this results only in a warning being displayed and does not prevent the command from running. The warning looks like this:

Warning: Failed prop type: Please provide a --new-schema value!
    in Check

I would expect this to act like a required prop being omitted, resulting in the help text for the command being displayed with a message about which required prop is missing.

vadimdemedes commented 5 years ago

It's not possible to use functions for prop types, because Pastel staticly analyzes your source code and scans for PropTypes.string, PropTypes.number and others.

vadimdemedes commented 1 year ago

Pastel 2.0 just shipped which now uses Zod for options/arguments validation and parsing. It'll now definitely fail if input doesn't match the schema you defined.