Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
PropTypes: common/url
Browse files Browse the repository at this point in the history
  • Loading branch information
artkravchenko committed Jun 28, 2016
1 parent bee9f53 commit 53ed385
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/prop-types/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,22 @@ export const date = createSimplifiedRequirableTypeChecker(
return null;
}
);

export const url = createSimplifiedRequirableTypeChecker(
(propValue, propFullName, componentName, location) => {
const expectedType = 'string';
if (typeof propValue !== expectedType) {
return getTypeError(propValue, expectedType, propFullName, componentName, location);
}

const test = RegExp(/^[a-z0-9_-]+$/i);
if (!propValue.match(test)) {
return new Error(
`Invalid prop \`${propFullName}\` of type \`${expectedType}\` ` +
`supplied to \`${componentName}\` is invalid URL representation.`
);
}

return null;
}
);
4 changes: 2 additions & 2 deletions src/prop-types/geotags.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { PropTypes } from 'react';

import CONTINENTS from '../consts/continents';

import { mapOf, uuid, date } from './common';
import { mapOf, uuid, date, url } from './common';
import { TagMore } from './tags';

const continentCodes = Object.keys(CONTINENTS);
Expand Down Expand Up @@ -49,7 +49,7 @@ export const Geotag = PropTypes.shape({
post_count: PropTypes.number.isRequired,
type: PropTypes.string, // oneOf("Country", ...)
updated_at: date.isRequired,
url_name: PropTypes.string // url formatted string
url_name: url.isRequired
});

export const Geotags = PropTypes.arrayOf(Geotag);
Expand Down
4 changes: 2 additions & 2 deletions src/prop-types/hashtags.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
*/
import { PropTypes } from 'react';

import { mapOf, uuid, date } from './common';
import { mapOf, uuid, date, url } from './common';
import { TagMore } from './tags';

export const Hashtag = PropTypes.shape({
_sphinx_id: PropTypes.string.isRequired,
created_at: date.isRequired,
id: uuid.isRequired,
name: PropTypes.string.isRequired,
name: url.isRequired,
more: TagMore,
post_count: PropTypes.number.isRequired,
updated_at: date.isRequired
Expand Down
6 changes: 3 additions & 3 deletions src/prop-types/schools.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
import { PropTypes } from 'react';

import { mapOf, uuid, date } from './common';
import { mapOf, uuid, date, url } from './common';
import { Attachment } from './attachments';
import { TagMore } from './tags';

Expand Down Expand Up @@ -52,14 +52,14 @@ export const School = PropTypes.shape({
teaching_languages: PropTypes.arrayOf(PropTypes.string),
twitter: PropTypes.string,
updated_at: date.isRequired,
url_name: PropTypes.string.isRequired,
url_name: url.isRequired,
website: PropTypes.string,
wikipedia: PropTypes.string
});

export const SchoolLight = PropTypes.shape({
id: uuid.isRequired,
url_name: PropTypes.string.isRequired,
url_name: url.isRequired,
name: PropTypes.string.isRequired
});

Expand Down

0 comments on commit 53ed385

Please sign in to comment.