Skip to content

Latest commit

History

History
128 lines (97 loc) 路 2.21 KB

File metadata and controls

128 lines (97 loc) 路 2.21 KB
title description source contributors
instance
Creates an instance schema.
/schemas/instance/instance.ts
fabian-hiller

import { ApiList, Property } from '~/components'; import { properties } from './properties';

instance

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

Generics

  • TClass <Property {...properties.TClass} />

Parameters

  • class_ {/* prettier-ignore */}<Property {...properties.class_} />
  • message <Property {...properties.message} />
  • pipe <Property {...properties.pipe} />

Explanation

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.

Returns

  • Schema <Property {...properties.Schema} />

Examples

The following examples show how instance can be used.

Error schema

Schema to validate an Error instance.

const ErrorSchema = instance(Error, 'Error instance required.');

File schema

Schema to validate an File instance.

const FileSchema = instance(File, [
  mimeType(['image/jpeg', 'image/png']),
  maxSize(1024 * 1024 * 10),
]);

Related

The following APIs can be combined with instance.

Schemas

<ApiList items={[ 'array', 'intersect', 'lazy', 'map', 'nonNullable', 'nonNullish', 'nonOptional', 'nullable', 'nullish', 'object', 'optional', 'record', 'set', 'tuple', 'union', ]} />

Methods

<ApiList items={[ 'brand', 'coerce', 'fallback', 'getDefault', 'getDefaults', 'getFallback', 'getFallbacks', 'is', 'parse', 'safeParse', 'transform', ]} />

Transformations

<ApiList items={['toCustom', 'toMaxValue', 'toMinValue']} />

Validations

<ApiList items={[ 'custom', 'maxSize', 'maxValue', 'mimeType', 'minSize', 'minValue', 'notSize', 'notValue', 'size', 'value', ]} />