diff --git a/npm-packages/convex/src/values/validator.ts b/npm-packages/convex/src/values/validator.ts index 8d988fc10..83d43beb1 100644 --- a/npm-packages/convex/src/values/validator.ts +++ b/npm-packages/convex/src/values/validator.ts @@ -165,10 +165,14 @@ export const v = { /** * Validates that the value is an Object with the given properties. - * @param fields An object specifying the validator for each property. + * @param options Optional configuration for this object validator. + * @param options.name An optional name for this object validator. */ - object: (fields: T) => { - return new VObject, T>({ isOptional: "required", fields }); + object: ( + fields: T, + options?: { name?: string | undefined }, + ) => { + return new VObject, T>({ isOptional: "required", fields, name: options?.name }); }, /** diff --git a/npm-packages/convex/src/values/validators.ts b/npm-packages/convex/src/values/validators.ts index 6156dbecf..3c3b62d90 100644 --- a/npm-packages/convex/src/values/validators.ts +++ b/npm-packages/convex/src/values/validators.ts @@ -286,15 +286,22 @@ export class VObject< */ readonly kind = "object" as const; + /** + * A name for this object. + */ + readonly name: string|undefined; + /** * Usually you'd use `v.object({ ... })` instead. */ constructor({ isOptional, fields, + name, }: { isOptional: IsOptional; fields: Fields; + name: string|undefined; }) { super({ isOptional }); globalThis.Object.values(fields).forEach((v) => { @@ -303,6 +310,7 @@ export class VObject< } }); this.fields = fields; + this.name = name; } /** @internal */ get json(): ValidatorJSON { @@ -317,6 +325,7 @@ export class VObject< }, ]), ), + name: this.name, }; } /** @internal */ @@ -324,6 +333,7 @@ export class VObject< return new VObject({ isOptional: "optional", fields: this.fields, + name: this.name, }); } } @@ -660,7 +670,7 @@ export type ValidatorJSON = keys: RecordKeyValidatorJSON; values: RecordValueValidatorJSON; } - | { type: "object"; value: Record } + | { type: "object"; value: Record, name: string|undefined } | { type: "union"; value: ValidatorJSON[] }; export type RecordKeyValidatorJSON =