@nakanoaas/notion-valibot-schema 0.3.0 → 0.6.0

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/README.md CHANGED
@@ -153,13 +153,15 @@ const tasks = v.parse(TaskListSchema, results);
153
153
  | **Date** (Full) | `FullDateSchema` / `NullableFullDateSchema` | `{ start: Date; end: Date \| null; time_zone: string \| null }` / `{ start: Date; end: Date \| null; time_zone: string \| null } \| null` |
154
154
  | **Relation** | `RelationSchema` | `string[]` (Page IDs) |
155
155
  | **Relation** (Single) | `SingleRelationSchema` | `string` (Page ID) |
156
+ | **Rollup** (Simple) | `RollupSimpleSchema(schema)` | `Inferred<schema>` |
156
157
  | **Rollup** (Array) | `RollupArraySchema(schema)` | `Inferred<schema>[]` |
157
158
  | **Rollup** (Single) | `SingleRollupArraySchema(schema)` | `Inferred<schema>` |
158
159
  | **Rollup** (Single, Nullable) | `NullableSingleRollupArraySchema(schema)` | `Inferred<schema> \| null` |
160
+ | **Formula** | `FormulaSchema(schema)` | `Inferred<schema>` |
159
161
  | **URL** | `UrlSchema` | `string` |
160
162
  | **Email** | `EmailSchema` | `string` |
161
163
  | **Phone** | `PhoneNumberSchema` | `string` |
162
- | **Files** | `FileArraySchema` | `string[]` (URLs) |
164
+ | **Files** | `FileSchema` | `string[]` (URLs) |
163
165
  | **Files** (Single) | `SingleFileSchema` / `NullableSingleFileSchema` | `string` (URL) / `string \| null` |
164
166
  | **Created/Edited By** | `CreatedBySchema` / `LastEditedBySchema` | `string` (User ID) |
165
167
  | **Created/Edited Time**| `CreatedTimeSchema` / `LastEditedTimeSchema` | `Date` |
@@ -171,15 +173,34 @@ const tasks = v.parse(TaskListSchema, results);
171
173
  ### Advanced Schemas
172
174
 
173
175
  #### Formulas
174
- Formulas in Notion can return different types (string, number, boolean, date). Use `FormulaSchema` with a specific inner schema to handle this.
176
+ Formulas in Notion can return different types (string, number, boolean, date). Use `FormulaSchema` with the matching inner schema for each formula property.
175
177
 
176
178
  ```ts
177
- import { FormulaSchema, RichTextSchema } from "@nakanoaas/notion-valibot-schema";
179
+ import * as v from "valibot";
180
+ import {
181
+ BooleanFormulaSchema,
182
+ FormulaSchema,
183
+ NumberSchema,
184
+ SingleDateSchema,
185
+ StringFormulaSchema,
186
+ } from "@nakanoaas/notion-valibot-schema";
178
187
 
179
- const MySchema = v.object({
180
- // If your formula returns text
181
- MyFormula: FormulaSchema(RichTextSchema),
188
+ const PageSchema = v.object({
189
+ id: v.string(),
190
+ properties: v.object({
191
+ FormulaText: FormulaSchema(StringFormulaSchema),
192
+ FormulaNumber: FormulaSchema(NumberSchema),
193
+ FormulaBoolean: FormulaSchema(BooleanFormulaSchema),
194
+ FormulaDate: FormulaSchema(SingleDateSchema),
195
+ }),
182
196
  });
197
+
198
+ const page = await notion.pages.retrieve({ page_id: "..." });
199
+ const parsed = v.parse(PageSchema, page);
200
+ // parsed.properties.FormulaText: string
201
+ // parsed.properties.FormulaNumber: number
202
+ // parsed.properties.FormulaBoolean: boolean
203
+ // parsed.properties.FormulaDate: Date
183
204
  ```
184
205
 
185
206
  #### Rollups
@@ -187,19 +208,20 @@ Rollups are powerful but complex. We provide helpers for common rollup types.
187
208
 
188
209
  ```ts
189
210
  import {
190
- RollupNumberSchema,
191
- RollupDateSchema,
211
+ RollupSimpleSchema,
192
212
  RollupArraySchema,
193
213
  SingleRollupArraySchema,
194
214
  NullableSingleRollupArraySchema,
215
+ NumberSchema,
216
+ SingleDateSchema,
195
217
  } from "@nakanoaas/notion-valibot-schema";
196
218
 
197
219
  const MySchema = v.object({
198
220
  // Sum/Average rollup (returns number)
199
- TotalCost: RollupNumberSchema,
221
+ TotalCost: RollupSimpleSchema(NumberSchema),
200
222
 
201
223
  // Date rollup (returns Date)
202
- LatestMeeting: RollupDateSchema,
224
+ LatestMeeting: RollupSimpleSchema(SingleDateSchema),
203
225
 
204
226
  // Array rollup (e.g., pulling tags from related items)
205
227
  AllTags: RollupArraySchema(v.string()),
@@ -1,7 +1,7 @@
1
1
  const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
2
2
  let valibot = require("valibot");
3
3
  valibot = require_runtime.__toESM(valibot, 1);
4
- //#region src/file.ts
4
+ //#region src/files.ts
5
5
  /**
6
6
  * Schema for a Notion file object structure.
7
7
  *
@@ -54,12 +54,12 @@ const InnerFileSchema = valibot.variant("type", [valibot.object({
54
54
  * @example
55
55
  * ```ts
