summaryrefslogtreecommitdiff
path: root/src/api/it.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-03-03 08:56:07 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-03-03 08:56:07 +0900
commitdc45055f2ffcea2369f12d104999746220b22c90 (patch)
treed3ec8f4a03076fe804c906cd60656e197f3010f2 /src/api/it.ts
parentwip (diff)
downloadsharkey-dc45055f2ffcea2369f12d104999746220b22c90.tar.gz
sharkey-dc45055f2ffcea2369f12d104999746220b22c90.tar.bz2
sharkey-dc45055f2ffcea2369f12d104999746220b22c90.zip
wip
Diffstat (limited to 'src/api/it.ts')
-rw-r--r--src/api/it.ts78
1 files changed, 35 insertions, 43 deletions
diff --git a/src/api/it.ts b/src/api/it.ts
index 039f879957..b08612c709 100644
--- a/src/api/it.ts
+++ b/src/api/it.ts
@@ -48,23 +48,27 @@ class QueryCore implements Query {
value: any;
error: Error;
- constructor() {
- this.value = null;
+ constructor(value: any) {
+ this.value = value;
this.error = null;
}
+ get isEmpty() {
+ return this.value === undefined || this.value === null;
+ }
+
/**
- * このインスタンスの値が null、またはエラーが存在しているなどして、処理をスキップするべきか否か
+ * このインスタンスの値が空、またはエラーが存在しているなどして、処理をスキップするべきか否か
*/
get shouldSkip() {
- return this.error !== null || this.value === null;
+ return this.error !== null || this.isEmpty;
}
/**
- * このインスタンスの値が undefined または null の場合エラーにします
+ * このインスタンスの値が空の場合エラーにします
*/
required() {
- if (this.error === null && this.value === null) {
+ if (this.error === null && this.isEmpty) {
this.error = new Error('required');
}
return this;
@@ -74,7 +78,7 @@ class QueryCore implements Query {
* このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します
*/
default(value: any) {
- if (this.value === null) {
+ if (this.isEmpty) {
this.value = value;
}
return this;
@@ -109,13 +113,9 @@ class BooleanQuery extends QueryCore {
error: Error;
constructor(value) {
- super();
- if (value === undefined || value === null) {
- this.value = null;
- } else if (typeof value != 'boolean') {
+ super(value);
+ if (!this.isEmpty && typeof value != 'boolean') {
this.error = new Error('must-be-a-boolean');
- } else {
- this.value = value;
}
}
@@ -155,13 +155,9 @@ class NumberQuery extends QueryCore {
error: Error;
constructor(value) {
- super();
- if (value === undefined || value === null) {
- this.value = null;
- } else if (!Number.isFinite(value)) {
+ super(value);
+ if (!this.isEmpty && !Number.isFinite(value)) {
this.error = new Error('must-be-a-number');
- } else {
- this.value = value;
}
}
@@ -238,13 +234,9 @@ class StringQuery extends QueryCore {
error: Error;
constructor(value) {
- super();
- if (value === undefined || value === null) {
- this.value = null;
- } else if (typeof value != 'string') {
+ super(value);
+ if (!this.isEmpty && typeof value != 'string') {
this.error = new Error('must-be-a-string');
- } else {
- this.value = value;
}
}
@@ -327,13 +319,9 @@ class ArrayQuery extends QueryCore {
error: Error;
constructor(value) {
- super();
- if (value === undefined || value === null) {
- this.value = null;
- } else if (!Array.isArray(value)) {
+ super(value);
+ if (!this.isEmpty && !Array.isArray(value)) {
this.error = new Error('must-be-an-array');
- } else {
- this.value = value;
}
}
@@ -362,6 +350,18 @@ class ArrayQuery extends QueryCore {
}
/**
+ * このインスタンスの配列内の要素すべてが文字列であるか検証します
+ * ひとつでも文字列以外の要素が存在する場合エラーにします
+ */
+ allString() {
+ if (this.shouldSkip) return this;
+ if (this.value.some(x => typeof x != 'string')) {
+ this.error = new Error('dirty-array');
+ }
+ return this;
+ }
+
+ /**
* このインスタンスの値が undefined または null の場合エラーにします
*/
required() {
@@ -397,13 +397,9 @@ class IdQuery extends QueryCore {
error: Error;
constructor(value) {
- super();
- if (value === undefined || value === null) {
- this.value = null;
- } else if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) {
+ super(value);
+ if (!this.isEmpty && (typeof value != 'string' || !mongo.ObjectID.isValid(value))) {
this.error = new Error('must-be-an-id');
- } else {
- this.value = new mongo.ObjectID(value);
}
}
@@ -443,13 +439,9 @@ class ObjectQuery extends QueryCore {
error: Error;
constructor(value) {
- super();
- if (value === undefined || value === null) {
- this.value = null;
- } else if (typeof value != 'object') {
+ super(value);
+ if (!this.isEmpty && typeof value != 'object') {
this.error = new Error('must-be-an-object');
- } else {
- this.value = value;
}
}