import { HKT2, Kind, Kind2, URIS, URIS2 } from 'fp-ts/HKT'; import type { CamelCase } from 'type-fest'; /** * @since 1.3.0 * @category Model */ export declare type OptionalKeyFlag = 'Optional'; /** * @since 1.3.0 * @category Model */ export declare type RequiredKeyFlag = 'Required'; /** * @since 1.3.0 * @category Model */ export declare type KeyNotMapped = typeof KeyNotMapped; declare const KeyNotMapped: unique symbol; /** * @since 1.3.0 * @category Model */ export declare type KeyFlag = OptionalKeyFlag | RequiredKeyFlag; /** * Meta information for an HKT2 for if the key is optional or required, and if the key is remapped * * @since 1.3.0 * @category Model */ export interface Prop, K extends string | KeyNotMapped> { readonly _flag: Flag; readonly _keyRemap: K; readonly _val: Val; } /** * Meta information for a Kind for if the key is optional or required, and if the key is remapped * * @since 1.3.0 * @category Model */ export interface Prop1, K extends string | KeyNotMapped> { readonly _flag: Flag; readonly _keyRemap: K; readonly _val: Val; } /** * Meta information for a Kind2 for if the key is optional or required, and if the key is remapped * * @since 1.3.0 * @category Model */ export interface Prop2, K extends string | KeyNotMapped> { readonly _flag: Flag; readonly _keyRemap: K; readonly _val: Val; } interface Required { /** * Used to indicate that a property is required * * @since 1.3.0 */ >(val: Val): Prop2; /** * Used to indicate that a property is required * * @since 1.3.0 */ >(val: Val): Prop1; /** * Used to indicate that a property is required * * @since 1.3.0 */ >(val: Val): Prop; } /** * Indicates that a property is required * * @since 1.3.0 * @category Constructors */ export declare const required: Required; /** * @since 1.3.0 * @category Guards */ export declare const isRequiredFlag: (flag: KeyFlag) => flag is "Required"; interface Optional { /** * Used to indicate that a property is optional * * @since 1.3.0 */ >(val: Val): Prop2; /** * Used to indicate that a property is optional * * @since 1.3.0 */ >(val: Val): Prop1; /** * Used to indicate that a property is optional * * @since 1.3.0 */ >(val: Val): Prop; } /** * Indicates that a property is optional * * @since 1.3.0 * @category Constructors */ export declare const optional: Optional; interface MapKeyTo { /** * Used to remap a property's key to a new key in the output type * * @since 1.3.0 */ (mapTo: K): >(prop: Prop2) => Prop2; /** * Used to remap a property's key to a new key in the output type * * @since 1.3.0 */ (mapTo: K): >(prop: Prop1) => Prop1; /** * Used to remap a property's key to a new key in the output type * * @since 1.3.0 */ (mapTo: K): >(prop: Prop) => Prop; } /** * @since 1.3.0 * @category Guards */ export declare const isOptionalFlag: (flag: KeyFlag) => flag is "Optional"; /** * Used to remap a property's key to a new key in the output type * * @since 1.3.0 * @category Combinators * @example * import * as fc from 'fast-check' * import * as S from 'schemata-ts/schemata' * import * as s from 'schemata-ts/struct' * import { getArbitrary } from 'schemata-ts/Arbitrary' * import { getGuard } from 'schemata-ts/Guard' * * const databasePerson = s.defineStruct({ * first_name: s.mapKeyTo('firstName')(s.required(S.String)), * last_name: s.mapKeyTo('lastName')(s.required(S.String)), * age: s.required(S.Number), * is_married: s.mapKeyTo('isMarried')(s.required(S.BooleanFromString)), * }) * * const DatabasePerson = S.StructM(databasePerson) * * // DatabasePerson will have the type: * // SchemaExt< * // { first_name: string, last_name: string, age: number, is_married: string }, * // { firstName: string, lastName: string, age: number, isMarried: boolean } * // > * * const arbitrary = getArbitrary(DatabasePerson).arbitrary(fc) * const guard = getGuard(DatabasePerson) * * fc.assert(fc.property(arbitrary, guard.is)) */ export declare const mapKeyTo: MapKeyTo; /** * @since 1.3.0 * @category Guards */ export declare const keyIsNotMapped: (key: string | KeyNotMapped) => key is typeof KeyNotMapped; /** * A type-level key remap provider * * @since 1.4.0 * @category Models */ export interface KeyRemapLambda { readonly input: string; readonly output: string; } /** * Applies a remap type-level function to a remap lambda * * @since 1.4.0 * @category Models */ export declare type ApplyKeyRemap = R extends { readonly input: string; } ? (R & { readonly input: Val; })['output'] : { readonly R: R; readonly input: (val: Val) => Val; }; interface MapKeysWith { /** * Used to remap a struct's keys using a provided type-level function and equivalent string mapper * * @since 1.4.0 */ (mapping: (s: S) => ApplyKeyRemap): , string | KeyNotMapped>>>(props: Props) => { [K in keyof Props]: Props[K] extends Prop2 ? Val extends Kind2 ? Remap extends KeyNotMapped ? Prop2> : Prop2> : never : never; }; /** * Used to remap a struct's keys using a provided type-level function and equivalent string mapper * * @since 1.4.0 */ (mapping: (s: S) => ApplyKeyRemap): , string | KeyNotMapped>>>(props: Props) => { [K in keyof Props]: Props[K] extends Prop1 ? Val extends Kind ? Remap extends KeyNotMapped ? Prop1> : Prop1> : never : never; }; /** * Used to remap a struct's keys using a provided type-level function and equivalent string mapper * * @since 1.4.0 */ (mapping: (s: S) => ApplyKeyRemap): , string | KeyNotMapped>>>(props: Props) => { [K in keyof Props]: Props[K] extends Prop ? Val extends HKT2 ? Remap extends KeyNotMapped ? Prop> : Prop> : never : never; }; } /** * Remap a struct's keys using provided RemapLambda, and string-mapping function * * @since 1.4.0 * @category Combinators * @example * import * as E from 'fp-ts/Either' * import { pipe } from 'fp-ts/function' * import { getDecoder } from 'schemata-ts/Decoder' * import * as S from 'schemata-ts/schemata' * import * as s from 'schemata-ts/struct' * * interface CapitalizeLambda extends s.KeyRemapLambda { * readonly output: Capitalize * } * * const capitalize: (s: string) => string = s => * `${s.substring(0, 1).toUpperCase()}${s.substring(1)}` * * const MappedStructDecoder = pipe( * s.defineStruct({ * foo: s.required(S.String), * bar: s.optional(S.Number), * qux: s.optional(S.Boolean), * }), * s.mapKeysWith(capitalize), * S.StructM, * getDecoder, * ) * * assert.deepStrictEqual( * MappedStructDecoder.decode({ foo: 'foo', bar: 1 }), * E.right({ Foo: 'foo', Bar: 1 }), * ) */ export declare const mapKeysWith: MapKeysWith; /** * A KeyRemapLambda for remapping keys to camelCase * * @since 1.4.0 * @category Combinators */ export interface CamelCaseLambda extends KeyRemapLambda { readonly output: CamelCase; } /** * Remap a struct's non-camel-cased-keys into camelCase * * @since 1.4.0 * @category Combinators * @example * import * as S from 'schemata-ts/schemata' * import * as s from 'schemata-ts/struct' * import { getEncoder } from 'schemata-ts/Encoder' * * const databasePerson = s.struct({ * first_name: S.String, * last_name: S.String, * age: S.Number, * is_married: S.BooleanFromString, * }) * * const DatabasePerson = S.StructM(s.camelCaseKeys(databasePerson)) * * // DatabasePerson will have the type: * // SchemaExt< * // { first_name: string, last_name: string, age: number, is_married: string }, * // { firstName: string, lastName: string, age: number, isMarried: boolean } * // > * * const encoder = getEncoder(DatabasePerson) * * assert.deepStrictEqual( * encoder.encode({ * firstName: 'John', * lastName: 'Doe', * age: 42, * isMarried: false, * }), * { * first_name: 'John', * last_name: 'Doe', * age: 42, * is_married: 'false', * }, * ) */ export declare const camelCaseKeys: , Props extends Record, string | typeof KeyNotMapped>>>(props: Props) => { [K in keyof Props]: Props[K] extends Prop2 ? Val extends Kind2 ? Remap extends typeof KeyNotMapped ? Prop2> : Prop2> : never : never; }; interface StructDefinition { /** * A convenience function to declare reusable struct definitions with type-safety * without first plugging into StructM * * @since 1.3.0 * @category Models */ , string | KeyNotMapped>>>(props: Props): Props; /** * A convenience function to declare reusable struct definitions with type-safety * without first plugging into StructM * * @since 1.3.0 * @category Models */ , string | KeyNotMapped>>>(props: Props): Props; /** * A convenience function to declare reusable struct definitions with type-safety * without first plugging into StructM * * @since 1.3.0 * @category Models */ , string | KeyNotMapped>>>(props: Props): Props; } /** * @since 1.3.0 * @category Constructors * @example * import * as fc from 'fast-check' * import * as S from 'schemata-ts/schemata' * import * as s from 'schemata-ts/struct' * import { getGuard } from 'schemata-ts/Guard' * import { getArbitrary } from 'schemata-ts/Arbitrary' * * const someDomainType = s.defineStruct({ * a: s.required(S.String), * b: s.required(S.BooleanFromNumber), * }) * * const SomeDomainTypeSchema = S.StructM(someDomainType) * * // SomeDomainType will have the type: * // SchemaExt<{ a: string, b: number }, { a: string, b: boolean }> * * const arbitrary = getArbitrary(SomeDomainTypeSchema).arbitrary(fc) * const guard = getGuard(SomeDomainTypeSchema) * * fc.assert(fc.property(arbitrary, guard.is)) */ export declare const defineStruct: StructDefinition; interface Struct { /** * A convenience function to declare a struct where all keys are required * * @since 1.4.0 * @category Models */ >>(props: Props): { [K in keyof Props]: Prop2; }; /** * A convenience function to declare a struct where all keys are required * * @since 1.4.0 * @category Models */ >>(props: Props): { [K in keyof Props]: Prop1; }; /** * A convenience function to declare a struct where all keys are required * * @since 1.4.0 * @category Models */ >>(props: Props): { [K in keyof Props]: Prop; }; } /** * Defines a StructM declaration where all keys are required * * @since 1.4.0 * @category Constructors * @example * import * as S from 'schemata-ts/schemata' * import * as s from 'schemata-ts/struct' * import { getEncoder } from 'schemata-ts/Encoder' * * const someDomainType = s.struct({ * a: S.String, * b: S.BooleanFromNumber, * }) * * const SomeDomainTypeSchema = S.StructM(someDomainType) * * // SomeDomainTypeSchema will have the type: * // SchemaExt<{ a: string, b: number }, { a: string, b: boolean }> * * const encoder = getEncoder(SomeDomainTypeSchema) * * assert.deepStrictEqual( * encoder.encode({ * a: 'foo', * b: false, * }), * { * a: 'foo', * b: 0, * }, * ) */ export declare const struct: Struct; interface Partial { /** * Remap a StructM definition such that all keys are optional * * @since 1.3.0 * @category Models */ , string | KeyNotMapped>>>(props: Props): { [K in keyof Props]: Props[K] extends Prop2 ? Prop2 : never; }; /** * Remap a StructM definition such that all keys are optional * * @since 1.3.0 * @category Models */ , string | KeyNotMapped>>>(props: Props): { [K in keyof Props]: Props[K] extends Prop1 ? Prop1 : never; }; /** * Remap a StructM definition such that all keys are optional * * @since 1.3.0 * @category Models */ , string | KeyNotMapped>>>(props: Props): { [K in keyof Props]: Props[K] extends Prop ? Prop : never; }; } /** * Marks all properties as optional * * @since 1.3.0 * @category Utilities */ export declare const partial: Partial; interface Complete { /** * Remap a StructM definition such that all keys are required * * @since 1.3.0 * @category Models */ , string | KeyNotMapped>>>(props: Props): { [K in keyof Props]: Props[K] extends Prop2 ? Prop2 : never; }; /** * Remap a StructM definition such that all keys are required * * @since 1.3.0 * @category Models */ , string | KeyNotMapped>>>(props: Props): { [K in keyof Props]: Props[K] extends Prop1 ? Prop1 : never; }; /** * Remap a StructM definition such that all keys are required * * @since 1.3.0 * @category Models */ , string | KeyNotMapped>>>(props: Props): { [K in keyof Props]: Props[K] extends Prop ? Prop : never; }; } /** * Marks all properties as required. * * @since 1.3.0 * @category Utilities */ export declare const complete: Complete; /** * Include only a specified set of keys of an object. Built for StructM but works with any struct * * @since 1.3.0 * @category Utilities */ export declare const pick: , Keys extends [keyof A, ...Exclude[]]>(...keys: Keys) => (obj: A) => { [K in Keys[number]]: A[K]; }; /** * Exclude a set of keys from an object. Built for StructM but works with any struct * * @since 1.3.0 * @category Utilities */ export declare const omit: , Keys extends [keyof A, ...ReadonlyArray>]>(...omittedKeys: Keys) => (obj: A) => { [K in Exclude]: A[K]; }; export {}; //# sourceMappingURL=struct.d.ts.map