Function: map()
Call Signature
export declare function map<T, U, S extends CombinatorSchema<T> = CombinatorSchema<T>>(schema: S & CombinatorSchema<T>, transform: (value: T) => U): Modified<S, Combinator<U>>Transform the output of a combinator schema.
Creates a new schema that applies transform to the result of schema.parse. The original schema is not modified. Other modifiers on schema (for example multiple) are kept, and transform is applied to each value of a multiple schema.
A default set on schema is kept, but it does not go through transform: when it is used, the value is the default as is, although it is typed as U. Set the default after map(), with a transformed value.
A union of schemas of different types, such as strict ? integer() : string(), matches the last overload, whose transform takes a value of any of their types. An instantiation expression with two type arguments, such as typeof map<number, string>, is a type error, since that overload takes two type arguments too, the first of which must be a schema: give S as well, as in typeof map<number, string, CombinatorSchema<number>>.
Type Parameters
| Name | Description |
|---|---|
T | The input schema's parsed type. |
U | The transformed type. |
S extends CombinatorSchema<T> = CombinatorSchema<T> | The input combinator schema, inferred from schema. Its other modifiers are kept. If type arguments are given explicitly without S, S is CombinatorSchema<T>, and the type of the result lacks the other modifiers, although the returned object has them. |
Parameters
| Name | Type | Description |
|---|---|---|
schema | S & CombinatorSchema<T> | The base combinator schema. |
transform | (value: T) => U | The transformation function. |
Returns
Modified<S, Combinator<U>> — A new combinator schema that resolves to the transformed type.
Examples
const args = {
doubled: map(integer(), n => n * 2)
}Call Signature
export declare function map<T, U, S extends CombinatorSchema<T> = CombinatorSchema<T>>(schema: S & Combinator<T>, transform: (value: T) => U): Modified<S, Combinator<U>>Transform the output of a combinator schema, as the first overload does, for a schema that fits the first overload but that TypeScript does not match with it at first, such as positional(integer()), a class instance or a schema typed by an interface. This overload keeps such a schema from matching the overload for a union of schemas of different types.
Creates a new schema that applies transform to the result of schema.parse. The original schema is not modified. Other modifiers on schema (for example multiple) are kept, and transform is applied to each value of a multiple schema.
A default set on schema is kept, but it does not go through transform: when it is used, the value is the default as is, although it is typed as U. Set the default after map(), with a transformed value.
Type Parameters
| Name | Description |
|---|---|
T | The input schema's parsed type. |
U | The transformed type. |
S extends CombinatorSchema<T> = CombinatorSchema<T> | The input combinator schema, inferred from schema. Its other modifiers are kept. |
Parameters
| Name | Type | Description |
|---|---|---|
schema | S & Combinator<T> | The base combinator schema. |
transform | (value: T) => U | The transformation function. |
Returns
Modified<S, Combinator<U>> — A new combinator schema that resolves to the transformed type.
Examples
const args = {
doubled: map(positional(integer()), n => n * 2)
}Call Signature
export declare function map<S extends CombinatorSchema<unknown>, U>(schema: S, transform: (value: ParsedType<S>) => U): Modified<S, Combinator<U>>Transform the output of a union of combinator schemas of different types, such as strict ? integer() : string().
Creates a new schema that applies transform to the result of schema.parse, which is a value of any of their types: a number or a string for strict ? integer() : string(). The original schema is not modified. Other modifiers on schema (for example multiple) are kept, and transform is applied to each value of a multiple schema.
A default set on schema is kept, but it does not go through transform: when it is used, the value is the default as is, although it is typed as U. Set the default after map(), with a transformed value.
Type Parameters
| Name | Description |
|---|---|
S extends CombinatorSchema<unknown> | The input combinator schema, inferred from schema: a union of schemas of different types. Its other modifiers are kept. |
U | The transformed type. |
Parameters
| Name | Type | Description |
|---|---|---|
schema | S | The base combinator schema. |
transform | (value: ParsedType<S>) => U | The transformation function, which takes a value of any of the types that the schemas of the union parse to. |
Returns
Modified<S, Combinator<U>> — A new combinator schema that resolves to the transformed type.
Examples
const strict = process.argv.includes('--strict')
const args = {
// `value` is a number or a string
timeout: map(strict ? integer({ min: 0 }) : string(), value => String(value))
}