跳至主要內容

優化原始校驗器

建置 Refine 校驗器 的起點是原始組合子。

這些是初始建構方塊,可以使用集合或其他自訂組合子組合成更高階的組合子。

bool()

驗證值為 布林

// define checker
const check = bool();

// test a value
const result = check(false);
assert(result.type === 'success');

// result should typecheck
const value: boolean = result.value;

// test an invalid value
const failedResult = check(1);
assert(failedResult.type === 'failure');

number()

驗證值為 數字

// define checker
const check = number();

// test a value
const result = check(1);
assert(result.type === 'success');

// result should typecheck
const value: number = result.value;

// test an invalid value
const failedResult = check(false);
assert(failedResult.type === 'failure');

string()

驗證值為 字串

// define checker
const check = string();

// test a value
const result = check('test');
assert(result.type === 'success');

// result should typecheck
const value: string = result.value;

// test an invalid value
const failedResult = check(false);
assert(failedResult.type === 'failure');

字串也可以接受正規表示式引數進行驗證。

// define checker
const check = string(/^users?$/);

// test a value
const result = check('user');
assert(result.type === 'success');

// result should typecheck
const value: string = result.value;

// test an invalid value
const failedResult = check('buser');
assert(failedResult.type === 'failure');

literal()

驗證值為指定的文字型別

// define checker
// note: to get Flow to use the literal, we must annotate
const check = literal<'add_todo'>('add_todo');

// can also use for null/undefined/true/false literals
const checkExactlyNull = literal<null>(null);

// test a value
const result = check('add_todo');
assert(result.type === 'success');

// result should typecheck
const value: 'add_todo' = result.value;

// test an invalid value
const failedResult = check('remove_todo');
assert(failedResult.type === 'failure');

stringLiterals()

校驗器斷言混合值是否與字串文字的聯集相符。在物件中提供合法值作為金鑰/值,且可透過提供物件中的不同值進行翻譯。

const suitChecker = stringLiterals({
heart: 'heart',
spade: 'spade',
club: 'club',
diamond: 'diamond',
});

const suit: 'heart' | 'spade' | 'club' | 'diamond' = assertion(suitChecker())(x);

date()

驗證值為 JavaScript 日期物件

// define checker
const check = date();

// test a value
const result = check(new Date());
assert(result.type === 'success');

// result should typecheck
const value: Date = result.value;

// test an invalid value
const failedResult = check(1);
assert(failedResult.type === 'failure');

jsonDate()

與日期類似,儘管也可以將 ISO 日期字串隱式強制轉換為日期物件。這在對/自 JSON 序列化時特別有用。

// define checker
const check = jsonDate();

// test a value
const result = check((new Date()).toString());
assert(result.type === 'success');

// result should typecheck
const value: Date = result.value;

// test an invalid value
const failedResult = check(1);
assert(failedResult.type === 'failure');

mixed()

填補值/預設校驗器,允許略過對某些值的檢查。總是成功。

// define checker
const check = mixed();

// test a value
assert(check(new Date()).type === 'success');
assert(check(1).type === 'success');
assert(check('test').type === 'success');

如果您想要略過檢查部分未知值,這可能會有所幫助...

// if we don't want to check below a certain level of an object...
const Request = object({
code: number(),
url: string(),
params: mixed(), // don't care what this is
});

nullable()

可建立給定檢查器的可為空版本

// define checker
const check = nullable(string());

// result type of checking a value is a nullable string
const result: ?string = check(null);

// test a value
assert(check('test').type === 'success');
assert(check(null).type === 'success');
assert(check(1).type === 'failure');

預設情況下,傳遞至 nullable 的值必須與檢查器規範完全相符(當值不為空時),否則將會失敗。

傳遞 nullWithWarningWhenInvalid 選項可允許處理不太重要的無效值。如果提供的檢查器標記某結果為無效,新的檢查器將回傳 null。

例如

const Options = object({
// this must be a non-null string,
// or Options is not valid
filename: string(),
// if this field is not a string,
// it will be null and Options will pass the checker
description: nullable(string(), {
nullWithWarningWhenInvalid: true,
})
})

const result = Options({filename: 'test', description: 1});

assert(result.type === 'success');
assert(result.value.description === null);

// there will be a warning
assert(result.warnings.length === 1);

voidable()

類似於 nullable,可建立給定檢查器版本的 T | void

// define checker
const check = voidable(string());

// test a value
assert(check('test').type === 'success');
assert(check(null).type === 'failure');
assert(check(undefined).type === 'success');
assert(check(1).type === 'failure');

預設情況下,傳遞至 nullable 的值必須與檢查器規範完全相符(當值未定義時),否則將會失敗。

傳遞 undefinedWithWarningWhenInvalid 選項可允許處理不太重要的無效值。如果提供的檢查器標記某結果為無效,新的檢查器將回傳 undefined。

例如

const Options = object({
// this must be a non-null string,
// or Options is not valid
filename: string(),
// if this field is not a string,
// it will be undefined and Options will pass the checker
description: voidable(string(), {
undefinedWithWarningWhenInvalid: true,
})
})

const result = Options({filename: 'test', description: 1});

assert(result.type === 'success');
assert(result.value.description === undefined);

// there will be a warning
assert(result.warnings.length === 1);