This commit is contained in:
syuilo
2017-03-03 07:27:47 +09:00
parent 6e181ee0f1
commit 2e4e599c01

View File

@ -53,6 +53,13 @@ class QueryCore implements Query {
this.error = null; this.error = null;
} }
/**
* このインスタンスの値が null、またはエラーが存在しているなどして、処理をスキップするべきか否か
*/
get shouldSkip() {
return this.error !== null || this.value === null;
}
/** /**
* このインスタンスの値が undefined または null の場合エラーにします * このインスタンスの値が undefined または null の場合エラーにします
*/ */
@ -86,7 +93,7 @@ class QueryCore implements Query {
* @param validator バリデータ * @param validator バリデータ
*/ */
validate(validator: Validator<any>) { validate(validator: Validator<any>) {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
const result = validator(this.value); const result = validator(this.value);
if (result === false) { if (result === false) {
this.error = new Error('invalid-format'); this.error = new Error('invalid-format');
@ -164,7 +171,7 @@ class NumberQuery extends QueryCore {
* @param max 上限 * @param max 上限
*/ */
range(min: number, max: number) { range(min: number, max: number) {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
if (this.value < min || this.value > max) { if (this.value < min || this.value > max) {
this.error = new Error('invalid-range'); this.error = new Error('invalid-range');
} }
@ -176,7 +183,7 @@ class NumberQuery extends QueryCore {
* @param value 下限 * @param value 下限
*/ */
min(value: number) { min(value: number) {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
if (this.value < value) { if (this.value < value) {
this.error = new Error('invalid-range'); this.error = new Error('invalid-range');
} }
@ -188,7 +195,7 @@ class NumberQuery extends QueryCore {
* @param value 上限 * @param value 上限
*/ */
max(value: number) { max(value: number) {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
if (this.value > value) { if (this.value > value) {
this.error = new Error('invalid-range'); this.error = new Error('invalid-range');
} }
@ -247,7 +254,7 @@ class StringQuery extends QueryCore {
* @param max 上限 * @param max 上限
*/ */
range(min: number, max: number) { range(min: number, max: number) {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
if (this.value.length < min || this.value.length > max) { if (this.value.length < min || this.value.length > max) {
this.error = new Error('invalid-range'); this.error = new Error('invalid-range');
} }
@ -255,7 +262,7 @@ class StringQuery extends QueryCore {
} }
trim() { trim() {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
this.value = this.value.trim(); this.value = this.value.trim();
return this; return this;
} }
@ -296,7 +303,7 @@ class StringQuery extends QueryCore {
* @param pattern 文字列の配列またはスペースで区切られた文字列 * @param pattern 文字列の配列またはスペースで区切られた文字列
*/ */
or(pattern: string | string[]) { or(pattern: string | string[]) {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
if (typeof pattern == 'string') pattern = pattern.split(' '); if (typeof pattern == 'string') pattern = pattern.split(' ');
const match = pattern.some(x => x === this.value); const match = pattern.some(x => x === this.value);
if (!match) this.error = new Error('not-match-pattern'); if (!match) this.error = new Error('not-match-pattern');
@ -309,7 +316,7 @@ class StringQuery extends QueryCore {
* @param pattern 正規表現 * @param pattern 正規表現
*/ */
match(pattern: RegExp) { match(pattern: RegExp) {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
if (!pattern.test(this.value)) this.error = new Error('not-match-pattern'); if (!pattern.test(this.value)) this.error = new Error('not-match-pattern');
return this; return this;
} }
@ -334,7 +341,7 @@ class ArrayQuery extends QueryCore {
* 配列の値がユニークでない場合(=重複した項目がある場合)エラーにします * 配列の値がユニークでない場合(=重複した項目がある場合)エラーにします
*/ */
unique() { unique() {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
if (hasDuplicates(this.value)) { if (hasDuplicates(this.value)) {
this.error = new Error('must-be-unique'); this.error = new Error('must-be-unique');
} }
@ -347,7 +354,7 @@ class ArrayQuery extends QueryCore {
* @param max 上限 * @param max 上限
*/ */
range(min: number, max: number) { range(min: number, max: number) {
if (this.error || this.value === null) return this; if (this.shouldSkip) return this;
if (this.value.length < min || this.value.length > max) { if (this.value.length < min || this.value.length > max) {
this.error = new Error('invalid-range'); this.error = new Error('invalid-range');
} }