@cogenta/blocks 1.0.4 → 1.1.3

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.
@@ -0,0 +1,45 @@
1
+ import { z } from 'zod';
2
+ import { type AnyBlockField, type BlockSchema } from './field.js';
3
+ /**
4
+ * A block schema written as **data** rather than as calls to `f.*`.
5
+ *
6
+ * This exists because a block can now come from somewhere that may not run
7
+ * code in this process: a plugin declares its blocks in
8
+ * `plugin.manifest.json`, read and never executed (the hole L31's security
9
+ * review closed). The declaration is validated where it arrives, and turned
10
+ * into a real schema here, by the same constructors every vocabulary block is
11
+ * built from — so a declared block is a first-class block, with the same
12
+ * validator, the same envelope and the same fallback chain, not a second
13
+ * class of thing the rest of the code has to know about.
14
+ *
15
+ * Deliberately structural: nothing here knows what a plugin is, and
16
+ * `@cogenta/blocks` gains no dependency on one.
17
+ */
18
+ /** The kinds a declaration may use: contract B's own, plus `list` for a repeating group. */
19
+ export declare const DECLARABLE_FIELD_KINDS: readonly ["text", "richText", "number", "boolean", "media", "relation", "select", "color", "json", "list"];
20
+ export type DeclarableFieldKind = (typeof DECLARABLE_FIELD_KINDS)[number];
21
+ export interface DeclaredBlockField {
22
+ readonly kind: string;
23
+ readonly required?: boolean;
24
+ readonly localized?: boolean;
25
+ readonly label?: string;
26
+ readonly help?: string;
27
+ readonly options?: Readonly<Record<string, unknown>>;
28
+ /** For `list` only: the fields one item holds. */
29
+ readonly of?: Readonly<Record<string, DeclaredBlockField>>;
30
+ }
31
+ export type DeclaredBlockSchema = Readonly<Record<string, DeclaredBlockField>>;
32
+ /** One declared field, as the real field a block schema holds. */
33
+ export declare function blockFieldFromDeclaration(declaration: DeclaredBlockField, path: string): AnyBlockField;
34
+ /** A whole declared schema, ready for `defineBlock`. */
35
+ export declare function blockSchemaFromDeclaration(fields: DeclaredBlockSchema | undefined, path: string): BlockSchema;
36
+ /**
37
+ * A declared field map as one strict object schema.
38
+ *
39
+ * Where `blockSchemaFromDeclaration` builds what `defineBlock` wants, this
40
+ * builds what anything else validating a plain settings object wants — a
41
+ * plugin's widget settings, in L32 step 4. Strict, so a stored setting the
42
+ * declaration never mentioned is refused rather than kept and rendered later.
43
+ */
44
+ export declare function declaredObjectSchema(fields: DeclaredBlockSchema | undefined, path: string): z.ZodType<Record<string, unknown>>;
45
+ //# sourceMappingURL=from-declaration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-declaration.d.ts","sourceRoot":"","sources":["../src/from-declaration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,KAAK,aAAa,EAAqB,KAAK,WAAW,EAAK,MAAM,YAAY,CAAA;AAEvF;;;;;;;;;;;;;;GAcG;AAEH,4FAA4F;AAC5F,eAAO,MAAM,sBAAsB,4GAA0C,CAAA;AAE7E,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,sBAAsB,CAAC,CAAC,MAAM,CAAC,CAAA;AAEzE,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAC3B,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAA;IAC5B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAA;IACpD,kDAAkD;IAClD,QAAQ,CAAC,EAAE,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAAA;CAC3D;AAED,MAAM,MAAM,mBAAmB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAAA;AAqB9E,kEAAkE;AAClE,wBAAgB,yBAAyB,CACvC,WAAW,EAAE,kBAAkB,EAC/B,IAAI,EAAE,MAAM,GACX,aAAa,CA8Cf;AAED,wDAAwD;AACxD,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,mBAAmB,GAAG,SAAS,EACvC,IAAI,EAAE,MAAM,GACX,WAAW,CAMb;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,mBAAmB,GAAG,SAAS,EACvC,IAAI,EAAE,MAAM,GACX,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAOpC"}
@@ -0,0 +1,110 @@
1
+ import { z } from 'zod';
2
+ import { invalidBlockDefinition } from './errors.js';
3
+ import { BLOCK_FIELD_KINDS, f } from './field.js';
4
+ /**
5
+ * A block schema written as **data** rather than as calls to `f.*`.
6
+ *
7
+ * This exists because a block can now come from somewhere that may not run
8
+ * code in this process: a plugin declares its blocks in
9
+ * `plugin.manifest.json`, read and never executed (the hole L31's security
10
+ * review closed). The declaration is validated where it arrives, and turned
11
+ * into a real schema here, by the same constructors every vocabulary block is
12
+ * built from — so a declared block is a first-class block, with the same
13
+ * validator, the same envelope and the same fallback chain, not a second
14
+ * class of thing the rest of the code has to know about.
15
+ *
16
+ * Deliberately structural: nothing here knows what a plugin is, and
17
+ * `@cogenta/blocks` gains no dependency on one.
18
+ */
19
+ /** The kinds a declaration may use: contract B's own, plus `list` for a repeating group. */
20
+ export const DECLARABLE_FIELD_KINDS = [...BLOCK_FIELD_KINDS, 'list'];
21
+ function refuse(path, message) {
22
+ throw invalidBlockDefinition(path, message);
23
+ }
24
+ function common(declaration) {
25
+ return {
26
+ ...(declaration.required === true ? { required: true } : {}),
27
+ ...(declaration.localized === true ? { localized: true } : {}),
28
+ ...(declaration.label === undefined && declaration.help === undefined
29
+ ? {}
30
+ : {
31
+ admin: {
32
+ ...(declaration.label === undefined ? {} : { label: declaration.label }),
33
+ ...(declaration.help === undefined ? {} : { help: declaration.help }),
34
+ },
35
+ }),
36
+ };
37
+ }
38
+ /** One declared field, as the real field a block schema holds. */
39
+ export function blockFieldFromDeclaration(declaration, path) {
40
+ const options = { ...(declaration.options ?? {}), ...common(declaration) };
41
+ switch (declaration.kind) {
42
+ case 'text':
43
+ return f.text(options);
44
+ case 'richText':
45
+ return f.richText(options);
46
+ case 'number':
47
+ return f.number(options);
48
+ case 'boolean':
49
+ return f.boolean(options);
50
+ case 'media':
51
+ return f.media(options);
52
+ case 'relation':
53
+ return f.relation(options);
54
+ case 'color':
55
+ return f.color(options);
56
+ case 'json':
57
+ return f.json(options);
58
+ case 'select': {
59
+ const choices = (declaration.options ?? {})['options'];
60
+ if (!Array.isArray(choices) || choices.some((choice) => typeof choice !== 'string')) {
61
+ refuse(path, 'a select field must list its options as strings');
62
+ }
63
+ return f.select(options);
64
+ }
65
+ case 'list': {
66
+ const of = declaration.of;
67
+ if (of === undefined || Object.keys(of).length === 0) {
68
+ refuse(path, 'a list must say what one of its items holds');
69
+ }
70
+ // Strict, exactly as the vocabulary's own gallery and FAQ items are: an
71
+ // unexpected key in a stored item is a mistake to report, not data to
72
+ // keep and render later.
73
+ const shape = { _key: z.string().min(1) };
74
+ for (const [name, item] of Object.entries(of)) {
75
+ if (item.kind === 'list')
76
+ refuse(`${path}.of.${name}`, 'a list cannot hold another list');
77
+ const built = blockFieldFromDeclaration(item, `${path}.of.${name}`);
78
+ shape[name] = item.required === true ? built.zod : built.zod.optional();
79
+ }
80
+ return f.list(z.strictObject(shape), options);
81
+ }
82
+ default:
83
+ return refuse(path, `"${declaration.kind}" is not a field kind a block may declare`);
84
+ }
85
+ }
86
+ /** A whole declared schema, ready for `defineBlock`. */
87
+ export function blockSchemaFromDeclaration(fields, path) {
88
+ const schema = {};
89
+ for (const [name, declaration] of Object.entries(fields ?? {})) {
90
+ schema[name] = blockFieldFromDeclaration(declaration, `${path}.${name}`);
91
+ }
92
+ return schema;
93
+ }
94
+ /**
95
+ * A declared field map as one strict object schema.
96
+ *
97
+ * Where `blockSchemaFromDeclaration` builds what `defineBlock` wants, this
98
+ * builds what anything else validating a plain settings object wants — a
99
+ * plugin's widget settings, in L32 step 4. Strict, so a stored setting the
100
+ * declaration never mentioned is refused rather than kept and rendered later.
101
+ */
102
+ export function declaredObjectSchema(fields, path) {
103
+ const shape = {};
104
+ for (const [name, declaration] of Object.entries(fields ?? {})) {
105
+ const built = blockFieldFromDeclaration(declaration, `${path}.${name}`);
106
+ shape[name] = declaration.required === true ? built.zod : built.zod.optional();
107
+ }
108
+ return z.strictObject(shape);
109
+ }
110
+ //# sourceMappingURL=from-declaration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"from-declaration.js","sourceRoot":"","sources":["../src/from-declaration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,EAAsB,iBAAiB,EAAoB,CAAC,EAAE,MAAM,YAAY,CAAA;AAEvF;;;;;;;;;;;;;;GAcG;AAEH,4FAA4F;AAC5F,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,GAAG,iBAAiB,EAAE,MAAM,CAAU,CAAA;AAiB7E,SAAS,MAAM,CAAC,IAAY,EAAE,OAAe;IAC3C,MAAM,sBAAsB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;AAC7C,CAAC;AAED,SAAS,MAAM,CAAC,WAA+B;IAC7C,OAAO;QACL,GAAG,CAAC,WAAW,CAAC,QAAQ,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5D,GAAG,CAAC,WAAW,CAAC,SAAS,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9D,GAAG,CAAC,WAAW,CAAC,KAAK,KAA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/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from './action.js';
2
2
  export * from './define-block.js';
