Skip to content

Commit b74e9cc

Browse files
ianmacartneyConvex, Inc.
authored andcommitted
Ian/v.nullable (#42913)
- extra sugar GitOrigin-RevId: fa2580bfde61b27d880d025e1e3c2919257deef4
1 parent dba5fa8 commit b74e9cc

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

npm-packages/convex/src/values/validator.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ export const v = {
222222
optional: <T extends GenericValidator>(value: T) => {
223223
return value.asOptional() as VOptional<T>;
224224
},
225+
226+
/**
227+
* Allows specifying a value or null.
228+
*/
229+
nullable: <T extends GenericValidator>(value: T) => {
230+
return v.union(value, v.null());
231+
},
225232
};
226233

227234
/**

npm-packages/docs/docs/functions/validation.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export default mutation({
9292
});
9393
```
9494

95+
For convenience, `v.nullable(foo)` is equivalent to `v.union(foo, v.null())`.
96+
9597
### Literals
9698

9799
Fields that are a constant can be expressed with `v.literal`. This is especially

npm-packages/private-demos/typescript-modern/convex/messages.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const messageDoc = messageValidator.extend({
3434

3535
export const get = query({
3636
args: { id: v.id("messages") },
37-
returns: v.union(v.null(), messageDoc),
37+
returns: v.nullable(messageDoc),
3838
handler: async (ctx, { id }) => {
3939
return await ctx.db.get(id);
4040
},

0 commit comments

Comments
 (0)