From 6e8ee09a775c0f810866ff223bd66fb16e6faca3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 10:24:56 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=84=E3=81=84=E6=84=9F=E3=81=98=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/endpoints/i/update.ts | 8 ++-- src/api/it.ts | 71 ++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts index e3b307b0b..a5f153861 100644 --- a/src/api/endpoints/i/update.ts +++ b/src/api/endpoints/i/update.ts @@ -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; diff --git a/src/api/it.ts b/src/api/it.ts index f540172ad..845a56365 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -74,9 +74,14 @@ class QueryCore implements Query { value: any; error: Error; - constructor(value: any) { - this.value = value; - this.error = null; + 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() { @@ -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) + } } });