Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions npm-packages/convex/src/values/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <T extends PropertyValidators>(fields: T) => {
return new VObject<ObjectType<T>, T>({ isOptional: "required", fields });
object: <T extends PropertyValidators>(
fields: T,
options?: { name?: string | undefined },
) => {
return new VObject<ObjectType<T>, T>({ isOptional: "required", fields, name: options?.name });
},

/**
Expand Down
12 changes: 11 additions & 1 deletion npm-packages/convex/src/values/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -303,6 +310,7 @@ export class VObject<
}
});
this.fields = fields;
this.name = name;
}
/** @internal */
get json(): ValidatorJSON {
Expand All @@ -317,13 +325,15 @@ export class VObject<
},
]),
),
name: this.name,
};
}
/** @internal */
asOptional() {
return new VObject<Type | undefined, Fields, "optional", FieldPaths>({
isOptional: "optional",
fields: this.fields,
name: this.name,
});
}
}
Expand Down Expand Up @@ -660,7 +670,7 @@ export type ValidatorJSON =
keys: RecordKeyValidatorJSON;
values: RecordValueValidatorJSON;
}
| { type: "object"; value: Record<string, ObjectFieldType> }
| { type: "object"; value: Record<string, ObjectFieldType>, name: string|undefined }
| { type: "union"; value: ValidatorJSON[] };

export type RecordKeyValidatorJSON =
Expand Down