Skip to content

Instantly share code, notes, and snippets.

@bmakuh
Created August 25, 2016 17:36
Show Gist options
  • Save bmakuh/23b0f270bf2542acd118f2f5c3eb2078 to your computer and use it in GitHub Desktop.
Save bmakuh/23b0f270bf2542acd118f2f5c3eb2078 to your computer and use it in GitHub Desktop.
React URL propType
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