import type { DataModelFromSchemaDefinition, Expand, GenericDatabaseReader, GenericDataModel, SchemaDefinition, TableNamesInDataModel } from "convex/server"; import type { GenericId, GenericValidator, Infer, ObjectType, OptionalProperty, PropertyValidators, Validator, VAny, VArray, VBoolean, VBytes, VFloat64, VId, VInt64, VLiteral, VNull, VObject, VOptional, VRecord, VString, VUnion } from "convex/values"; import { v } from "convex/values"; /** * 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))` * To use with an array: * ```ts * const myLiterals = ["a", 1, false] as const; * const literalValidator = literals(...myLiterals) * ``` * A similar result can be achieved with `v.union(...myLiterals.map(v.literal))` * however the type of each union member will be the union of literal types, * rather than each member being a specific literal type. * * @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: >(...args: T) => VUnion; }>>; /** * 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: >(x: V) => VUnion<(VNull | V)["type"], [VNull, V], "required", (VNull | V)["fieldPaths"]>; /** * partial helps you define an object of optional validators more concisely. * * `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 function partial(obj: T): { [K in keyof T]: VOptional; }; /** * partial helps you define an object of optional validators more concisely. * * `partial(v.object({a: v.string(), b: v.number()}))` is equivalent to * `v.object({a: v.optional(v.string()), b: v.optional(v.number())})` * * @param obj The object of validators to make optional. e.g. v.object({a: v.string()}) * @returns A new object of validators that can be the value or undefined. */ export declare function partial, O extends OptionalProperty>(obj: VObject): PartialVObject; /** * partial helps you define a union of optional validators more concisely. * * `partial(v.union(v.object({a: v.string()}), v.object({b: v.number()})))` * is equivalent to * `v.union(v.object({a: v.optional(v.string())}), v.object({b: v.optional(v.number())}))` * * @param obj The union of validators to make optional. e.g. v.union(v.object({a: v.string()}), v.object({b: v.number()})) * @returns A new union of validators that can be the value or undefined. */ export declare function partial[], O extends OptionalProperty>(obj: VUnion): PartialVUnion; /** * partial helps you define a union of optional validators more concisely. * * `partial(v.union(v.object({a: v.string()}), v.object({b: v.number()})))` * is equivalent to * `v.union(v.object({a: v.optional(v.string())}), v.object({b: v.optional(v.number())}))` * * @param obj The union of validators to make optional. e.g. v.union(v.object({a: v.string()}), v.object({b: v.number()})) * @returns A new union of validators that can be the value or undefined. */ export declare function partial, Union extends VUnion>(obj: Object | Union): PartialVUnion | PartialVObject; type PartialVObject, Optional extends OptionalProperty> = VObject, { [K in keyof V]: VOptional; }, Optional>; type PartialUnionMembers[]> = { [K in keyof Members]: Members[K] extends VObject ? Members[K] extends VObject ? PartialVObject : Members[K] : Members[K] extends VUnion ? Members[K] extends VUnion ? PartialVUnion : Members[K] : Members[K]; }; type PartialVUnion[], Optional extends OptionalProperty> = VUnion, PartialUnionMembers, Optional>; /** @deprecated Use `v.string()` instead. Any string value. */ export declare const string: VString; /** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */ export declare const number: VFloat64; /** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */ export declare const float64: VFloat64; /** @deprecated Use `v.boolean()` instead. boolean value. For typing it only as true, use `l(true)` */ export declare const boolean: VBoolean; /** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */ export declare const biging: VInt64; /** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */ export declare const int64: VInt64; /** @deprecated Use `v.any()` instead. Any Convex value */ export declare const any: VAny; /** @deprecated Use `v.null()` instead. Null value. Underscore is so it doesn't shadow the null builtin */ export declare const null_: VNull; /** @deprecated Use `v.*()` instead. */ export declare const id: (tableName: TableName) => VId, "required">, object: (fields: T_2) => VObject, undefined>; } & { [Property_1 in Exclude]: Infer; }>, T_2, "required", { [Property_2 in keyof T_2]: Property_2 | `${Property_2 & string}.${T_2[Property_2]["fieldPaths"]}`; }[keyof T_2] & string>, array: >(element: T_1) => VArray, bytes: () => VBytes, literal: (literal: T) => VLiteral, optional: (value: T_4) => VOptional, union: []>(...members: T_3) => VUnion; /** @deprecated Use `v.bytes()` instead. ArrayBuffer validator. */ export declare const arrayBuffer: VBytes; /** * Utility to get the validators for fields associated with a table. * e.g. for systemFields("users") it would return: * { _id: v.id("users"), _creationTime: v.number() } * * @param tableName The table name in the schema. * @returns Validators for the system fields: _id and _creationTime */ export declare const systemFields: (tableName: TableName) => { _id: VId, "required">; _creationTime: VFloat64; }; export type SystemFields = ReturnType>; /** * Utility to add system fields to an object with fields mapping to validators. * e.g. withSystemFields("users", { name: v.string() }) would return: * { name: v.string(), _id: v.id("users"), _creationTime: v.number() } * * @param tableName Table name in the schema. * @param fields The fields of the table mapped to their validators. * @returns The fields plus system fields _id and _creationTime. */ export declare const withSystemFields: >(tableName: TableName, fields: T) => Expand, "required">; _creationTime: VFloat64; }>; export type AddFieldsToValidator, Fields extends PropertyValidators> = V extends VObject ? VObject>, Expand, O> : Validator>, V["isOptional"], V["fieldPaths"] & { [Property in keyof Fields & string]: `${Property}.${Fields[Property]["fieldPaths"]}` | Property; }[keyof Fields & string] & string>; /** * Equivalent to `v.object({ ...validator, ...fields })`. */ export declare function addFieldsToValidator(validator: Props, fields: Fields): VObject, Props & Fields, "required">; /** * Add fields to an object validator. * Equivalent to `v.object({ ...validator.fields, ...fields })`. */ export declare function addFieldsToValidator, Fields extends PropertyValidators>(validator: V, fields: Fields): VObject, V["fields"] & Fields, V["isOptional"]>; /** * Add fields to a union validator. * Equivalent to `v.union(...validator.members.map((m) => addFieldsToValidator(m, fields)))`. */ export declare function addFieldsToValidator, Fields extends PropertyValidators>(validator: V, fields: Fields): AddFieldsToValidator; export declare function addFieldsToValidator | VUnion, Fields extends PropertyValidators>(validator: V, fields: Fields): AddFieldsToValidator; export declare const doc: , TableName extends TableNamesInDataModel>>(schema: Schema, tableName: TableName) => AddFieldsToValidator>; /** * Creates a validator with a type-safe `.id(table)` and a new `.doc(table)`. * Can be used instead of `v` for function arugments & return validators. * However, it cannot be used as part of defining a schema, since it would be * circular. * ```ts * import schema from "./schema"; * export const vv = typedV(schema); * * export const myQuery = query({ * args: { docId: vv.id("mytable") }, * returns: vv.doc("mytable"), * handler: (ctx, args) => ctx.db.get(args.docId), * }) * * @param schema Typically from `import schema from "./schema"`. * @returns A validator like `v` with type-safe `v.id` and a new `v.doc` */ export declare function typedV>(schema: Schema): Omit & { id: >>(tableName: TableName) => VId, "required">; doc: >>(tableName: TableName) => AddFieldsToValidator>; }; /** * 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: (_brand: T) => VString; /** Mark fields as deprecated with this permissive validator typed as null */ export declare const deprecated: Validator; /** A maximally permissive validator that type checks as a given validator. * * If you want to have types that match some validator but you have invalid data * and you want to temporarily not validate schema for this field, * you can use this function to cast the permissive validator. * * Example in a schema: * ```ts * export default defineSchema({ * myTable: defineTable({ * myString: pretend(v.array(v.string())), * }), * }); * //...in some mutation * ctx.db.insert("myTable", { myString: 123 as any }); // no runtime error * ``` * Example in function argument validation: * ```ts * const myQuery = defineQuery({ * args: { myNumber: pretend(v.number()) }, * handler: async (ctx, args) => { * // args.myNumber is typed as number, but it's not validated. * const num = typeof args.myNumber === "number" ? * args.myNumber : Number(args.myNumber); * }, * }); */ export declare const pretend: (_typeToImmitate: T) => T; /** A validator that validates as optional but type checks as required. * * If you want to assume a field is set for type checking, but your data may not * actually have it set for all documents (e.g. when adding a new field), * you can use this function to allow the field to be unset at runtime. * This is unsafe, but can be convenient in these situations: * * 1. You are developing locally and want to add a required field and write * code assuming it is set. Once you push the code & schema, you can update * the data to match before running your code. * 2. You are going to run a migration right after pushing code, and are ok with * and you don't want to edit your code to handle the field being unset, * your app being in an inconsistent state until the migration completes. * * This differs from {@link pretend} in that it type checks the inner validator, * if the value is provided. * * Example in a schema: * ```ts * export default defineSchema({ * myTable: defineTable({ * myString: pretendRequired(v.array(v.string())), * }), * }); * //...in some mutation * ctx.db.insert("myTable", { myString: undefined }); // no runtime error * ``` * Example in function argument validation: * ```ts * const myQuery = defineQuery({ * args: { myNumber: pretendRequired(v.number()) }, * handler: async (ctx, args) => { * // args.myNumber is typed as number, but it might be undefined * const num = args.myNumber || 0; * }, * }); */ export declare const pretendRequired: >(optionalType: T) => T; export declare class ValidationError extends Error { expected: string; got: string; path?: string | undefined; constructor(expected: string, got: string, path?: string | undefined); } /** * Validate a value against a validator. * * WARNING: This does not validate that v.id is an ID for the given table. * It only validates that the ID is a string. Function `args`, `returns` and * schema definitions will validate that the ID is an ID for the given table. * * @param validator The validator to validate against. * @param value The value to validate. * @returns Whether the value is valid against the validator. */ export declare function validate>(validator: T, value: unknown, opts?: { throw?: boolean; db?: GenericDatabaseReader; allowUnknownFields?: boolean; _pathPrefix?: string; }): value is T["type"]; /** * Parse a value, using a Convex validator. This differs from `validate` in that * it strips unknown fields instead of throwing an error on them. * * @param validator - The Convex validator to parse the value against. * @param value - The value to parse. * @returns The parsed value, without fields not specified in the validator. */ export declare function parse>(validator: T, value: unknown): Infer; type NotUndefined = Exclude; /** * A type that converts an optional validator to a required validator. * * This is the inverse of `VOptional`. It takes a validator that may be optional * and returns the equivalent required validator type. * * @example * ```ts * type OptionalString = VOptional>; * type RequiredString = VRequired; // VString * ``` */ export type VRequired> = T extends VId ? VId, "required"> : T extends VString ? VString, "required"> : T extends VFloat64 ? VFloat64, "required"> : T extends VInt64 ? VInt64, "required"> : T extends VBoolean ? VBoolean, "required"> : T extends VNull ? VNull, "required"> : T extends VAny ? VAny, "required"> : T extends VLiteral ? VLiteral, "required"> : T extends VBytes ? VBytes, "required"> : T extends VObject ? VObject, Fields, "required", FieldPaths> : T extends VArray ? VArray, Element, "required"> : T extends VRecord ? VRecord, Key, Value, "required", FieldPaths> : T extends VUnion ? VUnion, Members, "required", FieldPaths> : never; /** * Converts an optional validator to a required validator. * * This is the inverse of `v.optional()`. It takes a validator that may be optional * and returns the equivalent required validator. * * ```ts * const optionalString = v.optional(v.string()); * const requiredString = vRequired(optionalString); // v.string() * * // Already required validators are returned as-is * const alreadyRequired = v.string(); * const stillRequired = vRequired(alreadyRequired); // v.string() * ``` * * @param validator The validator to make required. * @returns A required version of the validator. */ export declare function vRequired>(validator: T): VRequired; export {}; //# sourceMappingURL=validators.d.ts.map