title | description | source | contributors | |
---|---|---|---|---|
instance |
Creates an instance schema. |
/schemas/instance/instance.ts |
|
import { ApiList, Property } from '~/components'; import { properties } from './properties';
Creates an instance schema.
// Instance schema with an optional pipe
const Schema = instance<TClass>(class_, pipe);
// Instance schema with an optional message and pipe
const Schema = instance<TClass>(class_, message, pipe);
TClass
<Property {...properties.TClass} />
class_
{/* prettier-ignore */}<Property {...properties.class_} />message
<Property {...properties.message} />pipe
<Property {...properties.pipe} />
With instance
you can validate the data type of the input and with pipe
you can transform and validate the further details of the instance. If the input is not an instance of the specified class_
, you can use message
to customize the error message.
Schema
<Property {...properties.Schema} />
The following examples show how instance
can be used.
Schema to validate an Error
instance.
const ErrorSchema = instance(Error, 'Error instance required.');
Schema to validate an File
instance.
const FileSchema = instance(File, [
mimeType(['image/jpeg', 'image/png']),
maxSize(1024 * 1024 * 10),
]);
The following APIs can be combined with instance
.
<ApiList items={[ 'array', 'intersect', 'lazy', 'map', 'nonNullable', 'nonNullish', 'nonOptional', 'nullable', 'nullish', 'object', 'optional', 'record', 'set', 'tuple', 'union', ]} />
<ApiList items={[ 'brand', 'coerce', 'fallback', 'getDefault', 'getDefaults', 'getFallback', 'getFallbacks', 'is', 'parse', 'safeParse', 'transform', ]} />
<ApiList items={['toCustom', 'toMaxValue', 'toMinValue']} />
<ApiList items={[ 'custom', 'maxSize', 'maxValue', 'mimeType', 'minSize', 'minValue', 'notSize', 'notValue', 'size', 'value', ]} />