いい感じに

This commit is contained in:
syuilo 2017-03-03 10:24:56 +09:00
parent 3ca7a442ab
commit e0c44abd4a
2 changed files with 58 additions and 21 deletions

View File

@ -23,22 +23,22 @@ module.exports = async (params, user, _, isSecure) =>
new Promise(async (res, rej) =>
{
// Get 'name' parameter
const [name, nameErr] = it(params.name).expect.string().notNull().validate(isValidName).qed();
const [name, nameErr] = it(params.name).expect.string().validate(isValidName).qed();
if (nameErr) return rej('invalid name param');
if (name) user.name = name;
// Get 'description' parameter
const [description, descriptionErr] = it(params.description).expect.string().validate(isValidDescription).qed();
const [description, descriptionErr] = it(params.description).expect.nullable.string().validate(isValidDescription).qed();
if (descriptionErr) return rej('invalid description param');
if (description !== undefined) user.description = description;
// Get 'location' parameter
const [location, locationErr] = it(params.location).expect.string().validate(isValidLocation).qed();
const [location, locationErr] = it(params.location).expect.nullable.string().validate(isValidLocation).qed();
if (locationErr) return rej('invalid location param');
if (location !== undefined) user.location = location;
// Get 'birthday' parameter
const [birthday, birthdayErr] = it(params.birthday).expect.string().validate(isValidBirthday).qed();
const [birthday, birthdayErr] = it(params.birthday).expect.nullable.string().validate(isValidBirthday).qed();
if (birthdayErr) return rej('invalid birthday param');
if (birthday !== undefined) user.birthday = birthday;

View File

@ -74,10 +74,15 @@ class QueryCore implements Query {
value: any;
error: Error;
constructor(value: any) {
constructor(value: any, nullable: boolean = false) {
if (value === null && !nullable) {
this.value = undefined;
this.error = new Error('must-be-not-a-null');
} else {
this.value = value;
this.error = null;
}
}
get isUndefined() {
return this.value === undefined;
@ -166,8 +171,8 @@ class BooleanQuery extends QueryCore {
value: boolean;
error: Error;
constructor(value) {
super(value);
constructor(value: any, nullable: boolean = false) {
super(value, nullable);
if (!this.isEmpty && typeof value != 'boolean') {
this.error = new Error('must-be-a-boolean');
}
@ -201,8 +206,8 @@ class NumberQuery extends QueryCore {
value: number;
error: Error;
constructor(value) {
super(value);
constructor(value: any, nullable: boolean = false) {
super(value, nullable);
if (!this.isEmpty && !Number.isFinite(value)) {
this.error = new Error('must-be-a-number');
}
@ -273,8 +278,8 @@ class StringQuery extends QueryCore {
value: string;
error: Error;
constructor(value) {
super(value);
constructor(value: any, nullable: boolean = false) {
super(value, nullable);
if (!this.isEmpty && typeof value != 'string') {
this.error = new Error('must-be-a-string');
}
@ -351,8 +356,8 @@ class ArrayQuery extends QueryCore {
value: any[];
error: Error;
constructor(value) {
super(value);
constructor(value: any, nullable: boolean = false) {
super(value, nullable);
if (!this.isEmpty && !Array.isArray(value)) {
this.error = new Error('must-be-an-array');
}
@ -422,8 +427,8 @@ class IdQuery extends QueryCore {
value: mongo.ObjectID;
error: Error;
constructor(value) {
super(value);
constructor(value: any, nullable: boolean = false) {
super(value, nullable);
if (!this.isEmpty && (typeof value != 'string' || !mongo.ObjectID.isValid(value))) {
this.error = new Error('must-be-an-id');
}
@ -457,8 +462,8 @@ class ObjectQuery extends QueryCore {
value: any;
error: Error;
constructor(value) {
super(value);
constructor(value: any, nullable: boolean = false) {
super(value, nullable);
if (!this.isEmpty && typeof value != 'object') {
this.error = new Error('must-be-an-object');
}
@ -495,6 +500,14 @@ type It = {
string: () => StringQuery;
number: () => NumberQuery;
boolean: () => BooleanQuery;
nullable: {
string: () => StringQuery;
number: () => NumberQuery;
boolean: () => BooleanQuery;
id: () => IdQuery;
array: () => ArrayQuery;
object: () => ObjectQuery;
};
};
an: {
id: () => IdQuery;
@ -510,6 +523,14 @@ type It = {
id: () => IdQuery;
array: () => ArrayQuery;
object: () => ObjectQuery;
nullable: {
string: () => StringQuery;
number: () => NumberQuery;
boolean: () => BooleanQuery;
id: () => IdQuery;
array: () => ArrayQuery;
object: () => ObjectQuery;
};
};
};
@ -519,7 +540,15 @@ const it = (value: any) => ({
a: {
string: () => new StringQuery(value),
number: () => new NumberQuery(value),
boolean: () => new BooleanQuery(value)
boolean: () => new BooleanQuery(value),
nullable: {
string: () => new StringQuery(value, true),
number: () => new NumberQuery(value, true),
boolean: () => new BooleanQuery(value, true),
id: () => new IdQuery(value, true),
array: () => new ArrayQuery(value, true),
object: () => new ObjectQuery(value, true)
}
},
an: {
id: () => new IdQuery(value),
@ -534,7 +563,15 @@ const it = (value: any) => ({
boolean: () => new BooleanQuery(value),
id: () => new IdQuery(value),
array: () => new ArrayQuery(value),
object: () => new ObjectQuery(value)
object: () => new ObjectQuery(value),
nullable: {
string: () => new StringQuery(value, true),
number: () => new NumberQuery(value, true),
boolean: () => new BooleanQuery(value, true),
id: () => new IdQuery(value, true),
array: () => new ArrayQuery(value, true),
object: () => new ObjectQuery(value, true)
}
}
});