import { TypeGuardWithContext, Validator } from '../validation'; import { ConverterFunc } from './baseConverter'; import { Converter, OnError } from './converter'; import { FieldConverters, ObjectConverter, ObjectConverterOptions } from './objectConverter'; import { StringConverter } from './stringConverter'; /** * Action to take on conversion failures (deprecated - use Conversion.OnError) * @public * @deprecated use Conversion.OnError. */ export { OnError }; /** * A converter to convert unknown to string. Values of type * string succeed. Anything else fails. * @public */ export declare const string: StringConverter; /** * Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of supplied * enumerated values. Anything else fails. * * @remarks * Allowed enumerated values can also be supplied as context at conversion time. * @param values - Array of allowed values. * @returns A new {@link Converter | Converter} returning ``. * @public */ export declare function enumeratedValue(values: ReadonlyArray): Converter>; /** * Helper function to create a {@link Converter | Converter} which converts `unknown` to one of a set of supplied enumerated * values, mapping any of multiple supplied values to the enumeration. * @remarks * Enables mapping of multiple input values to a consistent internal representation (so e.g. `'y'`, `'yes'`, * `'true'`, `1` and `true` can all map to boolean `true`) * @param map - An array of tuples describing the mapping. The first element of each tuple is the result * value, the second is the set of values that map to the result. Tuples are evaluated in the order * supplied and are not checked for duplicates. * @param message - An optional error message. * @returns A {@link Converter | Converter} which applies the mapping and yields `` on success. * @public */ export declare function mappedEnumeratedValue(map: ReadonlyArray<[T, ReadonlyArray]>, message?: string): Converter>; /** * Helper function to create a {@link Converter | Converter} which converts `unknown` to some supplied literal value. Succeeds with * the supplied value if an identity comparison succeeds, fails otherwise. * @param value - The value to be compared. * @returns A {@link Converter | Converter} which returns the supplied value on success. * @public */ export declare function literal(value: T): Converter; /** * Deprecated alias for @see literal * @param value - The value to be compared. * @deprecated Use {@link Converters.literal} instead. * @internal */ export declare const value: typeof literal; /** * A {@link Converter | Converter} which converts `unknown` to a `number`. * @remarks * Numbers and strings with a numeric format succeed. Anything else fails. * @public */ export declare const number: Converter; /** * A {@link Converter | Converter} which converts `unknown` to `boolean`. * @remarks * Boolean values or the case-insensitive strings `'true'` and `'false'` succeed. * Anything else fails. * @public */ export declare const boolean: Converter; /** * A {@link Converter | Converter} which converts an optional `string` value. Values of type * `string` are returned. Anything else returns {@link Success | Success} with value `undefined`. * @public */ export declare const optionalString: Converter; /** * Helper function to create a {@link Converter | Converter} which converts any `string` into an * array of `string`, by separating at a supplied delimiter. * @remarks * Delimiter may also be supplied as context at conversion time. * @param delimiter - The delimiter at which to split. * @returns A new {@link Converter | Converter} returning `string[]`. * @public */ export declare function delimitedString(delimiter: string, options?: 'filtered' | 'all'): Converter; /** * Helper function to create a {@link Converter | Converter} from any {@link Validation.Validator} * @param validator - the validator to be wrapped * @returns A {@link Converter | Converter} which uses the supplied validator. * @public */ export declare function validated(validator: Validator): Converter; /** * Helper function to create a {@link Converter | Converter} from a supplied {@link Conversion.ConverterFunc | ConverterFunc}. * @param convert - the function to be wrapped * @returns A {@link Converter | Converter} which uses the supplied function. * @public */ export declare function generic(convert: ConverterFunc): Converter; /** * Helper function to create a {@link Converter | Converter} from a supplied type guard function. * @param description - a description of the thing to be validated for use in error messages * @param guard - a {@link Validation.TypeGuardWithContext} which performs the validation. * @returns A new {@link Converter | Converter} which validates the values using the supplied type guard * and returns them in place. * @public */ export declare function isA(description: string, guard: TypeGuardWithContext): Converter; /** * A {@link Converter | Converter} which converts an optional `number` value. * @remarks * Values of type `number` or numeric strings are converted and returned. * Anything else returns {@link Success | Success} with value `undefined`. * @public */ export declare const optionalNumber: Converter; /** * A {@link Converter | Converter} to convert an optional `boolean` value. * @remarks * Values of type `boolean` or strings that match (case-insensitive) `'true'` * or `'false'` are converted and returned. Anything else returns {@link Success | Success} * with value `undefined`. * @public */ export declare const optionalBoolean: Converter; /** * A helper function to create a {@link Converter | Converter} for polymorphic values. * Returns a converter which invokes the wrapped converters in sequence, returning the * first successful result. Returns an error if none of the supplied converters can * convert the value. * @remarks * If `onError` is `ignoreErrors` (default), then errors from any of the * converters are ignored provided that some converter succeeds. If * onError is `failOnError`, then an error from any converter fails the entire * conversion. * * @param converters - An ordered list of {@link Converter | converters} or {@link Validator | validators} * to be considered. * @param onError - Specifies treatment of unconvertible elements. * @returns A new {@link Converter | Converter} which yields a value from the union of the types returned * by the wrapped converters. * @public */ export declare function oneOf(converters: Array | Validator>, onError?: OnError): Converter; /** * A helper function to create a {@link Converter | Converter} which converts `unknown` to an array of ``. * @remarks * If `onError` is `'failOnError'` (default), then the entire conversion fails if any element cannot * be converted. If `onError` is `'ignoreErrors'`, then failing elements are silently ignored. * @param converter - {@link Converter | Converter} or {@link Validator | Validator} used to convert each * item in the array. * @param ignoreErrors - Specifies treatment of unconvertible elements. * @returns A {@link Converter | Converter} which returns an array of ``. * @public */ export declare function arrayOf(converter: Converter | Validator, onError?: OnError): Converter; /** * {@link Converter | Converter} to convert an `unknown` to an array of `string`. * @remarks * Returns {@link Success | Success} with the the supplied value if it as an array * of strings, returns {@link Failure | Failure} with an error message otherwise. * @public */ export declare const stringArray: Converter; /** * {@link Converter | Converter} to convert an `unknown` to an array of `number`. * @remarks * Returns {@link Success | Success} with the the supplied value if it as an array * of numbers, returns {@link Failure | Failure} with an error message otherwise. * @public */ export declare const numberArray: Converter; /** * Options for {@link Converters.(recordOf:3) | Converters.recordOf} and * {@link Converters.(mapOf:3) | Converters.mapOf} * helper functions. * @public */ export interface KeyedConverterOptions { /** * if `onError` is `'fail'` (default), then the entire conversion fails if any key or element * cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored. */ onError?: 'fail' | 'ignore'; /** * If present, `keyConverter` is used to convert the source object property names to * keys in the resulting map or record. * @remarks * Can be used to coerce key names to supported values and/or strong types. */ keyConverter?: Converter | Validator; } /** * A helper function to create a {@link Converter | Converter} which converts the `string`-keyed * properties using a supplied {@link Converter | Converter} or {@link Validator | Validator} to * produce a `Record`. * @remarks * The resulting converter fails conversion if any element cannot be converted. * @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each * item in the source object. * @returns A {@link Converter | Converter} which returns `Record`. * {@label WITH_DEFAULT} * @public */ export declare function recordOf(converter: Converter | Validator): Converter, TC>; /** * A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties * using a supplied {@link Converter | Converter} or {@link Validator | Validator} to produce a * `Record` and optionally specified handling of elements that cannot be converted. * @remarks * if `onError` is `'fail'` (default), then the entire conversion fails if any key or element * cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored. * @param converter - {@link Converter | Converter} or {@link Validator | Validator} for each item in * the source object. * @returns A {@link Converter | Converter} which returns `Record`. * {@label WITH_ON_ERROR} * @public */ export declare function recordOf(converter: Converter | Validator, onError: 'fail' | 'ignore'): Converter, TC>; /** * A helper function to create a {@link Converter | Converter} or which converts the `string`-keyed properties * using a supplied {@link Converter | Converter} or {@link Validator | Validator} to produce a * `Record`. * @remarks * If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed * converter for keys and/or control the handling of elements that fail conversion. * @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each item in the source object. * @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions} which * supplies a key converter and/or error-handling options. * @returns A {@link Converter | Converter} which returns `Record`. * {@label WITH_OPTIONS} * @public */ export declare function recordOf(converter: Converter | Validator, options: KeyedConverterOptions): Converter, TC>; /** * A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties * using a supplied {@link Converter | Converter} or {@link Validator | Validator} to produce a * `Map`. * @remarks * The resulting converter fails conversion if any element cannot be converted. * @param converter - {@link Converter | Converter} | {@link Validator | Validator} used for each item in * the source object. * @returns A {@link Converter | Converter} which returns `Map`. * {@label WITH_DEFAULT} * @public */ export declare function mapOf(converter: Converter | Validator): Converter, TC>; /** * A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties * using a supplied {@link Converter | Converter} or {@link Validator | Validator} to produce a * `Map` and specified handling of elements that cannot be converted. * @remarks * if `onError` is `'fail'` (default), then the entire conversion fails if any key or element * cannot be converted. If `onError` is `'ignore'`, failing elements are silently ignored. * @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for * each item in the source object. * @returns A {@link Converter | Converter} which returns `Map`. * {@label WITH_ON_ERROR} * @public */ export declare function mapOf(converter: Converter | Validator, onError: 'fail' | 'ignore'): Converter, TC>; /** * A helper function to create a {@link Converter | Converter} which converts the `string`-keyed properties * using a supplied {@link Converter | Converter} or {@link Validator | Validator} to produce * a `Map`. * @remarks * If present, the supplied {@link Converters.KeyedConverterOptions | options} can provide a strongly-typed * converter for keys and/or control the handling of elements that fail conversion. * @param converter - {@link Converter | Converter} or {@link Validator | Validator} used for each item * in the source object. * @param options - Optional {@link Converters.KeyedConverterOptions | KeyedConverterOptions} which * supplies a key converter and/or error-handling options. * @returns A {@link Converter | Converter} which returns `Map`. * {@label WITH_OPTIONS} * @public */ export declare function mapOf(converter: Converter | Validator, options: KeyedConverterOptions): Converter, TC>; /** * Helper function to create a {@link Converter | Converter} which validates that a supplied value is * of a type validated by a supplied validator function and returns it. * @remarks * If `validator` succeeds, this {@link Converter | Converter} returns {@link Success | Success} with the supplied * value of `from` coerced to type ``. Returns a {@link Failure | Failure} with additional * information otherwise. * @param validator - A validator function to determine if the converted value is valid. * @param description - A description of the validated type for use in error messages. * @returns A new {@link Converter | Converter} which applies the supplied validation. * @public */ export declare function validateWith(validator: (from: unknown) => from is T, description?: string): Converter; /** * A helper function to create a {@link Converter | Converter} which extracts and converts an element from an array. * @remarks * The returned {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists * in the supplied array and can be converted. Returns {@link Failure | Failure} with an error message otherwise. * @param index - The index of the element to be extracted. * @param converter - A {@link Converter | Converter} or {@link Validator | Validator} for the extracted element. * @returns A {@link Converter | Converter} which extracts the specified element from an array. * @public */ export declare function element(index: number, converter: Converter | Validator): Converter; /** * A helper function to create a {@link Converter | Converter} which extracts and converts an optional element from an array. * @remarks * The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value if the element exists * in the supplied array and can be converted. Returns {@link Success | Success} with value `undefined` if the parameter * is an array but the index is out of range. Returns {@link Failure | Failure} with a message if the supplied parameter * is not an array, if the requested index is negative, or if the element cannot be converted. * @param index - The index of the element to be extracted. * @param converter - A {@link Converter | Converter} or {@link Validator | Validator} used for the extracted element. * @returns A {@link Converter | Converter} which extracts the specified element from an array. * @public */ export declare function optionalElement(index: number, converter: Converter | Validator): Converter; /** * A helper function to create a {@link Converter | Converter} which extracts and convert a property specified * by name from an object. * @remarks * The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of the corresponding * object property if the field exists and can be converted. Returns {@link Failure | Failure} with an error message * otherwise. * @param name - The name of the field to be extracted. * @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted * field. * @public */ export declare function field(name: string, converter: Converter | Validator): Converter; /** * A helper function to create a {@link Converter | Converter} which extracts and convert a property specified * by name from an object. * @remarks * The resulting {@link Converter | Converter} returns {@link Success | Success} with the converted value of * the corresponding object property if the field exists and can be converted. Returns {@link Success | Success} * with `undefined` if the supplied parameter is an object but the named field is not present. * Returns {@link Failure | Failure} with an error message otherwise. * @param name - The name of the field to be extracted. * @param converter - {@link Converter | Converter} or {@link Validator | Validator} to use for the extracted field. * @public */ export declare function optionalField(name: string, converter: Converter | Validator): Converter; /** * Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object * without changing shape, given a {@link Conversion.FieldConverters | FieldConverters} and an optional * {@link Conversion.ObjectConverterOptions | ObjectConverterOptions} to further refine conversion behavior. * @remarks * By default, if all of the requested fields exist and can be converted, returns {@link Success | Success} * with a new object that contains the converted values under the original key names. If any required properties * do not exist or cannot be converted, the entire conversion fails, returning {@link Failure | Failure} with additional * error information. * * Fields that succeed but convert to undefined are omitted from the result object but do not * fail the conversion. * @param properties - An {@link Conversion.FieldConverters | FieldConverters} defining the shape of the * source object and {@link Converter | converters} to be applied to each properties. * @param options - An {@link Conversion.ObjectConverterOptions | ObjectConverterOptions} containing options * for the object converter. * @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions. * {@label WITH_OPTIONS} * @public */ export declare function object(properties: FieldConverters, options?: ObjectConverterOptions): ObjectConverter; /** * Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object * without changing shape, given a {@link Conversion.FieldConverters | FieldConverters} and a set of * optional properties. * @remarks * By default, if all of the requested fields exist and can be converted, returns {@link Success | Success} * with a new object that contains the converted values under the original key names. If any required properties * do not exist or cannot be converted, the entire conversion fails, returning {@link Failure | Failure} with additional * error information. * * Fields that succeed but convert to undefined are omitted from the result object but do not * fail the conversion. * @param properties - An {@link Conversion.FieldConverters | FieldConverters} defining the shape of the * source object and {@link Converter | converters} to be applied to each properties. * @param optional - An array of `(keyof T)` listing the keys to be considered optional. * {@label WITH_KEYS} * @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions. * @public * @deprecated Use {@link Converters.(object:1) | Converters.object(fields, options)} instead. */ export declare function object(properties: FieldConverters, optional: (keyof T)[]): ObjectConverter; /** * Options for the {@link Converters.(strictObject:1)} helper function. * @public */ export type StrictObjectConverterOptions = Omit, 'strict'>; /** * Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object * without changing shape, a {@link Conversion.FieldConverters | FieldConverters} and an optional * {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions} to further refine * conversion behavior. * * @remarks * Fields that succeed but convert to undefined are omitted from the result object but do not * fail the conversion. * * The conversion fails if any unexpected fields are encountered. * * @param properties - An object containing defining the shape and converters to be applied. * @param options - An optional @see StrictObjectConverterOptions containing options for the object converter. * @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions. * {@label WITH_OPTIONS} * @public */ export declare function strictObject(properties: FieldConverters, options?: StrictObjectConverterOptions): ObjectConverter; /** * Helper function to create a {@link Conversion.ObjectConverter | ObjectConverter} which converts an object * without changing shape, a {@link Conversion.FieldConverters | FieldConverters} and an optional * {@link Converters.StrictObjectConverterOptions | StrictObjectConverterOptions} to further refine * conversion behavior. * * @remarks * Fields that succeed but convert to undefined are omitted from the result object but do not * fail the conversion. * * The conversion fails if any unexpected fields are encountered. * * @param properties - An object containing defining the shape and converters to be applied. * @param optional - An array of `keyof T` containing keys to be considered optional. * @returns A new {@link Conversion.ObjectConverter | ObjectConverter} which applies the specified conversions. * {@label WITH_KEYS} * @deprecated Use {@link Converters.(strictObject:1) | Converters.strictObject(options)} instead. * @public */ export declare function strictObject(properties: FieldConverters, optional: (keyof T)[]): ObjectConverter; /** * A string-keyed `Record` which maps specific {@link Converter | converters} or * {@link Validator | Validators} to the value of a discriminator property. * @public */ export type DiscriminatedObjectConverters = Record | Validator>; /** * Helper to create a {@link Converter | Converter} which converts a discriminated object without changing shape. * @remarks * Takes the name of the discriminator property and a * {@link Converters.DiscriminatedObjectConverters | string-keyed Record of converters and validators}. During conversion, * the resulting {@link Converter | Converter} invokes the converter from `converters` that corresponds to the value of * the discriminator property in the source object. * * If the source is not an object, the discriminator property is missing, or the discriminator has * a value not present in the converters, conversion fails and returns {@link Failure | Failure} with more information. * @param discriminatorProp - Name of the property used to discriminate types. * @param converters - {@link Converters.DiscriminatedObjectConverters | String-keyed record of converters and validators} * to invoke, where each key corresponds to a value of the discriminator property. * @returns A {@link Converter | Converter} which converts the corresponding discriminated object. * @public */ export declare function discriminatedObject(discriminatorProp: string, converters: DiscriminatedObjectConverters): Converter; /** * Helper to create a {@link Converter | Converter} which converts a source object to a new object with a * different shape. * * @remarks * On successful conversion, the resulting {@link Converter | Converter} returns {@link Success | Success} with a new * object, which contains the converted values under the key names specified at initialization time. * It returns {@link Failure | Failure} with an error message if any fields to be extracted do not exist * or cannot be converted. * * Fields that succeed but convert to undefined are omitted from the result object but do not * fail the conversion. * * @param properties - An object with key names that correspond to the target object and an * appropriate {@link Conversion.FieldConverters | FieldConverter} which extracts and converts * a single filed from the source object. * @returns A {@link Converter | Converter} with the specified conversion behavior. * @public */ export declare function transform(properties: FieldConverters): Converter; /** * Per-property converters and configuration for each field in the destination object of * a {@link Converters.transformObject} call. * @public */ export type FieldTransformers = { [key in keyof TDEST]: { /** * The name of the property in the source object to be converted. */ from: keyof TSRC; /** * The converter or validator used to convert the property. */ converter: Converter | Validator; /** * If `true` then a missing source property is ignored. If `false` or omitted * then a missing source property causes an error. */ optional?: boolean; }; }; /** * Options for a {@link Converters.transformObject} call. * @public */ export interface TransformObjectOptions { /** * If `strict` is `true` then unused properties in the source object cause * an error, otherwise they are ignored. */ strict: true; /** * An optional list of source properties to be ignored when strict mode * is enabled. */ ignore?: (keyof TSRC)[]; /** * An optional description of this transform to be used for error messages. */ description?: string; } /** * Helper to create a strongly-typed {@link Converter | Converter} which converts a source object to a * new object with a different shape. * * @remarks * On successful conversion, the resulting {@link Converter | Converter} returns {@link Success | Success} with a new * object, which contains the converted values under the key names specified at initialization time. * * It returns {@link Failure | Failure} with an error message if any fields to be extracted do not exist * or cannot be converted. * * @param destinationFields - An object with key names that correspond to the target object and an * appropriate {@link Converters.FieldTransformers | FieldTransformers} which specifies the name * of the corresponding property in the source object, the converter or validator used for each source * property and any other configuration to guide the conversion. * @param options - Options which affect the transformation. * * @returns A {@link Converter | Converter} with the specified conversion behavior. * @public */ export declare function transformObject(destinationFields: FieldTransformers, options?: TransformObjectOptions): Converter; //# sourceMappingURL=converters.d.ts.map