56
56
  * import * as v from "valibot";
57
- * import { FileArraySchema } from "@nakanoaas/notion-valibot-utils";
57
+ * import { FileSchema } from "@nakanoaas/notion-valibot-utils";
58
58
  *
59
59
  * const PageSchema = v.object({
60
60
  * id: v.string(),
61
61
  * properties: v.object({
62
- * Files: FileArraySchema,
62
+ * Files: FileSchema,
63
63
  * }),
64
64
  * });
65
65
  *
@@ -68,7 +68,7 @@ const InnerFileSchema = valibot.variant("type", [valibot.object({
68
68
  * // parsed.properties.Files: string[]
69
69
  * ```
70
70
  */
71
- const FileArraySchema = valibot.pipe(valibot.object({ files: valibot.array(InnerFileSchema) }), valibot.transform((v) => v.files.map((v) => v.type === "file" ? v.file.url : v.external.url)));
71
+ const FileSchema = valibot.pipe(valibot.object({ files: valibot.array(InnerFileSchema) }), valibot.transform((v) => v.files.map((v) => v.type === "file" ? v.file.url : v.external.url)));
72
72
  /**
73
73
  * Schema to extract the `files` property from a Notion page property and transform it to a single URL.
74
74
  *
@@ -157,8 +157,8 @@ const NullableSingleFileSchema = valibot.pipe(valibot.object({ files: valibot.ar
157
157
  return file.type === "file" ? file.file.url : file.external.url;
158
158
  }));
159
159
  //#endregion
160
- exports.FileArraySchema = FileArraySchema;
160
+ exports.FileSchema = FileSchema;
161
161
  exports.NullableSingleFileSchema = NullableSingleFileSchema;
162
162
  exports.SingleFileSchema = SingleFileSchema;
163
163
 
164
- //# sourceMappingURL=file.cjs.map
164
+ //# sourceMappingURL=files.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"files.cjs","names":["v"],"sources":["../src/files.ts"],"sourcesContent":["import * as v from \"valibot\";\n\n/**\n * Schema for a Notion file object structure.\n *\n * **Input:**\n * ```\n * {\n * type: \"file\";\n * file: {\n * url: string;\n * };\n * } | {\n * type: \"external\";\n * external: {\n * url: string;\n * };\n * }\n * ```\n *\n * @internal\n */\nconst InnerFileSchema = v.variant(\"type\", [\n\tv.object({\n\t\ttype: v.literal(\"file\"),\n\t\tfile: v.object({\n\t\t\turl: v.string(),\n\t\t}),\n\t}),\n\tv.object({\n\t\ttype: v.literal(\"external\"),\n\t\texternal: v.object({\n\t\t\turl: v.string(),\n\t\t}),\n\t}),\n]);\n\n/**\n * Schema to extract the `files` property from a Notion page property and transform it to an array of URLs.\n *\n * **Input:**\n * ```\n * {\n * files: Array<{\n * type: \"file\";\n * file: {\n * url: string;\n * };\n * } | {\n * type: \"external\";\n * external: {\n * url: string;\n * };\n * }>;\n * }\n * ```\n *\n * **Output:** `string[]`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { FileSchema } from \"@nakanoaas/notion-valibot-utils\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * Files: FileSchema,\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.Files: string[]\n * ```\n */\nexport const FileSchema = v.pipe(\n\tv.object({\n\t\tfiles: v.array(InnerFileSchema),\n\t}),\n\tv.transform((v) =>\n\t\tv.files.map((v) => (v.type === \"file\" ? v.file.url : v.external.url)),\n\t),\n);\n\n/**\n * Schema to extract the `files` property from a Notion page property and transform it to a single URL.\n *\n * **Input:**\n * ```\n * {\n * files: [{\n * type: \"file\";\n * file: {\n * url: string;\n * };\n * } | {\n * type: \"external\";\n * external: {\n * url: string;\n * };\n * }];\n * }\n * ```\n *\n * **Output:** `string`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { SingleFileSchema } from \"@nakanoaas/notion-valibot-utils\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * Attachment: SingleFileSchema,\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.Attachment: string\n * ```\n */\nexport const SingleFileSchema = v.pipe(\n\tv.object({\n\t\tfiles: v.tuple([InnerFileSchema]),\n\t}),\n\tv.transform((v) => {\n\t\tconst file = v.files[0];\n\t\treturn file.type === \"file\" ? file.file.url : file.external.url;\n\t}),\n);\n\n/**\n * Schema to extract the `files` property from a Notion page property and transform it to a single URL or `null`.\n *\n * **Input:**\n * ```\n * {\n * files: Array<{\n * type: \"file\";\n * file: {\n * url: string;\n * };\n * } | {\n * type: \"external\";\n * external: {\n * url: string;\n * };\n * }>;\n * }\n * ```\n *\n * **Output:** `string | null`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { NullableSingleFileSchema } from \"@nakanoaas/notion-valibot-utils\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * Attachment: NullableSingleFileSchema,\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.Attachment: string | null\n * ```\n */\nexport const NullableSingleFileSchema = v.pipe(\n\tv.object({\n\t\tfiles: v.array(InnerFileSchema),\n\t}),\n\tv.transform((v) => {\n\t\tconst file = v.files[0];\n\t\tif (!file) {\n\t\t\treturn null;\n\t\t}\n\t\treturn file.type === \"file\" ? file.file.url : file.external.url;\n\t}),\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAsBA,MAAM,kBAAkBA,QAAE,QAAQ,QAAQ,CACzCA,QAAE,OAAO;CACR,MAAMA,QAAE,QAAQ,MAAM;CACtB,MAAMA,QAAE,OAAO,EACd,KAAKA,QAAE,OAAO,EACf,CAAC;AACF,CAAC,GACDA,QAAE,OAAO;CACR,MAAMA,QAAE,QAAQ,UAAU;CAC1B,UAAUA,QAAE,OAAO,EAClB,KAAKA,QAAE,OAAO,EACf,CAAC;AACF,CAAC,CACF,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCD,MAAa,aAAaA,QAAE,KAC3BA,QAAE,OAAO,EACR,OAAOA,QAAE,MAAM,eAAe,EAC/B,CAAC,GACDA,QAAE,WAAW,MACZ,EAAE,MAAM,KAAK,MAAO,EAAE,SAAS,SAAS,EAAE,KAAK,MAAM,EAAE,SAAS,GAAI,CACrE,CACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,MAAa,mBAAmBA,QAAE,KACjCA,QAAE,OAAO,EACR,OAAOA,QAAE,MAAM,CAAC,eAAe,CAAC,EACjC,CAAC,GACDA,QAAE,WAAW,MAAM;CAClB,MAAM,OAAO,EAAE,MAAM;CACrB,OAAO,KAAK,SAAS,SAAS,KAAK,KAAK,MAAM,KAAK,SAAS;AAC7D,CAAC,CACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,MAAa,2BAA2BA,QAAE,KACzCA,QAAE,OAAO,EACR,OAAOA,QAAE,MAAM,eAAe,EAC/B,CAAC,GACDA,QAAE,WAAW,MAAM;CAClB,MAAM,OAAO,EAAE,MAAM;CACrB,IAAI,CAAC,MACJ,OAAO;CAER,OAAO,KAAK,SAAS,SAAS,KAAK,KAAK,MAAM,KAAK,SAAS;AAC7D,CAAC,CACF"}
@@ -1,6 +1,6 @@
1
1
  import * as v from "valibot";
2
2
 
3
- //#region src/file.d.ts
3
+ //#region src/files.d.ts
4
4
  /**
5
5
  * Schema to extract the `files` property from a Notion page property and transform it to an array of URLs.
6
6
  *
@@ -26,12 +26,12 @@ import * as v from "valibot";
26
26
  * @example
27
27
  * ```ts
28
28
  * import * as v from "valibot";
29
- * import { FileArraySchema } from "@nakanoaas/notion-valibot-utils";
29
+ * import { FileSchema } from "@nakanoaas/notion-valibot-utils";
30
30
  *
31
31
  * const PageSchema = v.object({
32
32
  * id: v.string(),
33
33
  * properties: v.object({
34
- * Files: FileArraySchema,
34
+ * Files: FileSchema,
35
35
  * }),
36
36
  * });
37
37
  *
@@ -40,7 +40,7 @@ import * as v from "valibot";
40
40
  * // parsed.properties.Files: string[]
41
41
  * ```
42
42
  */
43
- declare const FileArraySchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
43
+ declare const FileSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
44
44
  readonly files: v.ArraySchema<v.VariantSchema<"type", [v.ObjectSchema<{
45
45
  readonly type: v.LiteralSchema<"file", undefined>;
46
46
  readonly file: v.ObjectSchema<{
@@ -194,5 +194,5 @@ declare const NullableSingleFileSchema: v.SchemaWithPipe<readonly [v.ObjectSchem
194
194
  })[];
195
195
  }, string | null>]>;
196
196
  //#endregion
197
- export { FileArraySchema, NullableSingleFileSchema, SingleFileSchema };
198
- //# sourceMappingURL=file.d.cts.map
197
+ export { FileSchema, NullableSingleFileSchema, SingleFileSchema };
198
+ //# sourceMappingURL=files.d.cts.map
@@ -1,6 +1,6 @@
1
1
  import * as v from "valibot";
2
2
 
3
- //#region src/file.d.ts
3
+ //#region src/files.d.ts
4
4
  /**
5
5
  * Schema to extract the `files` property from a Notion page property and transform it to an array of URLs.
6
6
  *
@@ -26,12 +26,12 @@ import * as v from "valibot";
26
26
  * @example
27
27
  * ```ts
28
28
  * import * as v from "valibot";
29
- * import { FileArraySchema } from "@nakanoaas/notion-valibot-utils";
29
+ * import { FileSchema } from "@nakanoaas/notion-valibot-utils";
30
30
  *
31
31
  * const PageSchema = v.object({
32
32
  * id: v.string(),
33
33
  * properties: v.object({
34
- * Files: FileArraySchema,
34
+ * Files: FileSchema,
35
35
  * }),
36
36
  * });
37
37
  *
@@ -40,7 +40,7 @@ import * as v from "valibot";
40
40
  * // parsed.properties.Files: string[]
41
41
  * ```
42
42
  */
43
- declare const FileArraySchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
43
+ declare const FileSchema: v.SchemaWithPipe<readonly [v.ObjectSchema<{
44
44
  readonly files: v.ArraySchema<v.VariantSchema<"type", [v.ObjectSchema<{
45
45
  readonly type: v.LiteralSchema<"file", undefined>;
46
46
  readonly file: v.ObjectSchema<{
@@ -194,5 +194,5 @@ declare const NullableSingleFileSchema: v.SchemaWithPipe<readonly [v.ObjectSchem
194
194
  })[];
195
195
  }, string | null>]>;
196
196
  //#endregion
197
- export { FileArraySchema, NullableSingleFileSchema, SingleFileSchema };
198
- //# sourceMappingURL=file.d.mts.map
197
+ export { FileSchema, NullableSingleFileSchema, SingleFileSchema };
198
+ //# sourceMappingURL=files.d.mts.map
@@ -1,5 +1,5 @@
1
1
  import * as v from "valibot";
2
- //#region src/file.ts
2
+ //#region src/files.ts
3
3
  /**
4
4
  * Schema for a Notion file object structure.
5
5
  *
@@ -52,12 +52,12 @@ const InnerFileSchema = v.variant("type", [v.object({
52
52
  * @example
53
53
  * ```ts
54
54
  * import * as v from "valibot";
55
- * import { FileArraySchema } from "@nakanoaas/notion-valibot-utils";
55
+ * import { FileSchema } from "@nakanoaas/notion-valibot-utils";
56
56
  *
57
57
  * const PageSchema = v.object({
58
58
  * id: v.string(),
59
59
  * properties: v.object({
60
- * Files: FileArraySchema,
60
+ * Files: FileSchema,
61
61
  * }),
62
62
  * });
63
63
  *
@@ -66,7 +66,7 @@ const InnerFileSchema = v.variant("type", [v.object({
66
66
  * // parsed.properties.Files: string[]
67
67
  * ```
68
68
  */
69
- const FileArraySchema = v.pipe(v.object({ files: v.array(InnerFileSchema) }), v.transform((v) => v.files.map((v) => v.type === "file" ? v.file.url : v.external.url)));
69
+ const FileSchema = v.pipe(v.object({ files: v.array(InnerFileSchema) }), v.transform((v) => v.files.map((v) => v.type === "file" ? v.file.url : v.external.url)));
70
70
  /**
71
71
  * Schema to extract the `files` property from a Notion page property and transform it to a single URL.
72
72
  *
@@ -155,6 +155,6 @@ const NullableSingleFileSchema = v.pipe(v.object({ files: v.array(InnerFileSchem
155
155
  return file.type === "file" ? file.file.url : file.external.url;
156
156
  }));
157
157
  //#endregion
158
- export { FileArraySchema, NullableSingleFileSchema, SingleFileSchema };
158
+ export { FileSchema, NullableSingleFileSchema, SingleFileSchema };
159
159
 
160
- //# sourceMappingURL=file.mjs.map
160
+ //# sourceMappingURL=files.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"files.mjs","names":[],"sources":["../src/files.ts"],"sourcesContent":["import * as v from \"valibot\";\n\n/**\n * Schema for a Notion file object structure.\n *\n * **Input:**\n * ```\n * {\n * type: \"file\";\n * file: {\n * url: string;\n * };\n * } | {\n * type: \"external\";\n * external: {\n * url: string;\n * };\n * }\n * ```\n *\n * @internal\n */\nconst InnerFileSchema = v.variant(\"type\", [\n\tv.object({\n\t\ttype: v.literal(\"file\"),\n\t\tfile: v.object({\n\t\t\turl: v.string(),\n\t\t}),\n\t}),\n\tv.object({\n\t\ttype: v.literal(\"external\"),\n\t\texternal: v.object({\n\t\t\turl: v.string(),\n\t\t}),\n\t}),\n]);\n\n/**\n * Schema to extract the `files` property from a Notion page property and transform it to an array of URLs.\n *\n * **Input:**\n * ```\n * {\n * files: Array<{\n * type: \"file\";\n * file: {\n * url: string;\n * };\n * } | {\n * type: \"external\";\n * external: {\n * url: string;\n * };\n * }>;\n * }\n * ```\n *\n * **Output:** `string[]`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { FileSchema } from \"@nakanoaas/notion-valibot-utils\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * Files: FileSchema,\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.Files: string[]\n * ```\n */\nexport const FileSchema = v.pipe(\n\tv.object({\n\t\tfiles: v.array(InnerFileSchema),\n\t}),\n\tv.transform((v) =>\n\t\tv.files.map((v) => (v.type === \"file\" ? v.file.url : v.external.url)),\n\t),\n);\n\n/**\n * Schema to extract the `files` property from a Notion page property and transform it to a single URL.\n *\n * **Input:**\n * ```\n * {\n * files: [{\n * type: \"file\";\n * file: {\n * url: string;\n * };\n * } | {\n * type: \"external\";\n * external: {\n * url: string;\n * };\n * }];\n * }\n * ```\n *\n * **Output:** `string`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { SingleFileSchema } from \"@nakanoaas/notion-valibot-utils\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * Attachment: SingleFileSchema,\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.Attachment: string\n * ```\n */\nexport const SingleFileSchema = v.pipe(\n\tv.object({\n\t\tfiles: v.tuple([InnerFileSchema]),\n\t}),\n\tv.transform((v) => {\n\t\tconst file = v.files[0];\n\t\treturn file.type === \"file\" ? file.file.url : file.external.url;\n\t}),\n);\n\n/**\n * Schema to extract the `files` property from a Notion page property and transform it to a single URL or `null`.\n *\n * **Input:**\n * ```\n * {\n * files: Array<{\n * type: \"file\";\n * file: {\n * url: string;\n * };\n * } | {\n * type: \"external\";\n * external: {\n * url: string;\n * };\n * }>;\n * }\n * ```\n *\n * **Output:** `string | null`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { NullableSingleFileSchema } from \"@nakanoaas/notion-valibot-utils\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * Attachment: NullableSingleFileSchema,\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.Attachment: string | null\n * ```\n */\nexport const NullableSingleFileSchema = v.pipe(\n\tv.object({\n\t\tfiles: v.array(InnerFileSchema),\n\t}),\n\tv.transform((v) => {\n\t\tconst file = v.files[0];\n\t\tif (!file) {\n\t\t\treturn null;\n\t\t}\n\t\treturn file.type === \"file\" ? file.file.url : file.external.url;\n\t}),\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAsBA,MAAM,kBAAkB,EAAE,QAAQ,QAAQ,CACzC,EAAE,OAAO;CACR,MAAM,EAAE,QAAQ,MAAM;CACtB,MAAM,EAAE,OAAO,EACd,KAAK,EAAE,OAAO,EACf,CAAC;AACF,CAAC,GACD,EAAE,OAAO;CACR,MAAM,EAAE,QAAQ,UAAU;CAC1B,UAAU,EAAE,OAAO,EAClB,KAAK,EAAE,OAAO,EACf,CAAC;AACF,CAAC,CACF,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCD,MAAa,aAAa,EAAE,KAC3B,EAAE,OAAO,EACR,OAAO,EAAE,MAAM,eAAe,EAC/B,CAAC,GACD,EAAE,WAAW,MACZ,EAAE,MAAM,KAAK,MAAO,EAAE,SAAS,SAAS,EAAE,KAAK,MAAM,EAAE,SAAS,GAAI,CACrE,CACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,MAAa,mBAAmB,EAAE,KACjC,EAAE,OAAO,EACR,OAAO,EAAE,MAAM,CAAC,eAAe,CAAC,EACjC,CAAC,GACD,EAAE,WAAW,MAAM;CAClB,MAAM,OAAO,EAAE,MAAM;CACrB,OAAO,KAAK,SAAS,SAAS,KAAK,KAAK,MAAM,KAAK,SAAS;AAC7D,CAAC,CACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,MAAa,2BAA2B,EAAE,KACzC,EAAE,OAAO,EACR,OAAO,EAAE,MAAM,eAAe,EAC/B,CAAC,GACD,EAAE,WAAW,MAAM;CAClB,MAAM,OAAO,EAAE,MAAM;CACrB,IAAI,CAAC,MACJ,OAAO;CAER,OAAO,KAAK,SAAS,SAAS,KAAK,KAAK,MAAM,KAAK,SAAS;AAC7D,CAAC,CACF"}
package/dist/formula.cjs CHANGED
@@ -6,10 +6,8 @@ valibot = require_runtime.__toESM(valibot, 1);
6
6
  * Schema factory to extract the `formula` property result from a Notion page property.
7
7
  *
8
8
  * This is a generic schema factory that accepts another schema as a parameter,
9
- * allowing you to combine it with other schemas in this library to extract
10
- * typed formula results. The formula property in Notion can return different
11
- * types of values (string, number, boolean, or date) depending on the formula
12
- * configuration.
9
+ * allowing you to combine it with the formula, number, and date schemas in this library
10
+ * to extract typed formula results (e.g. string, number, boolean, or date formulas).
13
11
  *
14
12
  * **Input:**
15
13
  * ```
@@ -29,55 +27,158 @@ valibot = require_runtime.__toESM(valibot, 1);
29
27
  * ```
30
28
  *
31
29
  * **Output:** The output type depends on the schema passed as a parameter.
32
- * For example, if `RichTextSchema` is passed, the output will be `string`.
30
+ * For example, if `StringFormulaSchema` is passed, the output will be `string`.
33
31
  *
34
- * @param schema - A schema that validates the formula result type.
35
- * Must accept one of: string, number, boolean, or date formula results.
32
+ * @param schema - A schema that validates the formula result object.
33
+ * Use the pre-built `*FormulaSchema` helpers for string and boolean,
34
+ * `NumberSchema` / `NullableNumberSchema` for number,
35
+ * or the date schemas from this library for date formulas.
36
36
  *
37
37
  * @example
38
38
  * ```ts
39
39
  * import * as v from "valibot";
40
- * import { FormulaSchema, RichTextSchema } from "@nakanoaas/notion-valibot-utils";
40
+ * import {
41
+ * BooleanFormulaSchema,
42
+ * FormulaSchema,
43
+ * NumberSchema,
44
+ * SingleDateSchema,
45
+ * StringFormulaSchema,
46
+ * } from "@nakanoaas/notion-valibot-schema";
41
47
  *
42
48
  * const PageSchema = v.object({
43
49
  * id: v.string(),
44
50
  * properties: v.object({
45
- * FormulaText: FormulaSchema(RichTextSchema),
51
+ * FormulaText: FormulaSchema(StringFormulaSchema),
52
+ * FormulaNumber: FormulaSchema(NumberSchema),
53
+ * FormulaBoolean: FormulaSchema(BooleanFormulaSchema),
54
+ * FormulaDate: FormulaSchema(SingleDateSchema),
46
55
  * }),
47
56
  * });
48
57
  *
49
58
  * const page = await notion.pages.retrieve({ page_id: "..." });
50
59
  * const parsed = v.parse(PageSchema, page);
51
60
  * // parsed.properties.FormulaText: string
61
+ * // parsed.properties.FormulaNumber: number
62
+ * // parsed.properties.FormulaBoolean: boolean
63
+ * // parsed.properties.FormulaDate: Date
52
64
  * ```
65
+ */
66
+ function FormulaSchema(schema) {
67
+ return valibot.pipe(valibot.object({ formula: schema }), valibot.transform((v) => v.formula));
68
+ }
69
+ /**
70
+ * Pre-built inner schema for a non-nullable string formula result.
71
+ *
72
+ * Validates `{ string: string }` and extracts the `string` value.
73
+ * Use with {@link FormulaSchema} when the formula is guaranteed to return a string.
74
+ *
75
+ * **Input:** `{ string: string }`
76
+ * **Output:** `string`
53
77
  *
54
78
  * @example
55
79
  * ```ts
56
80
  * import * as v from "valibot";
57
- * import { FormulaSchema } from "@nakanoaas/notion-valibot-utils";
81
+ * import { FormulaSchema, StringFormulaSchema } from "@nakanoaas/notion-valibot-schema";
58
82
  *
59
- * // Custom schema for number formula
60
- * const NumberFormulaSchema = v.object({
61
- * type: v.literal("number"),
62
- * number: v.nullable(v.number()),
83
+ * const PageSchema = v.object({
84
+ * id: v.string(),
85
+ * properties: v.object({
86
+ * MyFormula: FormulaSchema(StringFormulaSchema),
87
+ * }),
63
88
  * });
64
89
  *
90
+ * const page = await notion.pages.retrieve({ page_id: "..." });
91
+ * const parsed = v.parse(PageSchema, page);
92
+ * // parsed.properties.MyFormula: string
93
+ * ```
94
+ */
95
+ const StringFormulaSchema = valibot.pipe(valibot.object({ string: valibot.string() }), valibot.transform((v) => v.string));
96
+ /**
97
+ * Pre-built inner schema for a nullable string formula result.
98
+ *
99
+ * Validates `{ string: string | null }` and extracts the `string` value.
100
+ * Use with {@link FormulaSchema} when the string formula result may be null.
101
+ *
102
+ * **Input:** `{ string: string | null }`
103
+ * **Output:** `string | null`
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * import * as v from "valibot";
108
+ * import { FormulaSchema, NullableStringFormulaSchema } from "@nakanoaas/notion-valibot-schema";
109
+ *
65
110
  * const PageSchema = v.object({
66
111
  * id: v.string(),
67
112
  * properties: v.object({
68
- * FormulaNumber: FormulaSchema(NumberFormulaSchema),
113
+ * MyFormula: FormulaSchema(NullableStringFormulaSchema),
69
114
  * }),
70
115
  * });
71
116
  *
72
117
  * const page = await notion.pages.retrieve({ page_id: "..." });
73
118
  * const parsed = v.parse(PageSchema, page);
74
- * // parsed.properties.FormulaNumber: { type: "number"; number: number | null }
119
+ * // parsed.properties.MyFormula: string | null
75
120
  * ```
76
121
  */
77
- function FormulaSchema(schema) {
78
- return valibot.pipe(valibot.object({ formula: schema }), valibot.transform((v) => v.formula));
79
- }
122
+ const NullableStringFormulaSchema = valibot.pipe(valibot.object({ string: valibot.nullable(valibot.string()) }), valibot.transform((v) => v.string));
123
+ /**
124
+ * Pre-built inner schema for a non-nullable boolean formula result.
125
+ *
126
+ * Validates `{ boolean: boolean }` and extracts the `boolean` value.
127
+ * Use with {@link FormulaSchema} when the formula is guaranteed to return a boolean.
128
+ *
129
+ * **Input:** `{ boolean: boolean }`
130
+ * **Output:** `boolean`
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * import * as v from "valibot";
135
+ * import { FormulaSchema, BooleanFormulaSchema } from "@nakanoaas/notion-valibot-schema";
136
+ *
137
+ * const PageSchema = v.object({
138
+ * id: v.string(),
139
+ * properties: v.object({
140
+ * MyFormula: FormulaSchema(BooleanFormulaSchema),
141
+ * }),
142
+ * });
143
+ *
144
+ * const page = await notion.pages.retrieve({ page_id: "..." });
145
+ * const parsed = v.parse(PageSchema, page);
146
+ * // parsed.properties.MyFormula: boolean
147
+ * ```
148
+ */
149
+ const BooleanFormulaSchema = valibot.pipe(valibot.object({ boolean: valibot.boolean() }), valibot.transform((v) => v.boolean));
150
+ /**
151
+ * Pre-built inner schema for a nullable boolean formula result.
152
+ *
153
+ * Validates `{ boolean: boolean | null }` and extracts the `boolean` value.
154
+ * Use with {@link FormulaSchema} when the boolean formula result may be null.
155
+ *
156
+ * **Input:** `{ boolean: boolean | null }`
157
+ * **Output:** `boolean | null`
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * import * as v from "valibot";
162
+ * import { FormulaSchema, NullableBooleanFormulaSchema } from "@nakanoaas/notion-valibot-schema";
163
+ *
164
+ * const PageSchema = v.object({
165
+ * id: v.string(),
166
+ * properties: v.object({
167
+ * MyFormula: FormulaSchema(NullableBooleanFormulaSchema),
168
+ * }),
169
+ * });
170
+ *
171
+ * const page = await notion.pages.retrieve({ page_id: "..." });
172
+ * const parsed = v.parse(PageSchema, page);
173
+ * // parsed.properties.MyFormula: boolean | null
174
+ * ```
175
+ */
176
+ const NullableBooleanFormulaSchema = valibot.pipe(valibot.object({ boolean: valibot.nullable(valibot.boolean()) }), valibot.transform((v) => v.boolean));
80
177
  //#endregion
178
+ exports.BooleanFormulaSchema = BooleanFormulaSchema;
81
179
  exports.FormulaSchema = FormulaSchema;
180
+ exports.NullableBooleanFormulaSchema = NullableBooleanFormulaSchema;
181
+ exports.NullableStringFormulaSchema = NullableStringFormulaSchema;
182
+ exports.StringFormulaSchema = StringFormulaSchema;
82
183
 
83
184
  //# sourceMappingURL=formula.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"formula.cjs","names":["v"],"sources":["../src/formula.ts"],"sourcesContent":["import * as v from \"valibot\";\n\n/**\n * Schema factory to extract the `formula` property result from a Notion page property.\n *\n * This is a generic schema factory that accepts another schema as a parameter,\n * allowing you to combine it with other schemas in this library to extract\n * typed formula results. The formula property in Notion can return different\n * types of values (string, number, boolean, or date) depending on the formula\n * configuration.\n *\n * **Input:**\n * ```\n * {\n * formula: {\n * type: \"string\" | \"number\" | \"boolean\" | \"date\";\n * string?: string | null;\n * number?: number | null;\n * boolean?: boolean | null;\n * date?: {\n * start: string;\n * end: string | null;\n * time_zone: string | null;\n * } | null;\n * }\n * }\n * ```\n *\n * **Output:** The output type depends on the schema passed as a parameter.\n * For example, if `RichTextSchema` is passed, the output will be `string`.\n *\n * @param schema - A schema that validates the formula result type.\n * Must accept one of: string, number, boolean, or date formula results.\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { FormulaSchema, RichTextSchema } from \"@nakanoaas/notion-valibot-utils\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * FormulaText: FormulaSchema(RichTextSchema),\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.FormulaText: string\n * ```\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { FormulaSchema } from \"@nakanoaas/notion-valibot-utils\";\n *\n * // Custom schema for number formula\n * const NumberFormulaSchema = v.object({\n * type: v.literal(\"number\"),\n * number: v.nullable(v.number()),\n * });\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * FormulaNumber: FormulaSchema(NumberFormulaSchema),\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.FormulaNumber: { type: \"number\"; number: number | null }\n * ```\n */\nexport function FormulaSchema<\n\tS extends v.GenericSchema<\n\t\t| { type: \"string\"; string: string | null }\n\t\t| { type: \"number\"; number: number | null }\n\t\t| { type: \"boolean\"; boolean: boolean | null }\n\t\t| {\n\t\t\t\ttype: \"date\";\n\t\t\t\tdate: {\n\t\t\t\t\tstart: string;\n\t\t\t\t\tend: string | null;\n\t\t\t\t\ttime_zone: string | null;\n\t\t\t\t} | null;\n\t\t },\n\t\tunknown\n\t>,\n>(schema: S) {\n\treturn v.pipe(\n\t\tv.object({\n\t\t\tformula: schema,\n\t\t}),\n\t\t// biome-ignore lint/style/noNonNullAssertion: valibot inference is not working correctly\n\t\tv.transform((v) => v.formula!),\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0EA,SAAgB,cAed,QAAW;CACZ,OAAOA,QAAE,KACRA,QAAE,OAAO,EACR,SAAS,OACV,CAAC,GAEDA,QAAE,WAAW,MAAM,EAAE,OAAQ,CAC9B;AACD"}
1
+ {"version":3,"file":"formula.cjs","names":["v"],"sources":["../src/formula.ts"],"sourcesContent":["import * as v from \"valibot\";\n\n/**\n * Schema factory to extract the `formula` property result from a Notion page property.\n *\n * This is a generic schema factory that accepts another schema as a parameter,\n * allowing you to combine it with the formula, number, and date schemas in this library\n * to extract typed formula results (e.g. string, number, boolean, or date formulas).\n *\n * **Input:**\n * ```\n * {\n * formula: {\n * type: \"string\" | \"number\" | \"boolean\" | \"date\";\n * string?: string | null;\n * number?: number | null;\n * boolean?: boolean | null;\n * date?: {\n * start: string;\n * end: string | null;\n * time_zone: string | null;\n * } | null;\n * }\n * }\n * ```\n *\n * **Output:** The output type depends on the schema passed as a parameter.\n * For example, if `StringFormulaSchema` is passed, the output will be `string`.\n *\n * @param schema - A schema that validates the formula result object.\n * Use the pre-built `*FormulaSchema` helpers for string and boolean,\n * `NumberSchema` / `NullableNumberSchema` for number,\n * or the date schemas from this library for date formulas.\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import {\n * BooleanFormulaSchema,\n * FormulaSchema,\n * NumberSchema,\n * SingleDateSchema,\n * StringFormulaSchema,\n * } from \"@nakanoaas/notion-valibot-schema\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * FormulaText: FormulaSchema(StringFormulaSchema),\n * FormulaNumber: FormulaSchema(NumberSchema),\n * FormulaBoolean: FormulaSchema(BooleanFormulaSchema),\n * FormulaDate: FormulaSchema(SingleDateSchema),\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.FormulaText: string\n * // parsed.properties.FormulaNumber: number\n * // parsed.properties.FormulaBoolean: boolean\n * // parsed.properties.FormulaDate: Date\n * ```\n */\nexport function FormulaSchema<\n\tS extends v.GenericSchema<\n\t\t| { boolean: boolean | null }\n\t\t| {\n\t\t\t\tdate: {\n\t\t\t\t\tstart: string;\n\t\t\t\t\tend: string | null;\n\t\t\t\t\ttime_zone: string | null;\n\t\t\t\t} | null;\n\t\t }\n\t\t| { number: number | null }\n\t\t| { string: string | null },\n\t\tunknown\n\t>,\n>(schema: S) {\n\treturn v.pipe(\n\t\tv.object({\n\t\t\tformula: schema,\n\t\t}),\n\t\t// biome-ignore lint/style/noNonNullAssertion: valibot inference is not working correctly\n\t\tv.transform((v) => v.formula!),\n\t);\n}\n\n/**\n * Pre-built inner schema for a non-nullable string formula result.\n *\n * Validates `{ string: string }` and extracts the `string` value.\n * Use with {@link FormulaSchema} when the formula is guaranteed to return a string.\n *\n * **Input:** `{ string: string }`\n * **Output:** `string`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { FormulaSchema, StringFormulaSchema } from \"@nakanoaas/notion-valibot-schema\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * MyFormula: FormulaSchema(StringFormulaSchema),\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.MyFormula: string\n * ```\n */\nexport const StringFormulaSchema = v.pipe(\n\tv.object({\n\t\tstring: v.string(),\n\t}),\n\tv.transform((v) => v.string),\n);\n\n/**\n * Pre-built inner schema for a nullable string formula result.\n *\n * Validates `{ string: string | null }` and extracts the `string` value.\n * Use with {@link FormulaSchema} when the string formula result may be null.\n *\n * **Input:** `{ string: string | null }`\n * **Output:** `string | null`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { FormulaSchema, NullableStringFormulaSchema } from \"@nakanoaas/notion-valibot-schema\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * MyFormula: FormulaSchema(NullableStringFormulaSchema),\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.MyFormula: string | null\n * ```\n */\nexport const NullableStringFormulaSchema = v.pipe(\n\tv.object({\n\t\tstring: v.nullable(v.string()),\n\t}),\n\tv.transform((v) => v.string),\n);\n\n/**\n * Pre-built inner schema for a non-nullable boolean formula result.\n *\n * Validates `{ boolean: boolean }` and extracts the `boolean` value.\n * Use with {@link FormulaSchema} when the formula is guaranteed to return a boolean.\n *\n * **Input:** `{ boolean: boolean }`\n * **Output:** `boolean`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { FormulaSchema, BooleanFormulaSchema } from \"@nakanoaas/notion-valibot-schema\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * MyFormula: FormulaSchema(BooleanFormulaSchema),\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.MyFormula: boolean\n * ```\n */\nexport const BooleanFormulaSchema = v.pipe(\n\tv.object({\n\t\tboolean: v.boolean(),\n\t}),\n\tv.transform((v) => v.boolean),\n);\n\n/**\n * Pre-built inner schema for a nullable boolean formula result.\n *\n * Validates `{ boolean: boolean | null }` and extracts the `boolean` value.\n * Use with {@link FormulaSchema} when the boolean formula result may be null.\n *\n * **Input:** `{ boolean: boolean | null }`\n * **Output:** `boolean | null`\n *\n * @example\n * ```ts\n * import * as v from \"valibot\";\n * import { FormulaSchema, NullableBooleanFormulaSchema } from \"@nakanoaas/notion-valibot-schema\";\n *\n * const PageSchema = v.object({\n * id: v.string(),\n * properties: v.object({\n * MyFormula: FormulaSchema(NullableBooleanFormulaSchema),\n * }),\n * });\n *\n * const page = await notion.pages.retrieve({ page_id: \"...\" });\n * const parsed = v.parse(PageSchema, page);\n * // parsed.properties.MyFormula: boolean | null\n * ```\n */\nexport const NullableBooleanFormulaSchema = v.pipe(\n\tv.object({\n\t\tboolean: v.nullable(v.boolean()),\n\t}),\n\tv.transform((v) => v.boolean),\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAgB,cAcd,QAAW;CACZ,OAAOA,QAAE,KACRA,QAAE,OAAO,EACR,SAAS,OACV,CAAC,GAEDA,QAAE,WAAW,MAAM,EAAE,OAAQ,CAC9B;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,MAAa,sBAAsBA,QAAE,KACpCA,QAAE,OAAO,EACR,QAAQA,QAAE,OAAO,EAClB,CAAC,GACDA,QAAE,WAAW,MAAM,EAAE,MAAM,CAC5B;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,MAAa,8BAA8BA,QAAE,KAC5CA,QAAE,OAAO,EACR,QAAQA,QAAE,SAASA,QAAE,OAAO,CAAC,EAC9B,CAAC,GACDA,QAAE,WAAW,MAAM,EAAE,MAAM,CAC5B;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,MAAa,uBAAuBA,QAAE,KACrCA,QAAE,OAAO,EACR,SAASA,QAAE,QAAQ,EACpB,CAAC,GACDA,QAAE,WAAW,MAAM,EAAE,OAAO,CAC7B;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BA,MAAa,+BAA+BA,QAAE,KAC7CA,QAAE,OAAO,EACR,SAASA,QAAE,SAASA,QAAE,QAAQ,CAAC,EAChC,CAAC,GACDA,QAAE,WAAW,MAAM,EAAE,OAAO,CAC7B"}