/** * Copyright (c) 2023-2024 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Adam Midlik * @author David Sehnal */ import { Field, OptionalField, RequiredField, ValueFor } from './field-schema.js'; type Fields = { [key in string]: Field; }; /** Type of `ParamsSchema` where all fields are completely independent */ export interface SimpleParamsSchema { type: 'simple'; /** Parameter fields */ fields: TFields; } export declare function SimpleParamsSchema(fields: TFields): SimpleParamsSchema; type ValuesForFields = { [key in keyof F as (F[key] extends RequiredField ? key : never)]: ValueFor; } & { [key in keyof F as (F[key] extends OptionalField ? key : never)]?: ValueFor; }; type ValuesForSimpleParamsSchema = ValuesForFields; type AllRequiredFields = { [key in keyof F]: F[key] extends Field ? RequiredField : never; }; type AllRequiredSimple = SimpleParamsSchema>; type Cases = { [case_ in string]: SimpleParamsSchema; }; /** Type of `ParamsSchema` where one field (discriminator) determines what other fields are allowed (i.e. discriminated union type) */ export interface UnionParamsSchema { type: 'union'; /** Name of parameter field that determines the rest (allowed values are defined by keys of `cases`) */ discriminator: TDiscriminator; /** Description for the discriminator parameter field */ discriminatorDescription: string; /** `ParamsSchema` for the rest, for each case of discriminator value */ cases: TCases; } export declare function UnionParamsSchema(discriminator: TDiscriminator, discriminatorDescription: string, cases: TCases): UnionParamsSchema; type ValuesForUnionParamsSchema = TCase extends keyof TSchema['cases'] ? { [discriminator in TSchema['discriminator']]: TCase; } & ValuesFor : never; type AllRequiredUnion = UnionParamsSchema; }>; /** Schema for "params", i.e. a flat collection of key-value pairs */ export type ParamsSchema = SimpleParamsSchema | UnionParamsSchema; /** Type of values for a params schema (optional fields can be missing) */ export type ValuesFor

= P extends SimpleParamsSchema ? ValuesForSimpleParamsSchema

: P extends UnionParamsSchema ? ValuesForUnionParamsSchema

: never; /** Variation of a params schema where all fields are required */ export type AllRequired

= P extends SimpleParamsSchema ? AllRequiredSimple

: P extends UnionParamsSchema ? AllRequiredUnion

: never; declare function AllRequiredSimple(schema: TSchema): AllRequired; declare function AllRequiredUnion(schema: TSchema): AllRequired; export declare function AllRequired(schema: TSchema): AllRequired; /** Type of full values for a params schema, i.e. including all optional fields */ export type FullValuesFor

= ValuesFor>; interface ValidationOptions { /** Check that all parameters (including optional) have a value provided. */ requireAll?: boolean; /** Check there are extra parameters other that those defined in the schema. */ noExtra?: boolean; } /** Return `undefined` if `values` contains correct value types for `schema`, * return description of validation issues, if `values` have wrong type. * If `options.requireAll`, all parameters (including optional) must have a value provided. * If `options.noExtra` is true, presence of any extra parameters is treated as an issue. */ export declare function paramsValidationIssues

(schema: P, values: { [k: string]: any; }, options?: ValidationOptions): string[] | undefined; /** Add default parameter values to `values` based on a parameter schema (only for optional parameters) */ export declare function addParamDefaults

(schema: P, values: ValuesFor

): FullValuesFor

; export {};