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');
} }