いい感じに

This commit is contained in:
syuilo 2017-03-03 10:24:56 +09:00
parent e340824d42
commit 6e8ee09a77
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) => new Promise(async (res, rej) =>
{ {
// Get 'name' parameter // 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 (nameErr) return rej('invalid name param');
if (name) user.name = name; if (name) user.name = name;
// Get 'description' parameter // 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 (descriptionErr) return rej('invalid description param');
if (description !== undefined) user.description = description; if (description !== undefined) user.description = description;
// Get 'location' parameter // 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 (locationErr) return rej('invalid location param');
if (location !== undefined) user.location = location; if (location !== undefined) user.location = location;
// Get 'birthday' parameter // 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 (birthdayErr) return rej('invalid birthday param');
if (birthday !== undefined) user.birthday = birthday; if (birthday !== undefined) user.birthday = birthday;

View File

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