2

I really like the idea of react proptypes and am wondering if they can be used to validate the input of any given function, not just the props being passed to a react component.

For example:

function doSomething (thing1, thing2) {
    Proptypes.validate(arguments, [Proptypes.string, Proptypes.number]);
}

Is there a way of getting proptypes to do this?

Thanks!

5
  • What are you envisioning validate would do? You're not planning on using exceptions in your "purely functional code", are you? ಠ_ಠ Commented Sep 7, 2015 at 8:48
  • Ideally this would be a way to do type checking during development, to make sure that the arguments to a function are actually of the correct type. It would also serve as a sort of documentation. Commented Sep 7, 2015 at 8:52
  • Oh, and it would be nice to be able to remove it completely when running the code in production! Commented Sep 7, 2015 at 8:54
  • Maybe you'd be interested in TypeScript? Commented Sep 7, 2015 at 9:00
  • I'm not sure that's what I necessarily want because I don't think it is able to give errors if a module is being used from another file. Like say I call a function from a separate file and pass incorrect data, I don't think the typescript would catch that currently..? Maybe I'm wrong about that. Commented Sep 7, 2015 at 9:37

2 Answers 2

3

Yes they can although you'll need to pass some extraneous params.

You'd probably want to do a wrapper function to make life easier but, don't forget that the React.PropTypes are just functions themselves.

For example:

React.PropTypes.string(props, propName, componentName)

Where props is the entire props object for a component, propName is the name (and key) of the prop being checked, and componentName is the name of the component you're calling it from.

Although I think it would probably be better to roll your own as it would be a very small amount of code to do this yourself and it would shield you against any potential changes to how React.PropTypes work in future versions of React.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you know if anyone has already written such a wrapper? It seems like validating the arguments of a function would be a very generic need.
Not sure, I've usually written my own in various projects due to needs being slightly different each time. In sure someone must have made a generic one though!
1

Prop validators are functions, so you can just call them. As the customProp example in the Prop Validation doc illustrates, the arguments are props, propName, and componentName. The function returns null on success or an Error on failure. For example:

React.PropTypes.number({'a': 4}, 'a', 'foo')
// null

React.PropTypes.number({'a': 'b'}, 'a', 'foo')
// Error: Invalid undefined `a` of type `string`
// supplied to `foo`, expected `number`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.