This commit is contained in:
syuilo
2017-03-03 02:42:17 +09:00
parent c94de44f27
commit d5e5419dfc
2 changed files with 90 additions and 20 deletions

View File

@@ -1,3 +1,8 @@
/**
* it
* 楽しいバリデーション
*/
import * as mongo from 'mongodb';
import hasDuplicates from '../common/has-duplicates';
@@ -11,6 +16,8 @@ interface Factory {
required: () => Factory;
default: (value: any) => Factory;
validate: (validator: Validator<any>) => Factory;
}
@@ -33,6 +40,16 @@ class FactoryCore implements Factory {
return this;
}
/**
* このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します
*/
default(value: any) {
if (this.value === null) {
this.value = value;
}
return this;
}
/**
* このインスタンスの値およびエラーを取得します
*/
@@ -79,6 +96,13 @@ class BooleanFactory extends FactoryCore {
return super.required();
}
/**
* このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します
*/
default(value: boolean) {
return super.default(value);
}
/**
* このインスタンスの値およびエラーを取得します
*/
@@ -124,6 +148,30 @@ class NumberFactory extends FactoryCore {
return this;
}
/**
* このインスタンスの値が指定された下限より下回っている場合エラーにします
* @param value 下限
*/
min(value: number) {
if (this.error || this.value === null) return this;
if (this.value < value) {
this.error = new Error('invalid-range');
}
return this;
}
/**
* このインスタンスの値が指定された上限より上回っている場合エラーにします
* @param value 上限
*/
max(value: number) {
if (this.error || this.value === null) return this;
if (this.value > value) {
this.error = new Error('invalid-range');
}
return this;
}
/**
* このインスタンスの値が undefined または null の場合エラーにします
*/
@@ -131,6 +179,13 @@ class NumberFactory extends FactoryCore {
return super.required();
}
/**
* このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します
*/
default(value: number) {
return super.default(value);
}
/**
* このインスタンスの値およびエラーを取得します
*/
@@ -189,6 +244,13 @@ class StringFactory extends FactoryCore {
return super.required();
}
/**
* このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します
*/
default(value: string) {
return super.default(value);
}
/**
* このインスタンスの値およびエラーを取得します
*/
@@ -252,6 +314,13 @@ class ArrayFactory extends FactoryCore {
return super.required();
}
/**
* このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します
*/
default(value: any[]) {
return super.default(value);
}
/**
* このインスタンスの値およびエラーを取得します
*/
@@ -291,6 +360,13 @@ class IdFactory extends FactoryCore {
return super.required();
}
/**
* このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します
*/
default(value: mongo.ObjectID) {
return super.default(value);
}
/**
* このインスタンスの値およびエラーを取得します
*/
@@ -330,6 +406,13 @@ class ObjectFactory extends FactoryCore {
return super.required();
}
/**
* このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します
*/
default(value: any) {
return super.default(value);
}
/**
* このインスタンスの値およびエラーを取得します
*/