リファインチェッカー
RefineのコアはChecker<T>
型です。チェッカーは基本的に mixed
(Flowの場合) または unknown
(TypeScriptの場合) の値を取り込み、CheckResult<T>
を返す関数です...
/**
* a function which checks if a given mixed value matches a type V,
* returning the value if it does, otherwise a failure message.
*/
type Checker<+V> = (
value: mixed,
// optional path within a parent object tree to the current value
path?: $ReadOnlyArray<string>,
) => CheckResult<V>;
/**
* the result of checking whether a type matches an expected value
*/
type CheckResult<+V> = CheckSuccess<V> | CheckFailure;
/**
* the result of failing to match a value to its expected type
*/
type CheckFailure = $ReadOnly<{
type: 'failure',
message: string,
path: $ReadOnlyArray<string>,
}>;
/**
* the result of successfully matching a value to its expected type
*/
type CheckSuccess<+V> = $ReadOnly<{
type: 'success',
value: V,
// if using `nullable()` with the `nullWithWarningWhenInvalid` option,
// failures that would have failed the check are appended as warnings
// here on the success result.
warnings: $ReadOnlyArray<CheckFailure>
}>;
以下で詳しく説明する組み込みチェッカーは、簡単な構成を可能にします。これにより、基本的なプリミティブからより複雑なチェッカーを構築できます
// type PersonType = $ReadOnly<{name: string, friends: ?Array<PersonType>}>
// const Person: Checker<PersonType>
const Person = object({
name: string(),
friends: nullable(array(lazy(() => Person)))
});
Refineは、組み込みのチェッカーを多数提供しています。詳細については、個々のドキュメントページを参照してください
さらに、Refineはjsonの解析やアサーション関数などの一般的なユースケース用のユーティリティ関数をいくつか提供しています。