UNPKG

convex-helpers

Version:

A collection of useful code to complement the official convex package.

70 lines (69 loc) 4.46 kB
import { PropertyValidators, Validator } from "convex/values"; import { Expand } from "."; /** * Helper for defining a union of literals more concisely. * * e.g. `literals("a", 1, false)` is equivalent to * `v.union(v.literal("a"), v.literal(1), v.literal(false))` * * @param args Values you want to use in a union of literals. * @returns A validator for the union of the literals. */ export declare const literals: <V extends string | number | bigint | boolean, T extends [V, V, ...V[]]>(...args: T) => Validator<T[number], false, never>; /** * nullable define a validator that can be the value or null more consisely. * * @param x The validator to make nullable. As in, it can be the value or null. * @returns A new validator that can be the value or null. */ export declare const nullable: <V extends Validator<any, false, any>>(x: V) => Validator<(V | Validator<null, false, never>)["type"], false, (V | Validator<null, false, never>)["fieldPaths"]>; /** * partial helps you define an object of optional validators more concisely. * * e.g. `partial({a: v.string(), b: v.number()})` is equivalent to * `{a: v.optional(v.string()), b: v.optional(v.number())}` * * @param obj The object of validators to make optional. e.g. {a: v.string()} * @returns A new object of validators that can be the value or undefined. */ export declare const partial: <T extends PropertyValidators>(obj: T) => { [K in keyof T]: T[K] extends Validator<infer V, false, infer F extends string> ? Validator<V | undefined, true, F> : never; }; /** Any string value. */ export declare const string: Validator<string, false, never>; /** JavaScript number, represented as a float64 in the database. */ export declare const number: Validator<number, false, never>; /** JavaScript number, represented as a float64 in the database. */ export declare const float64: Validator<number, false, never>; /** boolean value. For typing it only as true, use `l(true)` */ export declare const boolean: Validator<boolean, false, never>; /** bigint, though stored as an int64 in the database. */ export declare const bigint: Validator<bigint, false, never>; /** bigint, though stored as an int64 in the database. */ export declare const int64: Validator<bigint, false, never>; /** Any Convex value */ export declare const any: Validator<any, false, string>; /** Null value. Underscore is so it doesn't shadow the null builtin */ export declare const null_: Validator<null, false, never>; /** Re-export values from v without having to do v.* */ export declare const id: <TableName extends string>(tableName: TableName) => Validator<import("convex/values").GenericId<TableName>, false, never>, object: <T_2 extends PropertyValidators>(schema: T_2) => import("convex/dist/cjs-types/values/validator").ObjectValidator<T_2>, array: <T_1>(values: Validator<T_1, false, any>) => Validator<T_1[], false, never>, bytes: () => Validator<ArrayBuffer, false, never>, literal: <T extends string | number | bigint | boolean>(literal: T) => Validator<T, false, never>, optional: <T_4 extends Validator<any, false, any>>(inner: T_4) => Validator<T_4["type"] | undefined, true, T_4["fieldPaths"]>, union: <T_3 extends [Validator<any, false, any>, Validator<any, false, any>, ...Validator<any, false, any>[]]>(...schemaTypes: T_3) => Validator<T_3[number]["type"], false, T_3[number]["fieldPaths"]>; /** ArrayBuffer validator. */ export declare const arrayBuffer: () => Validator<ArrayBuffer, false, never>; export declare const systemFields: <TableName extends string>(tableName: TableName) => { _id: Validator<import("convex/values").GenericId<TableName>, false, never>; _creationTime: Validator<number, false, never>; }; export declare const withSystemFields: <TableName extends string, T extends Record<string, Validator<any, any, any>>>(tableName: TableName, fields: T) => Expand<T & { _id: Validator<import("convex/values").GenericId<TableName>, false, never>; _creationTime: Validator<number, false, never>; }>; /** * A string validator that is a branded string type. * * Read more at https://stack.convex.dev/using-branded-types-in-validators * * @param _brand - A unique string literal to brand the string with */ export declare const brandedString: <T extends string>(_brand: T) => Validator<string & { _: T; }, false, never>; /** Mark fields as deprecated with this permissive validator typed as null */ export declare const deprecated: Validator<null, true, never>;