From 6b6adeaa68a92d128179f97e40102d4d869ccebf Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 2 Apr 2018 04:52:11 +0900 Subject: [PATCH] Refactor --- src/models/user.ts | 52 +++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/models/user.ts b/src/models/user.ts index bbe9b5a53..2d1693573 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -15,30 +15,6 @@ User.createIndex('account.token'); export default User; -export function validateUsername(username: string): boolean { - return typeof username == 'string' && /^[a-zA-Z0-9\-]{3,20}$/.test(username); -} - -export function validatePassword(password: string): boolean { - return typeof password == 'string' && password != ''; -} - -export function isValidName(name: string): boolean { - return typeof name == 'string' && name.length < 30 && name.trim() != ''; -} - -export function isValidDescription(description: string): boolean { - return typeof description == 'string' && description.length < 500 && description.trim() != ''; -} - -export function isValidLocation(location: string): boolean { - return typeof location == 'string' && location.length < 50 && location.trim() != ''; -} - -export function isValidBirthday(birthday: string): boolean { - return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday); -} - type IUserBase = { _id: mongo.ObjectID; createdAt: Date; @@ -61,8 +37,6 @@ type IUserBase = { hostLower: string; }; -export type IUser = ILocalUser | IRemoteUser; - export interface ILocalUser extends IUserBase { host: null; account: { @@ -108,12 +82,38 @@ export interface IRemoteUser extends IUserBase { }; } +export type IUser = ILocalUser | IRemoteUser; + export const isLocalUser = (user: any): user is ILocalUser => user.host === null; export const isRemoteUser = (user: any): user is IRemoteUser => !isLocalUser(user); +export function validateUsername(username: string): boolean { + return typeof username == 'string' && /^[a-zA-Z0-9\-]{3,20}$/.test(username); +} + +export function validatePassword(password: string): boolean { + return typeof password == 'string' && password != ''; +} + +export function isValidName(name: string): boolean { + return typeof name == 'string' && name.length < 30 && name.trim() != ''; +} + +export function isValidDescription(description: string): boolean { + return typeof description == 'string' && description.length < 500 && description.trim() != ''; +} + +export function isValidLocation(location: string): boolean { + return typeof location == 'string' && location.length < 50 && location.trim() != ''; +} + +export function isValidBirthday(birthday: string): boolean { + return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday); +} + export function init(user): IUser { user._id = new mongo.ObjectID(user._id); user.avatarId = new mongo.ObjectID(user.avatarId);