Skip to content
This repository has been archived by the owner on Jul 9, 2022. It is now read-only.

Add user friendly prop-type validations #65

Open
axe312ger opened this issue Feb 27, 2020 · 0 comments
Open

Add user friendly prop-type validations #65

axe312ger opened this issue Feb 27, 2020 · 0 comments

Comments

@axe312ger
Copy link
Collaborator

Additionally to the common prop-types package, we could support and implement airbnb's version. It adds a lot of useful propTypes that allow us give users more context about what is possible. Even validation while editing should be possible.

https://github.com/airbnb/prop-types

These could be especially useful:

  • and: ensure that all provided propType validators pass
    • foo: and([number, nonNegativeInteger])
  • between: provide an object with an gt or gte number, and an lt or lte number (only one item allowed from each pairs; one or both pairs may be provided), and the resulting propType validator will ensure the prop value is a number within the given range. Alternatively, you can provide a function that takes the props object and returns a number for each of the gt/gte/lt/lte values.
    • foo: between({ gte: 0, lte: 5 })
    • foo: between({ gt: 0, lt: 5 })
  • elementType: require that the prop be a specific type of React element - takes a Component, an HTML tag name, or "*" to match everything.
    • foo: elementType('span')
    • foo: elementType(Component)
  • numericString: require the prop be a string that is conceptually numeric.
    • foo: numericString()
  • or: recursively allows only the provided propTypes, or arrays of those propTypes.
    • foo: or([bool.isRequired, explicitNull()])
  • range: provide a min, and a max, and the prop must be an integer in the range [min, max)
    • foo: range(-1, 2)
  • stringEndsWith: takes a non-empty string, and returns a validator that ensures the prop value is a string that ends with it.
    • foo: stringEndsWith('.png')
  • stringStartsWith: takes a non-empty string, and returns a validator that ensures the prop value is a string that starts with it.
    • foo: stringStartsWith('prefix-')
  • valuesOf: a non-object requiring PropTypes.objectOf. Takes a propType validator, and applies it to every own value on the propValue.
    • foo: valuesOf(number)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant