[API] Fix bug and extract validator

This commit is contained in:
syuilo 2017-03-01 20:35:54 +09:00
parent 4b9067914e
commit 4f5928c575
2 changed files with 7 additions and 3 deletions

View File

@ -5,7 +5,7 @@
*/ */
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import User from '../../models/user'; import User from '../../models/user';
import { isValidBirthday } from '../../models/user'; import { isValidName, 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';
@ -25,8 +25,8 @@ module.exports = async (params, user, _, isSecure) =>
// Get 'name' parameter // Get 'name' parameter
const name = params.name; const name = params.name;
if (name !== undefined && name !== null) { if (name !== undefined && name !== null) {
if (name.length > 50) { if (!isValidName(name)) {
return rej('too long name'); return rej('invalid name');
} }
user.name = name; user.name = name;

View File

@ -15,6 +15,10 @@ export function validatePassword(password: string): boolean {
return typeof password == 'string' && password != ''; return typeof password == 'string' && password != '';
} }
export function isValidName(name: string): boolean {
return typeof name == 'string' && name.length > 50 && name.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);
} }