convex-helpers 0.1.96 → 0.1.97
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.test.ts +29 -3
- package/package.json +1 -1
- package/validators.d.ts +26 -16
- package/validators.d.ts.map +1 -1
- package/validators.js +42 -20
- package/validators.ts +93 -28
package/index.test.ts
CHANGED
|
@@ -1,7 +1,32 @@
|
|
|
1
1
|
import { v } from "convex/values";
|
|
2
|
-
import { withoutSystemFields } from "./index.js";
|
|
2
|
+
import { withoutSystemFields, pick, omit } from "./index.js";
|
|
3
3
|
import { test, expect, expectTypeOf } from "vitest";
|
|
4
4
|
|
|
5
|
+
test("pick", () => {
|
|
6
|
+
const obj = { a: "a", b: "b", c: "c" };
|
|
7
|
+
const picked = pick(obj, ["a", "b"]);
|
|
8
|
+
expect(picked).toEqual({ a: "a", b: "b" });
|
|
9
|
+
expect(pick(obj, [])).toEqual({});
|
|
10
|
+
expect(pick(obj, ["a", "b", "c"])).toEqual({ a: "a", b: "b", c: "c" });
|
|
11
|
+
expect(pick(obj, ["a", "b", "c", "d" as any])).toEqual({
|
|
12
|
+
a: "a",
|
|
13
|
+
b: "b",
|
|
14
|
+
c: "c",
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
expectTypeOf(picked).toEqualTypeOf<{ a: string; b: string }>();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test("omit", () => {
|
|
21
|
+
const obj = { a: "a", b: "b", c: "c" };
|
|
22
|
+
const omitted = omit(obj, ["a", "b"]);
|
|
23
|
+
expect(omitted).toEqual({ c: "c" });
|
|
24
|
+
expect(omit(obj, [])).toEqual({ a: "a", b: "b", c: "c" });
|
|
25
|
+
expect(omit(obj, ["a", "b", "c", "d" as any])).toEqual({});
|
|
26
|
+
|
|
27
|
+
expectTypeOf(omitted).toEqualTypeOf<{ c: string }>();
|
|
28
|
+
});
|
|
29
|
+
|
|
5
30
|
test("withoutSystemFields", () => {
|
|
6
31
|
const obj = { _id: "1", _creationTime: 1, a: "a" };
|
|
7
32
|
const without = withoutSystemFields(obj);
|
|
@@ -20,6 +45,7 @@ test("withoutSystemFields type when it's a union", () => {
|
|
|
20
45
|
| { _id: string; _creationTime: number };
|
|
21
46
|
const without = withoutSystemFields(obj);
|
|
22
47
|
expect(without).toEqual({ a: "a" });
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
23
49
|
expectTypeOf(without).toEqualTypeOf<{ a: string } | {}>();
|
|
24
50
|
const obj2 = { _id: "1", _creationTime: 1, a: "a" } as
|
|
25
51
|
| { _id: string; _creationTime: number; a: string }
|
|
@@ -35,7 +61,7 @@ test("withoutSystemFields works on validators too", () => {
|
|
|
35
61
|
_creationTime: v.number(),
|
|
36
62
|
a: v.string(),
|
|
37
63
|
});
|
|
38
|
-
const { _id, _creationTime, ...
|
|
64
|
+
const { _id, _creationTime, ..._rest } = validator.fields;
|
|
39
65
|
const without = withoutSystemFields(validator.fields);
|
|
40
|
-
expectTypeOf(without).toEqualTypeOf<typeof
|
|
66
|
+
expectTypeOf(without).toEqualTypeOf<typeof _rest>();
|
|
41
67
|
});
|
package/package.json
CHANGED
package/validators.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { GenericValidator, Infer, ObjectType, PropertyValidators, VObject, VOptional, VString, VUnion, Validator } from "convex/values";
|
|
1
|
+
import type { GenericValidator, Infer, ObjectType, OptionalProperty, PropertyValidators, VLiteral, VObject, VOptional, VString, VUnion, Validator } from "convex/values";
|
|
2
2
|
import type { Expand } from "./index.js";
|
|
3
3
|
import type { DataModelFromSchemaDefinition, GenericDatabaseReader, GenericDataModel, SchemaDefinition, TableNamesInDataModel } from "convex/server";
|
|
4
4
|
/**
|
|
@@ -18,7 +18,7 @@ import type { DataModelFromSchemaDefinition, GenericDatabaseReader, GenericDataM
|
|
|
18
18
|
* @param args Values you want to use in a union of literals.
|
|
19
19
|
* @returns A validator for the union of the literals.
|
|
20
20
|
*/
|
|
21
|
-
export declare const literals: <
|
|
21
|
+
export declare const literals: <T extends (string | number | boolean | bigint)[]>(...args: T) => VUnion<T[number], VLiteral<T[number]>[]>;
|
|
22
22
|
/**
|
|
23
23
|
* nullable define a validator that can be the value or null more consisely.
|
|
24
24
|
*
|
|
@@ -32,29 +32,38 @@ export declare const nullable: <V extends Validator<any, "required", any>>(x: V)
|
|
|
32
32
|
* e.g. `partial({a: v.string(), b: v.number()})` is equivalent to
|
|
33
33
|
* `{a: v.optional(v.string()), b: v.optional(v.number())}`
|
|
34
34
|
*
|
|
35
|
+
* And `partial(v.object({a: v.string(), b: v.number()}))` is equivalent to
|
|
36
|
+
* `v.object({a: v.optional(v.string()), b: v.optional(v.number())})`
|
|
37
|
+
*
|
|
35
38
|
* @param obj The object of validators to make optional. e.g. {a: v.string()}
|
|
36
39
|
* @returns A new object of validators that can be the value or undefined.
|
|
37
40
|
*/
|
|
38
|
-
export declare
|
|
39
|
-
|
|
41
|
+
export declare function partial<T extends PropertyValidators>(obj: T): {
|
|
42
|
+
[K in keyof T]: VOptional<T[K]>;
|
|
43
|
+
};
|
|
44
|
+
export declare function partial<T, V extends Record<string, GenericValidator>, O extends OptionalProperty>(obj: VObject<T, V, O>): PartialVObject<T, V, O>;
|
|
45
|
+
type PartialVObject<T, V extends Record<string, GenericValidator>, O extends OptionalProperty> = VObject<Partial<T>, {
|
|
46
|
+
[K in keyof V]: VOptional<V[K]>;
|
|
47
|
+
}, O>;
|
|
48
|
+
/** @deprecated Use `v.string()` instead. Any string value. */
|
|
40
49
|
export declare const string: VString<string, "required">;
|
|
41
|
-
/** JavaScript number, represented as a float64 in the database. */
|
|
50
|
+
/** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */
|
|
42
51
|
export declare const number: import("convex/values").VFloat64<number, "required">;
|
|
43
|
-
/** JavaScript number, represented as a float64 in the database. */
|
|
52
|
+
/** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */
|
|
44
53
|
export declare const float64: import("convex/values").VFloat64<number, "required">;
|
|
45
|
-
/** boolean value. For typing it only as true, use `l(true)` */
|
|
54
|
+
/** @deprecated Use `v.boolean()` instead. boolean value. For typing it only as true, use `l(true)` */
|
|
46
55
|
export declare const boolean: import("convex/values").VBoolean<boolean, "required">;
|
|
47
|
-
/** bigint, though stored as an int64 in the database. */
|
|
48
|
-
export declare const
|
|
49
|
-
/** bigint, though stored as an int64 in the database. */
|
|
56
|
+
/** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */
|
|
57
|
+
export declare const biging: import("convex/values").VInt64<bigint, "required">;
|
|
58
|
+
/** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */
|
|
50
59
|
export declare const int64: import("convex/values").VInt64<bigint, "required">;
|
|
51
|
-
/** Any Convex value */
|
|
60
|
+
/** @deprecated Use `v.any()` instead. Any Convex value */
|
|
52
61
|
export declare const any: import("convex/values").VAny<any, "required", string>;
|
|
53
|
-
/** Null value. Underscore is so it doesn't shadow the null builtin */
|
|
62
|
+
/** @deprecated Use `v.null()` instead. Null value. Underscore is so it doesn't shadow the null builtin */
|
|
54
63
|
export declare const null_: import("convex/values").VNull<null, "required">;
|
|
55
|
-
/**
|
|
56
|
-
export declare const id: <TableName extends string>(tableName: TableName) => import("convex/values").VId<import("convex/values").GenericId<TableName>, "required">, object: <T_2 extends PropertyValidators>(fields: T_2) => VObject<import("convex/server").Expand<{ [Property in { [Property_1 in keyof T_2]: T_2[Property_1]["isOptional"] extends "optional" ? Property_1 : never; }[keyof T_2]]?: Exclude<Infer<T_2[Property]>, undefined> | undefined; } & { [Property_1 in Exclude<keyof T_2, { [Property in keyof T_2]: T_2[Property]["isOptional"] extends "optional" ? Property : never; }[keyof 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>, array: <T_1 extends Validator<any, "required", any>>(element: T_1) => import("convex/values").VArray<T_1["type"][], T_1, "required">, bytes: () => import("convex/values").VBytes<ArrayBuffer, "required">, literal: <T extends string | number | bigint | boolean>(literal: T) =>
|
|
57
|
-
/** ArrayBuffer validator. */
|
|
64
|
+
/** @deprecated Use `v.*()` instead. */
|
|
65
|
+
export declare const id: <TableName extends string>(tableName: TableName) => import("convex/values").VId<import("convex/values").GenericId<TableName>, "required">, object: <T_2 extends PropertyValidators>(fields: T_2) => VObject<import("convex/server").Expand<{ [Property in { [Property_1 in keyof T_2]: T_2[Property_1]["isOptional"] extends "optional" ? Property_1 : never; }[keyof T_2]]?: Exclude<Infer<T_2[Property]>, undefined> | undefined; } & { [Property_1 in Exclude<keyof T_2, { [Property in keyof T_2]: T_2[Property]["isOptional"] extends "optional" ? Property : never; }[keyof 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>, array: <T_1 extends Validator<any, "required", any>>(element: T_1) => import("convex/values").VArray<T_1["type"][], T_1, "required">, bytes: () => import("convex/values").VBytes<ArrayBuffer, "required">, literal: <T extends string | number | bigint | boolean>(literal: T) => VLiteral<T, "required">, optional: <T_4 extends GenericValidator>(value: T_4) => VOptional<T_4>, union: <T_3 extends Validator<any, "required", any>[]>(...members: T_3) => VUnion<T_3[number]["type"], T_3, "required", T_3[number]["fieldPaths"]>;
|
|
66
|
+
/** @deprecated Use `v.bytes()` instead. ArrayBuffer validator. */
|
|
58
67
|
export declare const arrayBuffer: import("convex/values").VBytes<ArrayBuffer, "required">;
|
|
59
68
|
/**
|
|
60
69
|
* Utility to get the validators for fields associated with a table.
|
|
@@ -129,7 +138,7 @@ export declare function typedV<Schema extends SchemaDefinition<any, boolean>>(sc
|
|
|
129
138
|
boolean: () => import("convex/values").VBoolean<boolean, "required">;
|
|
130
139
|
string: () => VString<string, "required">;
|
|
131
140
|
bytes: () => import("convex/values").VBytes<ArrayBuffer, "required">;
|
|
132
|
-
literal: <T extends string | number | bigint | boolean>(literal: T) =>
|
|
141
|
+
literal: <T extends string | number | bigint | boolean>(literal: T) => VLiteral<T, "required">;
|
|
133
142
|
array: <T_1 extends Validator<any, "required", any>>(element: T_1) => import("convex/values").VArray<T_1["type"][], T_1, "required">;
|
|
134
143
|
object: <T_2 extends PropertyValidators>(fields: T_2) => VObject<import("convex/server").Expand<{ [Property in { [Property_1 in keyof T_2]: T_2[Property_1]["isOptional"] extends "optional" ? Property_1 : never; }[keyof T_2]]?: Exclude<Infer<T_2[Property]>, undefined> | undefined; } & { [Property_1 in Exclude<keyof T_2, { [Property in keyof T_2]: T_2[Property]["isOptional"] extends "optional" ? Property : never; }[keyof 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>;
|
|
135
144
|
record: <Key extends Validator<string, "required", any>, Value extends Validator<any, "required", any>>(keys: Key, values: Value) => import("convex/values").VRecord<Record<Infer<Key>, Value["type"]>, Key, Value, "required", string>;
|
|
@@ -247,4 +256,5 @@ export declare function validate<T extends Validator<any, any, any>>(validator:
|
|
|
247
256
|
* @returns The parsed value, without fields not specified in the validator.
|
|
248
257
|
*/
|
|
249
258
|
export declare function parse<T extends Validator<any, any, any>>(validator: T, value: unknown): Infer<T>;
|
|
259
|
+
export {};
|
|
250
260
|
//# sourceMappingURL=validators.d.ts.map
|
package/validators.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["validators.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,gBAAgB,EAChB,KAAK,EACL,UAAU,EACV,kBAAkB,EAClB,OAAO,EACP,SAAS,EACT,OAAO,EACP,MAAM,EACN,SAAS,EACV,MAAM,eAAe,CAAC;AAEvB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAA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B,SAAQ,KAAK;IAE/B,QAAQ,EAAE,MAAM;IAChB,GAAG,EAAE,MAAM;IACX,IAAI,CAAC,EAAE,MAAM;gBAFb,QAAQ,EAAE,MAAM,EAChB,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,MAAM,YAAA;CAMvB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EACzD,SAAS,EAAE,CAAC,EACZ,KAAK,EAAE,OAAO,EACd,IAAI,CAAC,EAAE;IAEL,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB,EAAE,CAAC,EAAE,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;IAE7C,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAG7B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,GACA,KAAK,IAAI,CAAC,CAAC,MAAM,CAAC,CA+LpB;AAED;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EACtD,SAAS,EAAE,CAAC,EACZ,KAAK,EAAE,OAAO,GACb,KAAK,CAAC,CAAC,CAAC,CAGV"}
|
package/validators.js
CHANGED
|
@@ -17,9 +17,6 @@ import { v } from "convex/values";
|
|
|
17
17
|
* @returns A validator for the union of the literals.
|
|
18
18
|
*/
|
|
19
19
|
export const literals = (...args) => {
|
|
20
|
-
// The `any` above is unfortunate, because then we cannot get proper types
|
|
21
|
-
// for `validator.members`, but without it, TypeScript seems to have a hard
|
|
22
|
-
// time inferring the TS type for the first parameter.
|
|
23
20
|
return v.union(...args.map(v.literal));
|
|
24
21
|
};
|
|
25
22
|
/**
|
|
@@ -29,42 +26,67 @@ export const literals = (...args) => {
|
|
|
29
26
|
* @returns A new validator that can be the value or null.
|
|
30
27
|
*/
|
|
31
28
|
export const nullable = (x) => v.union(v.null(), x);
|
|
29
|
+
export function partial(fieldsOrObjOrUnion) {
|
|
30
|
+
if (fieldsOrObjOrUnion.isConvexValidator) {
|
|
31
|
+
if (fieldsOrObjOrUnion.kind === "object") {
|
|
32
|
+
return partialVObject(fieldsOrObjOrUnion);
|
|
33
|
+
}
|
|
34
|
+
return fieldsOrObjOrUnion;
|
|
35
|
+
}
|
|
36
|
+
return partialFields(fieldsOrObjOrUnion);
|
|
37
|
+
}
|
|
32
38
|
/**
|
|
33
|
-
*
|
|
39
|
+
* partialFields helps you define an object of optional validators more concisely.
|
|
34
40
|
*
|
|
35
|
-
* e.g. `
|
|
41
|
+
* e.g. `partialFields({a: v.string(), b: v.number()})` is equivalent to
|
|
36
42
|
* `{a: v.optional(v.string()), b: v.optional(v.number())}`
|
|
37
43
|
*
|
|
38
44
|
* @param obj The object of validators to make optional. e.g. {a: v.string()}
|
|
39
45
|
* @returns A new object of validators that can be the value or undefined.
|
|
40
46
|
*/
|
|
41
|
-
|
|
47
|
+
function partialFields(obj) {
|
|
42
48
|
return Object.fromEntries(Object.entries(obj).map(([k, vv]) => [
|
|
43
49
|
k,
|
|
44
50
|
vv.isOptional === "optional" ? vv : v.optional(vv),
|
|
45
51
|
]));
|
|
46
|
-
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* partialObject helps you define an object of optional validators more concisely.
|
|
55
|
+
*
|
|
56
|
+
* e.g. `partialObject({a: v.string(), b: v.number()})` is equivalent to
|
|
57
|
+
* `{a: v.optional(v.string()), b: v.optional(v.number())}`
|
|
58
|
+
*
|
|
59
|
+
* @param obj The object of validators to make optional. e.g. {a: v.string()}
|
|
60
|
+
* @returns A new object of validators that can be the value or undefined.
|
|
61
|
+
*/
|
|
62
|
+
function partialVObject(obj) {
|
|
63
|
+
const o = v.object(partialFields(obj.fields));
|
|
64
|
+
if (obj.isOptional === "optional") {
|
|
65
|
+
return v.optional(o);
|
|
66
|
+
}
|
|
67
|
+
return o;
|
|
68
|
+
}
|
|
47
69
|
// Shorthand for defining validators that look like types.
|
|
48
|
-
/** Any string value. */
|
|
70
|
+
/** @deprecated Use `v.string()` instead. Any string value. */
|
|
49
71
|
export const string = v.string();
|
|
50
|
-
/** JavaScript number, represented as a float64 in the database. */
|
|
72
|
+
/** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */
|
|
51
73
|
export const number = v.float64();
|
|
52
|
-
/** JavaScript number, represented as a float64 in the database. */
|
|
74
|
+
/** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */
|
|
53
75
|
export const float64 = v.float64();
|
|
54
|
-
/** boolean value. For typing it only as true, use `l(true)` */
|
|
76
|
+
/** @deprecated Use `v.boolean()` instead. boolean value. For typing it only as true, use `l(true)` */
|
|
55
77
|
export const boolean = v.boolean();
|
|
56
|
-
/** bigint, though stored as an int64 in the database. */
|
|
57
|
-
export const
|
|
58
|
-
/** bigint, though stored as an int64 in the database. */
|
|
78
|
+
/** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */
|
|
79
|
+
export const biging = v.int64();
|
|
80
|
+
/** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */
|
|
59
81
|
export const int64 = v.int64();
|
|
60
|
-
/** Any Convex value */
|
|
82
|
+
/** @deprecated Use `v.any()` instead. Any Convex value */
|
|
61
83
|
export const any = v.any();
|
|
62
|
-
/** Null value. Underscore is so it doesn't shadow the null builtin */
|
|
84
|
+
/** @deprecated Use `v.null()` instead. Null value. Underscore is so it doesn't shadow the null builtin */
|
|
63
85
|
export const null_ = v.null();
|
|
64
|
-
/**
|
|
86
|
+
/** @deprecated Use `v.*()` instead. */
|
|
65
87
|
export const { id, object, array, bytes, literal, optional, union } = v;
|
|
66
|
-
/** ArrayBuffer validator. */
|
|
67
|
-
export const arrayBuffer = bytes();
|
|
88
|
+
/** @deprecated Use `v.bytes()` instead. ArrayBuffer validator. */
|
|
89
|
+
export const arrayBuffer = v.bytes();
|
|
68
90
|
/**
|
|
69
91
|
* Utility to get the validators for fields associated with a table.
|
|
70
92
|
* e.g. for systemFields("users") it would return:
|
|
@@ -353,7 +375,7 @@ export function validate(validator, value, opts) {
|
|
|
353
375
|
prototype?.constructor?.name === "Object";
|
|
354
376
|
if (!isSimple) {
|
|
355
377
|
expected =
|
|
356
|
-
prototype?.constructor?.name ??
|
|
378
|
+
(prototype?.constructor?.name ?? typeof prototype) || "object";
|
|
357
379
|
valid = false;
|
|
358
380
|
break;
|
|
359
381
|
}
|
package/validators.ts
CHANGED
|
@@ -2,7 +2,9 @@ import type {
|
|
|
2
2
|
GenericValidator,
|
|
3
3
|
Infer,
|
|
4
4
|
ObjectType,
|
|
5
|
+
OptionalProperty,
|
|
5
6
|
PropertyValidators,
|
|
7
|
+
VLiteral,
|
|
6
8
|
VObject,
|
|
7
9
|
VOptional,
|
|
8
10
|
VString,
|
|
@@ -36,17 +38,13 @@ import type {
|
|
|
36
38
|
* @param args Values you want to use in a union of literals.
|
|
37
39
|
* @returns A validator for the union of the literals.
|
|
38
40
|
*/
|
|
39
|
-
export const literals = <
|
|
40
|
-
V extends string | number | boolean | bigint,
|
|
41
|
-
T extends V[],
|
|
42
|
-
>(
|
|
41
|
+
export const literals = <T extends (string | number | boolean | bigint)[]>(
|
|
43
42
|
...args: T
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return v.union(...args.map(v.literal)) as any;
|
|
43
|
+
) => {
|
|
44
|
+
return v.union(...args.map(v.literal)) as VUnion<
|
|
45
|
+
T[number],
|
|
46
|
+
VLiteral<T[number]>[]
|
|
47
|
+
>;
|
|
50
48
|
};
|
|
51
49
|
|
|
52
50
|
/**
|
|
@@ -64,42 +62,109 @@ export const nullable = <V extends Validator<any, "required", any>>(x: V) =>
|
|
|
64
62
|
* e.g. `partial({a: v.string(), b: v.number()})` is equivalent to
|
|
65
63
|
* `{a: v.optional(v.string()), b: v.optional(v.number())}`
|
|
66
64
|
*
|
|
65
|
+
* And `partial(v.object({a: v.string(), b: v.number()}))` is equivalent to
|
|
66
|
+
* `v.object({a: v.optional(v.string()), b: v.optional(v.number())})`
|
|
67
|
+
*
|
|
68
|
+
* @param obj The object of validators to make optional. e.g. {a: v.string()}
|
|
69
|
+
* @returns A new object of validators that can be the value or undefined.
|
|
70
|
+
*/
|
|
71
|
+
export function partial<T extends PropertyValidators>(
|
|
72
|
+
obj: T,
|
|
73
|
+
): {
|
|
74
|
+
[K in keyof T]: VOptional<T[K]>;
|
|
75
|
+
};
|
|
76
|
+
export function partial<
|
|
77
|
+
T,
|
|
78
|
+
V extends Record<string, GenericValidator>,
|
|
79
|
+
O extends OptionalProperty,
|
|
80
|
+
>(obj: VObject<T, V, O>): PartialVObject<T, V, O>;
|
|
81
|
+
export function partial(fieldsOrObjOrUnion: any) {
|
|
82
|
+
if (fieldsOrObjOrUnion.isConvexValidator) {
|
|
83
|
+
if (fieldsOrObjOrUnion.kind === "object") {
|
|
84
|
+
return partialVObject(fieldsOrObjOrUnion);
|
|
85
|
+
}
|
|
86
|
+
return fieldsOrObjOrUnion;
|
|
87
|
+
}
|
|
88
|
+
return partialFields(fieldsOrObjOrUnion);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* partialFields helps you define an object of optional validators more concisely.
|
|
93
|
+
*
|
|
94
|
+
* e.g. `partialFields({a: v.string(), b: v.number()})` is equivalent to
|
|
95
|
+
* `{a: v.optional(v.string()), b: v.optional(v.number())}`
|
|
96
|
+
*
|
|
67
97
|
* @param obj The object of validators to make optional. e.g. {a: v.string()}
|
|
68
98
|
* @returns A new object of validators that can be the value or undefined.
|
|
69
99
|
*/
|
|
70
|
-
|
|
100
|
+
function partialFields<T extends PropertyValidators>(
|
|
101
|
+
obj: T,
|
|
102
|
+
): {
|
|
103
|
+
[K in keyof T]: VOptional<T[K]>;
|
|
104
|
+
} {
|
|
71
105
|
return Object.fromEntries(
|
|
72
106
|
Object.entries(obj).map(([k, vv]) => [
|
|
73
107
|
k,
|
|
74
108
|
vv.isOptional === "optional" ? vv : v.optional(vv),
|
|
75
109
|
]),
|
|
76
|
-
) as
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
110
|
+
) as any;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* partialObject helps you define an object of optional validators more concisely.
|
|
115
|
+
*
|
|
116
|
+
* e.g. `partialObject({a: v.string(), b: v.number()})` is equivalent to
|
|
117
|
+
* `{a: v.optional(v.string()), b: v.optional(v.number())}`
|
|
118
|
+
*
|
|
119
|
+
* @param obj The object of validators to make optional. e.g. {a: v.string()}
|
|
120
|
+
* @returns A new object of validators that can be the value or undefined.
|
|
121
|
+
*/
|
|
122
|
+
function partialVObject<
|
|
123
|
+
T,
|
|
124
|
+
V extends Record<string, GenericValidator>,
|
|
125
|
+
O extends OptionalProperty,
|
|
126
|
+
>(obj: VObject<T, V, O>): PartialVObject<T, V, O> {
|
|
127
|
+
const o = v.object(partialFields(obj.fields));
|
|
128
|
+
if (obj.isOptional === "optional") {
|
|
129
|
+
return v.optional(o) as any;
|
|
130
|
+
}
|
|
131
|
+
return o as any;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
type PartialVObject<
|
|
135
|
+
T,
|
|
136
|
+
V extends Record<string, GenericValidator>,
|
|
137
|
+
O extends OptionalProperty,
|
|
138
|
+
> = VObject<
|
|
139
|
+
Partial<T>,
|
|
140
|
+
{
|
|
141
|
+
[K in keyof V]: VOptional<V[K]>;
|
|
142
|
+
},
|
|
143
|
+
O
|
|
144
|
+
>;
|
|
80
145
|
|
|
81
146
|
// Shorthand for defining validators that look like types.
|
|
82
147
|
|
|
83
|
-
/** Any string value. */
|
|
148
|
+
/** @deprecated Use `v.string()` instead. Any string value. */
|
|
84
149
|
export const string = v.string();
|
|
85
|
-
/** JavaScript number, represented as a float64 in the database. */
|
|
150
|
+
/** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */
|
|
86
151
|
export const number = v.float64();
|
|
87
|
-
/** JavaScript number, represented as a float64 in the database. */
|
|
152
|
+
/** @deprecated Use `v.float64()` instead. JavaScript number, represented as a float64 in the database. */
|
|
88
153
|
export const float64 = v.float64();
|
|
89
|
-
/** boolean value. For typing it only as true, use `l(true)` */
|
|
154
|
+
/** @deprecated Use `v.boolean()` instead. boolean value. For typing it only as true, use `l(true)` */
|
|
90
155
|
export const boolean = v.boolean();
|
|
91
|
-
/** bigint, though stored as an int64 in the database. */
|
|
92
|
-
export const
|
|
93
|
-
/** bigint, though stored as an int64 in the database. */
|
|
156
|
+
/** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */
|
|
157
|
+
export const biging = v.int64();
|
|
158
|
+
/** @deprecated Use `v.int64()` instead. bigint, though stored as an int64 in the database. */
|
|
94
159
|
export const int64 = v.int64();
|
|
95
|
-
/** Any Convex value */
|
|
160
|
+
/** @deprecated Use `v.any()` instead. Any Convex value */
|
|
96
161
|
export const any = v.any();
|
|
97
|
-
/** Null value. Underscore is so it doesn't shadow the null builtin */
|
|
162
|
+
/** @deprecated Use `v.null()` instead. Null value. Underscore is so it doesn't shadow the null builtin */
|
|
98
163
|
export const null_ = v.null();
|
|
99
|
-
/**
|
|
164
|
+
/** @deprecated Use `v.*()` instead. */
|
|
100
165
|
export const { id, object, array, bytes, literal, optional, union } = v;
|
|
101
|
-
/** ArrayBuffer validator. */
|
|
102
|
-
export const arrayBuffer = bytes();
|
|
166
|
+
/** @deprecated Use `v.bytes()` instead. ArrayBuffer validator. */
|
|
167
|
+
export const arrayBuffer = v.bytes();
|
|
103
168
|
|
|
104
169
|
/**
|
|
105
170
|
* Utility to get the validators for fields associated with a table.
|
|
@@ -478,7 +543,7 @@ export function validate<T extends Validator<any, any, any>>(
|
|
|
478
543
|
|
|
479
544
|
if (!isSimple) {
|
|
480
545
|
expected =
|
|
481
|
-
prototype?.constructor?.name ??
|
|
546
|
+
(prototype?.constructor?.name ?? typeof prototype) || "object";
|
|
482
547
|
valid = false;
|
|
483
548
|
break;
|
|
484
549
|
}
|