Created
August 25, 2016 17:36
-
-
Save bmakuh/23b0f270bf2542acd118f2f5c3eb2078 to your computer and use it in GitHub Desktop.
React URL propType
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { curry } from 'lodash' | |
// isRequired :: Bool => True | |
export const isRequired = true | |
// url :: Bool? -> [Prop] -> String -> String -> Either(Error, Nothing) | |
export const url = curry(( | |
required = false, | |
props, | |
propName, | |
componentName, | |
location, | |
propFullName, | |
secret | |
) => { | |
const FALSEY = [undefined, null, ''].includes(props[propName]) | |
const URL_PATTERN = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi | |
const URL_ERROR = 'Invalid prop `' + propName + '` supplied to' + | |
' `' + componentName + '`: ' + propName + | |
' is not a URL. Validation failed.' | |
const isValid = URL_PATTERN.test(props[propName]) | |
return required | |
? isValid | |
? true | |
: new Error(URL_ERROR) | |
: isValid || FALSEY | |
? true | |
: new Error(URL_ERROR) | |
}) | |
export default { | |
isRequired, | |
url | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment