groq-builder
Version:
A **schema-aware**, strongly-typed GROQ query builder. It enables you to build GROQ queries using **auto-completion**, **type-checking**, and **runtime validation**.
59 lines • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.zodMethods = void 0;
const zod_1 = require("zod");
const utils_1 = require("../types/utils");
const zodPrimitives = (0, utils_1.pick)(zod_1.z, [
"string",
"number",
"boolean",
"null",
"date",
"literal",
"union",
"array",
"object",
]);
const zodExtras = {
/**
* Zod's `.default()` method doesn't work well with GROQ,
* since GROQ only returns `null` and never `undefined`.
*
* So instead of chaining Zod's `default` method,
* use this `default` method instead.
*
* @example
* // Before:
* q.number().default(0)
* // After:
* q.default(q.number(), 0)
*/
default(schema, defaultValue) {
return (input) => {
if (input === null)
return defaultValue;
return schema.parse(input);
};
},
/**
* Shorthand for accessing the current value for a slug.
*
* @example
* // Before:
* q.star.filterByType("product").project({
* slug: ["slug.current", z.string()],
* })
* // After:
* q.star.filterByType("product").project({
* slug: q.slug("slug"),
* })
*/
slug(fieldName) {
return [`${fieldName}.current`, zod_1.z.string()];
},
};
exports.zodMethods = {
...zodPrimitives,
...zodExtras,
};
//# sourceMappingURL=zod.js.map