Skip to content

Commit 82b6b70

Browse files
committed
fix(schema): remove default values from improved types
Using `z.default` adds `undefined` to generated types. Not sure why, since I explicitly provide a default value so that the property cannot be `undefined` but well... More info: https://stackoverflow.com/questions/76396222/zod-using-optional-with-default-infers-the-wrong-type
1 parent de0e4e5 commit 82b6b70

File tree

3 files changed

+7
-22
lines changed

3 files changed

+7
-22
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ The loader will also automatically convert the value into a slug to be easily us
108108
It's recommended to use e.g. the title of the entry to be easily searchable and readable.
109109
**Do not use e.g. rich text fields as ids.**
110110

111-
### Improved type safety
111+
### Improved types
112112

113-
By default PocketBase reports `number` and `boolean` fields as not required.
113+
By default PocketBase reports `number` and `boolean` fields as not required, even though the API will always return at least `0` and `false` respectively.
114114
This means that the loader will add `undefined` to the type of these fields.
115115
If you want to enforce that these fields are always present, you can set the `improveTypes` option to `true`.
116116

@@ -123,8 +123,7 @@ const blog = defineCollection({
123123
});
124124
```
125125

126-
This will remove `undefined` from the type of these fields and provide default values with `0` and `false` respectively.
127-
PocketBase will also use these default values when creating new entries in the dashboard.
126+
This will remove `undefined` from the type of these fields and mark them as required.
128127

129128
## Type generation
130129

src/types/pocketbase-loader-options.type.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ export interface PocketBaseLoaderOptions {
6767
jsonSchemas?: Record<string, z.ZodSchema>;
6868
/**
6969
* Whether to improve the types of the generated schema.
70-
* This includes providing `0` or `false` as default values for number and boolean fields, respectively.
71-
* With this option enabled, the schema will not generate `undefined` as possible value for number and boolean fields.
70+
* With this option enabled, the schema will not include `undefined` as possible value for number and boolean fields and mark them as required.
7271
*
7372
* Why do we need this option?
74-
* PocketBase does use these values as the default values, even though it's not specified in the schema.
73+
* The PocketBase API does always return at least `0` or `false` as the default values, even though the fields are not marked as required in the schema.
7574
*/
7675
improveTypes?: boolean;
7776
}

src/utils/parse-schema.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export function parseSchema(
2626
switch (field.type) {
2727
case "number":
2828
// Coerce the value to a number
29-
fieldType = z.coerce.number();
29+
fieldType = z.number();
3030
break;
3131
case "bool":
3232
// Coerce the value to a boolean
33-
fieldType = z.coerce.boolean();
33+
fieldType = z.boolean();
3434
break;
3535
case "date":
3636
case "autodate":
@@ -82,19 +82,6 @@ export function parseSchema(
8282
// Improve number and boolean types by providing default values
8383
(improveTypes && (field.type === "number" || field.type === "bool"));
8484

85-
if (improveTypes) {
86-
switch (field.type) {
87-
case "number":
88-
// If the field is a number, provide a default value of 0
89-
fieldType = fieldType.default(0);
90-
break;
91-
case "bool":
92-
// If the field is a boolean, provide a default value of false
93-
fieldType = fieldType.default(false);
94-
break;
95-
}
96-
}
97-
9885
// If the field is not required, mark it as optional
9986
if (!isRequired) {
10087
fieldType = z.preprocess(

0 commit comments

Comments
 (0)