@cancia/astro 0.0.1 → 0.1.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.
@@ -1,73 +0,0 @@
1
- // src/schema/index.ts
2
- import { z } from "zod";
3
- function defineList(opts) {
4
- return {
5
- label: opts.label,
6
- labelSingular: opts.labelSingular ?? opts.label.replace(/s$/, ""),
7
- titleField: opts.titleField,
8
- bodyField: opts.bodyField,
9
- slugField: opts.slugField,
10
- fields: opts.fields,
11
- validator: z.object(opts.fields)
12
- };
13
- }
14
- function humanise(name) {
15
- return name.replace(/([A-Z])/g, " $1").replace(/[_-]/g, " ").replace(/^\w/, (c) => c.toUpperCase()).trim();
16
- }
17
- function inferWidget(prop, label) {
18
- if (prop.widget) return prop.widget;
19
- if (prop.enum) return "select";
20
- if (prop.type === "boolean") return "checkbox";
21
- if (prop.type === "number" || prop.type === "integer") return "number";
22
- if (prop.type === "string") {
23
- if (prop.format === "uri" || prop.format === "url") return "url";
24
- if (prop.format === "email") return "email";
25
- if (prop.format === "date-time" || prop.format === "date") return "datetime";
26
- if (label.toLowerCase() === "body" || label.toLowerCase() === "content") return "textarea";
27
- if (prop.maxLength && prop.maxLength > 200) return "textarea";
28
- return "text";
29
- }
30
- return "text";
31
- }
32
- function describeProperty(name, prop, required) {
33
- const label = prop.label ?? humanise(name);
34
- const desc = {
35
- name,
36
- label,
37
- widget: inferWidget(prop, label),
38
- required
39
- };
40
- if (prop.description) desc.description = prop.description;
41
- if (prop.placeholder) desc.placeholder = prop.placeholder;
42
- if (prop.minLength !== void 0) desc.minLength = prop.minLength;
43
- if (prop.maxLength !== void 0) desc.maxLength = prop.maxLength;
44
- if (prop.minimum !== void 0) desc.min = prop.minimum;
45
- if (prop.maximum !== void 0) desc.max = prop.maximum;
46
- if (prop.enum) desc.options = prop.enum;
47
- return desc;
48
- }
49
- function describeList(name, schema) {
50
- const json = z.toJSONSchema(schema.validator, {
51
- metadata: z.globalRegistry
52
- });
53
- const properties = json.properties ?? {};
54
- const requiredSet = new Set(json.required ?? []);
55
- const fields = Object.keys(schema.fields).map(
56
- (fieldName) => describeProperty(fieldName, properties[fieldName] ?? {}, requiredSet.has(fieldName))
57
- );
58
- return {
59
- name,
60
- label: schema.label,
61
- labelSingular: schema.labelSingular,
62
- titleField: schema.titleField,
63
- bodyField: schema.bodyField,
64
- slugField: schema.slugField,
65
- fields
66
- };
67
- }
68
-
69
- export {
70
- z,
71
- defineList,
72
- describeList
73
- };