Skip to content

Commit b18b235

Browse files
authored
Merge pull request #582 from velopert/fix/validate-inputs
input 값 검증하기
2 parents fe9e941 + 35e0a13 commit b18b235

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/components/write/TagInput.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import transitions from '../../lib/styles/transitions';
66
import { mediaQuery } from '../../lib/styles/media';
77
import { useTransition, animated } from 'react-spring';
88
import OutsideClickHandler from 'react-outside-click-handler';
9+
import { isEmptyOrWhitespace } from '../../lib/utils';
910

1011
export interface TagInputProps {
1112
ref?: React.RefObject<HTMLDivElement>;
@@ -48,9 +49,8 @@ const TagInput: React.FC<TagInputProps> = ({ onChange, tags: initialTags }) => {
4849
(tag: string) => {
4950
ignore.current = true;
5051
setValue('');
51-
if (tag === '' || tags.includes(tag)) return;
52-
let processed = tag;
53-
processed = tag.trim().slice(0,255);
52+
if (isEmptyOrWhitespace(tag) || tags.includes(tag)) return;
53+
let processed = tag.trim().slice(0, 255);
5454
if (processed.indexOf(' #') > 0) {
5555
const tempTags: string[] = [];
5656
const regex = /#(\S+)/g;

src/containers/register/RegisterFormContainer.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { withRouter, RouteComponentProps } from 'react-router-dom';
1616
import qs from 'qs';
1717
import { useApolloClient } from '@apollo/react-hooks';
1818
import { GET_CURRENT_USER } from '../../lib/graphql/user';
19+
import { isEmpty, trim } from 'ramda';
20+
import { isEmptyOrWhitespace } from '../../lib/utils';
1921

2022
interface RegisterFormContainerProps extends RouteComponentProps<{}> {}
2123

@@ -65,16 +67,21 @@ const RegisterFormContainer: React.FC<RegisterFormContainerProps> = ({
6567
const onSubmit = async (form: RegisterFormType) => {
6668
setError(null);
6769
// validate
70+
6871
const validation = {
6972
displayName: (text: string) => {
70-
if (text.trim() === '') {
71-
return '이름을 입력해주세요.';
73+
if (isEmptyOrWhitespace(text)) {
74+
return '프로필 이름을 입력해주세요.';
7275
}
7376
if (text.trim().length > 45) {
7477
return '이름은 최대 45자까지 입력 할 수 있습니다.';
7578
}
7679
},
7780
username: (text: string) => {
81+
if (isEmptyOrWhitespace(text)) {
82+
return '사용자 ID를 입력해주세요.';
83+
}
84+
7885
if (!/^[a-z0-9-_]{3,16}$/.test(text)) {
7986
return '사용자 ID는 3~16자의 알파벳 소문자,숫자,혹은 - _ 으로 이루어져야 합니다.';
8087
}

src/lib/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import distanceInWordsToNow from 'date-fns/formatDistanceToNow';
22
import format from 'date-fns/format';
33
import koLocale from 'date-fns/locale/ko';
4+
import { isEmpty, trim } from 'ramda';
45

56
export const formatDate = (date: string): string => {
67
const d = new Date(date);
@@ -115,3 +116,10 @@ export const createFallbackTitle = (username: string | null) => {
115116
};
116117

117118
export const ssrEnabled = process.env.REACT_APP_SSR === 'enabled';
119+
120+
export const isEmptyOrWhitespace = (str: string) => {
121+
if (typeof str !== 'string') {
122+
return isEmpty(str);
123+
}
124+
return isEmpty(trim(str.replace(/\s/g, '')));
125+
};

0 commit comments

Comments
 (0)