This commit is contained in:
syuilo 2017-03-03 07:27:47 +09:00
parent 6e181ee0f1
commit 2e4e599c01
1 changed files with 22 additions and 15 deletions

View File

@ -5,22 +5,22 @@
/**
* Usage Examples
*
*
* const [val, err] = it(x).must.be.a.string().or('asc desc').default('desc').qed();
* xは文字列でなければならず'asc''desc''desc'
*
*
* const [val, err] = it(x).must.be.a.number().required().range(0, 100).qed();
* xは数値でなければならず0~100
*
*
* const [val, err] = it(x).must.be.an.array().unique().required().validate(x => x[0] != 'strawberry pasta').qed();
* xは配列でなければならず'strawberry pasta'
*
*
* ~~
* const [val, err] = it(x).must.be.a.string().required().qed();
*
* const [val, err] = it(x, 'string', true);
*
*
*
* ~BDD風記法~
* must.be.a(n)  expect :
* const [val, err] = it(x).expect.string().required().qed();
@ -53,6 +53,13 @@ class QueryCore implements Query {
this.error = null;
}
/**
* null
*/
get shouldSkip() {
return this.error !== null || this.value === null;
}
/**
* undefined  null
*/
@ -86,7 +93,7 @@ class QueryCore implements Query {
* @param validator
*/
validate(validator: Validator<any>) {
if (this.error || this.value === null) return this;
if (this.shouldSkip) return this;
const result = validator(this.value);
if (result === false) {
this.error = new Error('invalid-format');
@ -164,7 +171,7 @@ class NumberQuery extends QueryCore {
* @param max
*/
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) {
this.error = new Error('invalid-range');
}
@ -176,7 +183,7 @@ class NumberQuery extends QueryCore {
* @param value
*/
min(value: number) {
if (this.error || this.value === null) return this;
if (this.shouldSkip) return this;
if (this.value < value) {
this.error = new Error('invalid-range');
}
@ -188,7 +195,7 @@ class NumberQuery extends QueryCore {
* @param value
*/
max(value: number) {
if (this.error || this.value === null) return this;
if (this.shouldSkip) return this;
if (this.value > value) {
this.error = new Error('invalid-range');
}
@ -247,7 +254,7 @@ class StringQuery extends QueryCore {
* @param max
*/
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) {
this.error = new Error('invalid-range');
}
@ -255,7 +262,7 @@ class StringQuery extends QueryCore {
}
trim() {
if (this.error || this.value === null) return this;
if (this.shouldSkip) return this;
this.value = this.value.trim();
return this;
}
@ -296,7 +303,7 @@ class StringQuery extends QueryCore {
* @param pattern
*/
or(pattern: string | string[]) {
if (this.error || this.value === null) return this;
if (this.shouldSkip) return this;
if (typeof pattern == 'string') pattern = pattern.split(' ');
const match = pattern.some(x => x === this.value);
if (!match) this.error = new Error('not-match-pattern');
@ -309,7 +316,7 @@ class StringQuery extends QueryCore {
* @param pattern
*/
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');
return this;
}
@ -334,7 +341,7 @@ class ArrayQuery extends QueryCore {
* (=)
*/
unique() {
if (this.error || this.value === null) return this;
if (this.shouldSkip) return this;
if (hasDuplicates(this.value)) {
this.error = new Error('must-be-unique');
}
@ -347,7 +354,7 @@ class ArrayQuery extends QueryCore {
* @param max
*/
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) {
this.error = new Error('invalid-range');
}