diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-03-03 09:15:38 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-03-03 09:15:38 +0900 |
| commit | 665bcda0a7cbf37d5691d48838da85e2f308babc (patch) | |
| tree | 9e7aa9b8e9fa41de0a4506d8a33bc38f0294081b /src/api | |
| parent | wip (diff) | |
| download | sharkey-665bcda0a7cbf37d5691d48838da85e2f308babc.tar.gz sharkey-665bcda0a7cbf37d5691d48838da85e2f308babc.tar.bz2 sharkey-665bcda0a7cbf37d5691d48838da85e2f308babc.zip | |
wip
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/endpoints/i/update.ts | 4 | ||||
| -rw-r--r-- | src/api/it.ts | 78 |
2 files changed, 34 insertions, 48 deletions
diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts index 1e46315ceb..aeda0a4527 100644 --- a/src/api/endpoints/i/update.ts +++ b/src/api/endpoints/i/update.ts @@ -23,9 +23,9 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().validate(isValidName).qed(); + const [name, nameErr] = it(params.name).expect.string().notNull().validate(isValidName).qed(); if (nameErr) return rej('invalid name param'); - user.name = name; + if (name) user.name = name; // Get 'description' parameter const description = params.description; diff --git a/src/api/it.ts b/src/api/it.ts index b08612c709..14f6063b24 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -53,8 +53,16 @@ class QueryCore implements Query { this.error = null; } + get isUndefined() { + return this.value === undefined; + } + + get isNull() { + return this.value === null; + } + get isEmpty() { - return this.value === undefined || this.value === null; + return this.isUndefined || this.isNull; } /** @@ -65,7 +73,7 @@ class QueryCore implements Query { } /** - * このインスタンスの値が空の場合エラーにします + * このインスタンスの値が空のときにエラーにします */ required() { if (this.error === null && this.isEmpty) { @@ -75,10 +83,30 @@ class QueryCore implements Query { } /** - * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します + * このインスタンスの値が設定されていない(=undefined)場合エラーにします + */ + notUndefined() { + if (this.error === null && this.isUndefined) { + this.error = new Error('required'); + } + return this; + } + + /** + * このインスタンスの値が null のときにエラーにします + */ + notNull() { + if (this.error === null && this.isNull) { + this.error = new Error('required'); + } + return this; + } + + /** + * このインスタンスの値が設定されていない(=undefined)ときにデフォルトで設定する値を設定します */ default(value: any) { - if (this.isEmpty) { + if (this.isUndefined) { this.value = value; } return this; @@ -120,13 +148,6 @@ class BooleanQuery extends QueryCore { } /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ default(value: boolean) { @@ -199,13 +220,6 @@ class NumberQuery extends QueryCore { } /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ default(value: number) { @@ -260,13 +274,6 @@ class StringQuery extends QueryCore { } /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ default(value: string) { @@ -362,13 +369,6 @@ class ArrayQuery extends QueryCore { } /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ default(value: any[]) { @@ -404,13 +404,6 @@ class IdQuery extends QueryCore { } /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ default(value: mongo.ObjectID) { @@ -446,13 +439,6 @@ class ObjectQuery extends QueryCore { } /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ default(value: any) { |