@polotno/schema 0.1.1 → 0.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.
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/normalize.d.ts +24 -1
- package/lib/normalize.d.ts.map +1 -1
- package/lib/normalize.js +62 -1
- package/lib/normalize.js.map +1 -1
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { normalizeDesign } from './normalize.js';
|
|
1
|
+
export { normalizeDesign, parseDesign } from './normalize.js';
|
|
2
|
+
export type { ParseOptions } from './normalize.js';
|
|
2
3
|
export { validateDesign, assertValidDesign, designSchemaLenient, designSchemaCanonical, CURRENT_SCHEMA_VERSION, } from './schema.js';
|
|
3
4
|
export type { DesignInput, ValidationError, ValidationResult, ValidateOptions, } from './schema.js';
|
|
4
5
|
export type * from './types.js';
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC9D,YAAY,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,aAAa,CAAC;AACrB,YAAY,EACV,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,eAAe,GAChB,MAAM,aAAa,CAAC;AAErB,mBAAmB,YAAY,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
// Single source: the zod schema (`./schema`) provides validation, defaults, and
|
|
3
3
|
// the generated JSON Schema; `./types` are the hand-written public TypeScript
|
|
4
4
|
// types. zod is a regular dependency (normalize parses with it).
|
|
5
|
-
export { normalizeDesign } from './normalize.js';
|
|
5
|
+
export { normalizeDesign, parseDesign } from './normalize.js';
|
|
6
6
|
export { validateDesign, assertValidDesign, designSchemaLenient, designSchemaCanonical, CURRENT_SCHEMA_VERSION, } from './schema.js';
|
|
7
7
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qDAAqD;AACrD,gFAAgF;AAChF,8EAA8E;AAC9E,iEAAiE;AAEjE,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qDAAqD;AACrD,gFAAgF;AAChF,8EAA8E;AAC9E,iEAAiE;AAEjE,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG9D,OAAO,EACL,cAAc,EACd,iBAAiB,EACjB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,aAAa,CAAC"}
|
package/lib/normalize.d.ts
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
|
-
import type { DesignInput } from './schema.js';
|
|
1
|
+
import type { DesignInput, ValidateOptions } from './schema.js';
|
|
2
2
|
import type { Design } from './types.js';
|
|
3
3
|
export declare function normalizeDesign(json: DesignInput): Design;
|
|
4
|
+
export interface ParseOptions extends ValidateOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Reject a design with no pages — catches the common mistake of passing the
|
|
7
|
+
* wrong object (e.g. the store itself instead of `store.toJSON()`), which
|
|
8
|
+
* otherwise validates as an empty design.
|
|
9
|
+
*/
|
|
10
|
+
requirePages?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Assign a synthetic `id` to any page/element/cell that lacks one before
|
|
13
|
+
* validating. `id` is otherwise required, but it's identity metadata the
|
|
14
|
+
* renderer doesn't draw, so consumers that accept hand-built JSON (the export
|
|
15
|
+
* converters) stay tolerant. Real `store.toJSON()` output always has ids, so
|
|
16
|
+
* this is a no-op there. Does not touch the caller's input (works on a copy).
|
|
17
|
+
*/
|
|
18
|
+
fillMissingIds?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The one entry a consumer should use to accept design JSON: validate, then
|
|
22
|
+
* normalize. Throws a formatted `Error` (`Invalid Polotno design:\n path:
|
|
23
|
+
* message`) on invalid input — the single source of the error format, shared
|
|
24
|
+
* with `assertValidDesign`.
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseDesign(input: unknown, opts?: ParseOptions): Design;
|
|
4
27
|
//# sourceMappingURL=normalize.d.ts.map
|
package/lib/normalize.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AA4BzC,wBAAgB,eAAe,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM,CAEzD;AAED,MAAM,WAAW,YAAa,SAAQ,eAAe;IACnD;;;;OAIG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAuCD;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAE,YAAiB,GAAG,MAAM,CAc3E"}
|
package/lib/normalize.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// No schemaVersion migration: this package assumes current-format input. A
|
|
5
5
|
// missing schemaVersion defaults to the current version; old documents that
|
|
6
6
|
// need migrating should round-trip through the core `polotno` store.
|
|
7
|
-
import { designSchemaLenient } from './schema.js';
|
|
7
|
+
import { designSchemaLenient, assertValidDesign } from './schema.js';
|
|
8
8
|
// Keys whose value is opaque client data (`custom` everywhere, animation `data`
|
|
9
9
|
// — both `z.unknown()`). The core store keeps these verbatim (`types.frozen()`),
|
|
10
10
|
// and CLAUDE.md forbids touching `custom`, so we copy them as-is and never walk
|
|
@@ -33,4 +33,65 @@ function cloneDropNulls(value) {
|
|
|
33
33
|
export function normalizeDesign(json) {
|
|
34
34
|
return designSchemaLenient.parse(cloneDropNulls(json));
|
|
35
35
|
}
|
|
36
|
+
const ID_ALPHABET = 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict';
|
|
37
|
+
// URL-safe id matching core's `nanoid(10)` (group-model.ts / page-model.ts) — 10
|
|
38
|
+
// chars from a 64-char alphabet. Inlined (no dependency) so schema bundles under
|
|
39
|
+
// any platform/bundler; prefers Web Crypto where present (browser + modern Node),
|
|
40
|
+
// falls back to Math.random so it never throws.
|
|
41
|
+
function randomId(size = 10) {
|
|
42
|
+
const c = globalThis.crypto;
|
|
43
|
+
let id = '';
|
|
44
|
+
if (c && typeof c.getRandomValues === 'function') {
|
|
45
|
+
const bytes = c.getRandomValues(new Uint8Array(size));
|
|
46
|
+
for (let i = 0; i < size; i++)
|
|
47
|
+
id += ID_ALPHABET[bytes[i] & 63];
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
for (let i = 0; i < size; i++)
|
|
51
|
+
id += ID_ALPHABET[(Math.random() * 64) | 0];
|
|
52
|
+
}
|
|
53
|
+
return id;
|
|
54
|
+
}
|
|
55
|
+
// Return a deep copy with a fresh `id` on any page/element/cell missing one.
|
|
56
|
+
// Recurses children (groups) + cells (tables) so ids stay collision-safe even if
|
|
57
|
+
// designs are later merged. Other required fields (`type`, font `fontFamily`) are
|
|
58
|
+
// left alone — their absence is genuinely malformed.
|
|
59
|
+
function withFilledIds(input) {
|
|
60
|
+
if (!input || typeof input !== 'object')
|
|
61
|
+
return input;
|
|
62
|
+
const clone = JSON.parse(JSON.stringify(input));
|
|
63
|
+
const ensure = (node) => {
|
|
64
|
+
if (node && typeof node === 'object') {
|
|
65
|
+
if (node.id == null || node.id === '')
|
|
66
|
+
node.id = randomId(10);
|
|
67
|
+
if (Array.isArray(node.children))
|
|
68
|
+
node.children.forEach(ensure);
|
|
69
|
+
if (Array.isArray(node.cells))
|
|
70
|
+
node.cells.forEach(ensure);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
if (Array.isArray(clone.pages))
|
|
74
|
+
clone.pages.forEach(ensure);
|
|
75
|
+
return clone;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The one entry a consumer should use to accept design JSON: validate, then
|
|
79
|
+
* normalize. Throws a formatted `Error` (`Invalid Polotno design:\n path:
|
|
80
|
+
* message`) on invalid input — the single source of the error format, shared
|
|
81
|
+
* with `assertValidDesign`.
|
|
82
|
+
*/
|
|
83
|
+
export function parseDesign(input, opts = {}) {
|
|
84
|
+
if (opts.fillMissingIds)
|
|
85
|
+
input = withFilledIds(input);
|
|
86
|
+
// Drop nulls first so validation agrees with normalization: both treat a
|
|
87
|
+
// null-valued field as missing (some inputs emit `null` for "unset", e.g.
|
|
88
|
+
// `dpi: null`). Without this, validation rejects nulls that normalize fills.
|
|
89
|
+
const cleaned = cloneDropNulls(input);
|
|
90
|
+
assertValidDesign(cleaned, opts);
|
|
91
|
+
const design = designSchemaLenient.parse(cleaned);
|
|
92
|
+
if (opts.requirePages && design.pages.length === 0) {
|
|
93
|
+
throw new Error('Invalid Polotno design: no pages — did you pass `store.toJSON()`?');
|
|
94
|
+
}
|
|
95
|
+
return design;
|
|
96
|
+
}
|
|
36
97
|
//# sourceMappingURL=normalize.js.map
|
package/lib/normalize.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,yEAAyE;AACzE,EAAE;AACF,2EAA2E;AAC3E,4EAA4E;AAC5E,qEAAqE;AAErE,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../src/normalize.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,yEAAyE;AACzE,EAAE;AACF,2EAA2E;AAC3E,4EAA4E;AAC5E,qEAAqE;AAErE,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAIrE,gFAAgF;AAChF,iFAAiF;AACjF,gFAAgF;AAChF,aAAa;AACb,MAAM,WAAW,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;AAEhD,iFAAiF;AACjF,iFAAiF;AACjF,6EAA6E;AAC7E,SAAS,cAAc,CAAC,KAAU;IAChC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC;IAC5D,CAAC;IACD,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACvC,MAAM,GAAG,GAAQ,EAAE,CAAC;QACpB,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACrC,MAAM,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC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