This commit is contained in:
syuilo 2017-03-03 09:24:17 +09:00
parent 665bcda0a7
commit 42ed35bfc9
2 changed files with 24 additions and 35 deletions

View file

@ -5,7 +5,7 @@
*/ */
import it from '../../it'; import it from '../../it';
import User from '../../models/user'; import User from '../../models/user';
import { isValidName, isValidBirthday } from '../../models/user'; import { isValidName, isValidDescription, isValidLocation, isValidBirthday } from '../../models/user';
import serialize from '../../serializers/user'; import serialize from '../../serializers/user';
import event from '../../event'; import event from '../../event';
import config from '../../../conf'; import config from '../../../conf';
@ -28,48 +28,29 @@ module.exports = async (params, user, _, isSecure) =>
if (name) user.name = name; if (name) user.name = name;
// Get 'description' parameter // Get 'description' parameter
const description = params.description; const [description, descriptionErr] = it(params.description).expect.string().validate(isValidDescription).qed();
if (description !== undefined && description !== null) { if (descriptionErr) return rej('invalid description param');
if (description.length > 500) { if (description !== undefined) user.description = description;
return rej('too long description');
}
user.description = description;
}
// Get 'location' parameter // Get 'location' parameter
const location = params.location; const [location, locationErr] = it(params.location).expect.string().validate(isValidLocation).qed();
if (location !== undefined && location !== null) { if (locationErr) return rej('invalid location param');
if (location.length > 50) { if (location !== undefined) user.location = location;
return rej('too long location');
}
user.profile.location = location;
}
// Get 'birthday' parameter // Get 'birthday' parameter
const birthday = params.birthday; const [birthday, birthdayErr] = it(params.birthday).expect.string().validate(isValidBirthday).qed();
if (birthday != null) { if (birthdayErr) return rej('invalid birthday param');
if (!isValidBirthday(birthday)) { if (birthday !== undefined) user.birthday = birthday;
return rej('invalid birthday');
}
user.profile.birthday = birthday;
} else {
user.profile.birthday = null;
}
// Get 'avatar_id' parameter // Get 'avatar_id' parameter
const avatar = params.avatar_id; const [avatarId, avatarIdErr] = it(params.avatar_id).expect.id().notNull().qed();
if (avatar !== undefined && avatar !== null) { if (avatarIdErr) return rej('invalid avatar_id param');
user.avatar_id = new mongo.ObjectID(avatar); if (avatarId) user.avatar_id = avatarId;
}
// Get 'banner_id' parameter // Get 'banner_id' parameter
const banner = params.banner_id; const [bannerId, bannerIdErr] = it(params.banner_id).expect.id().notNull().qed();
if (banner !== undefined && banner !== null) { if (bannerIdErr) return rej('invalid banner_id param');
user.banner_id = new mongo.ObjectID(banner); if (bannerId) user.banner_id = bannerId;
}
await User.update(user._id, { await User.update(user._id, {
$set: { $set: {

View file

@ -19,6 +19,14 @@ export function isValidName(name: string): boolean {
return typeof name == 'string' && name.length < 30 && name.trim() != ''; 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 { export function isValidBirthday(birthday: string): boolean {
return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday); return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday);
} }