React Prop Validation for Date Strings

Bat

How can I validate date strings in the format yyyy/mm/dd?

I know I can use PropTypes.string but it is too loose.

0xc14m1z

You can basically copy-paste from prop-types documentation using a proper regex:

yourProp: function(props, propName, componentName) {
  if (!/\d{4}\/\d{2}\/\d{2}/.test(props[propName])) {
    return new Error(
      'Invalid prop `' + propName + '` supplied to' +
      ' `' + componentName + '`. Validation failed.'
    );
  }
}

Remember, the documentation is your friend: https://www.npmjs.com/package/prop-types

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

React prop validation for date objects

ESLint React PropTypes, 'prop' is missing in prop validation

new Date as react key prop?

missing in props validation react/prop-types

Failed prop type while doing prop validation in React.js

How to define prop validation rule for an object of objects in React?

Extending react prop-types validation with custom checking

Dispatch action is missing in props validation [react/prop-types]

Eslint - 'match' is missing in props validation (react/prop-types)

How to add prop validation for style object in React Native?

Line 5: 'tags' is missing in props validation react/prop-types

'children' is missing in props validation react/prop-types

Date validation - How localize/translate strings "today" and "tomorrow"

Check if prop passed validation

ESLint: '.map is missing in props validation' (eslint react/prop-types) in Typescript React

How to dynamically change the date for the date key in the items prop for react-native-calendars

You are manually calling a React.PropTypes validation function for the `path` prop on `Route`

Line 4:27: 'component' is missing in props validation react/prop-types

Using react-native-calendars, how to pass the pressed date back into 'markedDates' prop?

Invariant Violation: A date or time must be specified as `value` prop While using DateTimePicker -expo-react-native

children prop in React component

React class prop onclick

React - passing refs as a prop

React interdependent prop types

Ref as prop in Typescript in react

React Redux prop is undefined

What exactly is a "Prop" in React?

React pass function as prop

React Component as a prop