メインコンテンツにスキップ

Refine プリミティブチェッカー

Refine チェッカー を構築する際の出発点は、プリミティブなコンビネータです。

これらは、コレクションや他のカスタムコンビネータを使用して、高階コンビネータに構成できる初期の構成要素です。

bool()

値が boolean であることを検証します

// 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()

値が 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()

値が 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');

string は、検証のために正規表現引数を受け取ることもできます。

// 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の Date オブジェクトであることを検証します

// 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()

dateに似ていますが、ISO日付文字列を暗黙的にDateオブジェクトに強制変換します。これは、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()

指定されたチェッカーのnull許容バージョンを作成します

// 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に渡される値は、nullでない場合、チェッカースペックと完全に一致する必要があります。そうでない場合は失敗します。

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