3
3
  export * from './errors.js';
4
4
  export * from './field.js';
5
+ export * from './from-declaration.js';
5
6
  export * from './migrate.js';
6
7
  export * from './plain-text.js';
7
8
  export * from './registry.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,uBAAuB,CAAA;AACrC,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA"}
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ export * from './action.js';
2
2
  export * from './define-block.js';
3
3
  export * from './errors.js';
4
4
  export * from './field.js';
5
+ export * from './from-declaration.js';
5
6
  export * from './migrate.js';
6
7
  export * from './plain-text.js';
7
8
  export * from './registry.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,uBAAuB,CAAA;AACrC,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,cAAc,CAAA;AAC5B,cAAc,iBAAiB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cogenta/blocks",
3
- "version": "1.0.4",
3
+ "version": "1.1.3",
4
4
  "description": "The semantic block vocabulary every Cogenta theme implements.",
5
5
  "license": "MPL-2.0",
6
6
  "type": "module",
@@ -28,8 +28,8 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "zod": "^4.4.3",
31
- "@cogenta/core": "0.9.0",
32
- "@cogenta/schema": "0.5.2"
31
+ "@cogenta/core": "0.11.0",
32
+ "@cogenta/schema": "0.7.1"
33
33
  },
34
34
  "devDependencies": {
35
35
  "typescript": "^7.0.2",