convex
Version:
Client for the Convex Cloud
99 lines • 4.54 kB
TypeScript
import { Expand } from "../type_utils.js";
import { GenericId } from "./index.js";
import { OptionalProperty, VAny, VArray, VBoolean, VBytes, VFloat64, VId, VInt64, VLiteral, VNull, VObject, VOptional, VRecord, VString, VUnion, Validator } from "./validators.js";
/**
* The type that all validators must extend.
*
* @public
*/
export type GenericValidator = Validator<any, any, any>;
export declare function isValidator(v: any): v is GenericValidator;
/**
* Coerce an object with validators as properties to a validator.
* If a validator is passed, return it.
*
* @public
*/
export declare function asObjectValidator<V extends Validator<any, any, any> | PropertyValidators>(obj: V): V extends Validator<any, any, any> ? V : V extends PropertyValidators ? Validator<ObjectType<V>> : never;
/**
* Coerce an object with validators as properties to a validator.
* If a validator is passed, return it.
*
* @public
*/
export type AsObjectValidator<V extends Validator<any, any, any> | PropertyValidators> = V extends Validator<any, any, any> ? V : V extends PropertyValidators ? Validator<ObjectType<V>> : never;
/**
* The validator builder.
*
* This builder allows you to build validators for Convex values.
*
* Validators can be used in [schema definitions](https://docs.convex.dev/database/schemas)
* and as input validators for Convex functions.
*
* @public
*/
export declare const v: {
id<TableName extends string>(tableName: TableName): VId<GenericId<TableName>, "required">;
null(): VNull<null, "required">;
/**
* Alias for `v.float64()`
*/
number(): VFloat64<number, "required">;
float64(): VFloat64<number, "required">;
/**
* @deprecated Use `v.int64()` instead
*/
bigint(): VInt64<bigint, "required">;
int64(): VInt64<bigint, "required">;
boolean(): VBoolean<boolean, "required">;
string(): VString<string, "required">;
bytes(): VBytes<ArrayBuffer, "required">;
literal<T extends string | number | bigint | boolean>(literal: T): VLiteral<T, "required">;
array<T_1 extends Validator<any, "required", any>>(element: T_1): VArray<T_1["type"][], T_1, "required">;
object<T_2 extends PropertyValidators>(fields: T_2): VObject<Expand<{ [Property in OptionalKeys<T_2>]?: Exclude<Infer<T_2[Property]>, undefined> | undefined; } & { [Property_1 in Exclude<keyof T_2, OptionalKeys<T_2>>]: Infer<T_2[Property_1]>; }>, T_2, "required", { [Property_2 in keyof T_2]: Property_2 | `${Property_2 & string}.${T_2[Property_2]["fieldPaths"]}`; }[keyof T_2] & string>;
/* @internal
record<Key extends Validator<any, "required", any>, Value extends Validator<any, "required", any>>(keys: Key, values: Value): VRecord<Value["isOptional"] extends true ? { [key in Infer<Key>]?: Value["type"] | undefined; } : Record<Infer<Key>, Value["type"]>, Key, Value, "required", string>; */
union<T_3 extends Validator<any, "required", any>[]>(...members: T_3): VUnion<T_3[number]["type"], T_3, "required", T_3[number]["fieldPaths"]>;
any(): VAny<any, "required", string>;
optional<T_4 extends GenericValidator>(value: T_4): VOptional<T_4>;
};
/**
* Validators for each property of an object.
*
* This is represented as an object mapping the property name to its
* {@link Validator}.
*
* @public
*/
export type PropertyValidators = Record<string, Validator<any, OptionalProperty, any>>;
/**
* Compute the type of an object from {@link PropertyValidators}.
*
* @public
*/
export type ObjectType<Fields extends PropertyValidators> = Expand<{
[Property in OptionalKeys<Fields>]?: Exclude<Infer<Fields[Property]>, undefined>;
} & {
[Property in RequiredKeys<Fields>]: Infer<Fields[Property]>;
}>;
type OptionalKeys<PropertyValidators extends Record<string, GenericValidator>> = {
[Property in keyof PropertyValidators]: PropertyValidators[Property]["isOptional"] extends "optional" ? Property : never;
}[keyof PropertyValidators];
type RequiredKeys<PropertyValidators extends Record<string, GenericValidator>> = Exclude<keyof PropertyValidators, OptionalKeys<PropertyValidators>>;
/**
* Extract a TypeScript type from a validator.
*
* Example usage:
* ```ts
* const objectSchema = v.object({
* property: v.string(),
* });
* type MyObject = Infer<typeof objectSchema>; // { property: string }
* ```
* @typeParam V - The type of a {@link Validator} constructed with {@link v}.
*
* @public
*/
export type Infer<T extends Validator<any, OptionalProperty, any>> = T["type"];
export {};
//# sourceMappingURL=validator.d.ts.map