@latex-kit/schema 0.3.0 → 0.4.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
@@ -48,7 +48,9 @@ export default z.object({
48
48
  ```
49
49
 
50
50
  Shared types: `ISODate`, `Money`, `Address`, `Signer`, `Person`,
51
- `Organization`, `Party`, `Localized` (`{ en: "…", ja: "…" }`).
51
+ `Organization`, `Party`, `Localized` (`{ en: "…", ja: "…" }`). A signer,
52
+ person or party may name its signature image file (`signature`), for signed
53
+ builds.
52
54
 
53
55
  ## Records and references
54
56
 
@@ -74,6 +76,25 @@ TypeScript) is replaced by `data/orgs/example-widgets.json|yaml|ts`, so a
74
76
  company or person is written once and used everywhere. References resolve
75
77
  recursively; cycles and paths outside `data/` are errors.
76
78
 
79
+ A record may also say how each party signed it, for `--variant signed`.
80
+ Keys are the paths `\signature` uses:
81
+
82
+ ```yaml
83
+ signed:
84
+ discloser: { date: "2026-11-25" }
85
+ recipient: { date: "2026-11-26", signature: jordan-2026.png }
86
+ ```
87
+
88
+ In TypeScript, `defineDocument` types a record against a template's
89
+ generated type; any object in it may be a `ref()`:
90
+
91
+ ```ts
92
+ import { defineDocument, ref } from 'latex-kit';
93
+ import type { InvoiceData } from '../templates/invoice/types';
94
+
95
+ export default defineDocument<InvoiceData>({ template: 'invoice', data: { from: ref('orgs/acme'), … } });
96
+ ```
97
+
77
98
  ## Document IDs
78
99
 
79
100
  `mintDocId({ prefix, epoch })` makes `NDA-20261125-8K2M4Q7Z` (40 random
@@ -89,6 +110,7 @@ edited: you are told the line to add). Re-renders reuse it, and
89
110
  | `loadFile(file)` | JSON, YAML, or a TS/JS module's default export |
90
111
  | `resolveRefs(value, { dataDir })` | Replace every `$ref`; returns the files read |
91
112
  | `loadDocument(file, { dataDir })` | Read and check a record, resolve its references |
113
+ | `defineDocument<Data>(record)` | Type a TypeScript record |
92
114
  | `loadTemplateSchema(file)` | `{ schema, meta }` from a `schema.ts` |
93
115
  | `validate(schema, value)` | `{ ok, value }` or `{ ok: false, issues: [{ path, message }] }` |
94
116
  | `toJsonSchema(schema)` | JSON Schema of the data as written in records |
package/esm/record.d.ts CHANGED
@@ -1,13 +1,46 @@
1
1
  import { z } from 'zod';
2
+ import { Ref } from './ref';
3
+ /** How one party signed this document: `signed: { discloser: { date: 2026-11-25 } }`. */
4
+ export declare const Signing: z.ZodObject<{
5
+ date: z.ZodOptional<z.ZodString>;
6
+ signature: z.ZodOptional<z.ZodString>;
7
+ }, z.core.$strict>;
8
+ export type Signing = z.infer<typeof Signing>;
2
9
  /** The file that describes one document: which template, its ID, its data. */
3
10
  export declare const DocumentRecord: z.ZodObject<{
4
11
  $schema: z.ZodOptional<z.ZodString>;
5
12
  template: z.ZodString;
6
13
  docId: z.ZodOptional<z.ZodString>;
7
14
  locale: z.ZodOptional<z.ZodString>;
15
+ signed: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
16
+ date: z.ZodOptional<z.ZodString>;
17
+ signature: z.ZodOptional<z.ZodString>;
18
+ }, z.core.$strict>>>;
8
19
  data: z.ZodUnknown;
9
20
  }, z.core.$strict>;
10
21
  export type DocumentRecord = z.infer<typeof DocumentRecord>;
22
+ /** `T`, where any object may instead be a reference to a shared record. */
23
+ export type WithRefs<T> = T extends readonly (infer U)[] ? WithRefs<U>[] : T extends object ? {
24
+ [K in keyof T]: WithRefs<T[K]>;
25
+ } | Ref : T;
26
+ /** A document record written in TypeScript. */
27
+ export interface DocumentInput<Data = unknown> {
28
+ template: string;
29
+ docId?: string;
30
+ locale?: string;
31
+ signed?: Record<string, Signing>;
32
+ data: WithRefs<Data>;
33
+ }
34
+ /**
35
+ * Type a TypeScript record: `documents/<name>.ts`. Pass the template's
36
+ * generated type to check the data as you write it:
37
+ *
38
+ * import type { InvoiceData } from '../templates/invoice/types';
39
+ * export default defineDocument<InvoiceData>({ template: 'invoice', data: { … } });
40
+ *
41
+ * Computed values (totals, dates) belong here, not in the template.
42
+ */
43
+ export declare function defineDocument<Data = unknown>(record: DocumentInput<Data>): DocumentInput<Data>;
11
44
  export interface LoadedDocument {
12
45
  file: string;
13
46
  record: DocumentRecord;
package/esm/record.js CHANGED
@@ -1,6 +1,16 @@
1
1
  import { z } from 'zod';
2
2
  import { loadFile } from './load';
3
3
  import { resolveRefs } from './ref';
4
+ import { ISODate } from './types';
5
+ /** How one party signed this document: `signed: { discloser: { date: 2026-11-25 } }`. */
6
+ export const Signing = z
7
+ .object({
8
+ /** Printed on the signature block's date line in signed builds. */
9
+ date: ISODate.optional(),
10
+ /** Signature image file for this document, instead of the signer's default. */
11
+ signature: z.string().optional(),
12
+ })
13
+ .strict();
4
14
  /** The file that describes one document: which template, its ID, its data. */
5
15
  export const DocumentRecord = z
6
16
  .object({
@@ -8,9 +18,23 @@ export const DocumentRecord = z
8
18
  template: z.string().min(1),
9
19
  docId: z.string().min(1).optional(),
10
20
  locale: z.string().optional(),
21
+ /** Signing facts per party, keyed by the path \\signature uses. */
22
+ signed: z.record(z.string(), Signing).optional(),
11
23
  data: z.unknown(),
12
24
  })
13
25
  .strict();
26
+ /**
27
+ * Type a TypeScript record: `documents/<name>.ts`. Pass the template's
28
+ * generated type to check the data as you write it:
29
+ *
30
+ * import type { InvoiceData } from '../templates/invoice/types';
31
+ * export default defineDocument<InvoiceData>({ template: 'invoice', data: { … } });
32
+ *
33
+ * Computed values (totals, dates) belong here, not in the template.
34
+ */
35
+ export function defineDocument(record) {
36
+ return record;
37
+ }
14
38
  export class RecordError extends Error {
15
39
  file;
16
40
  constructor(message, file) {
@@ -24,7 +48,7 @@ export async function loadDocument(file, options) {
24
48
  const parsed = DocumentRecord.safeParse(raw);
25
49
  if (!parsed.success) {
26
50
  const msg = parsed.error.issues.map((i) => `${i.path.join('.') || 'record'}: ${i.message}`).join('; ');
27
- throw new RecordError(`${file}: not a document record (${msg}). Expected { template, docId?, locale?, data }`, file);
51
+ throw new RecordError(`${file}: not a document record (${msg}). Expected { template, docId?, locale?, signed?, data }`, file);
28
52
  }
29
53
  const { value, files } = await resolveRefs(parsed.data.data ?? {}, { dataDir: options.dataDir });
30
54
  return { file, record: parsed.data, data: value, refFiles: files };
package/esm/schema.d.ts CHANGED
@@ -6,8 +6,8 @@ export interface TemplateMeta {
6
6
  docIdPrefix?: string;
7
7
  /** Default locale for records that do not set one. */
8
8
  locale?: string;
9
- /** TeX engine for this template. Default: lualatex. */
10
- engine?: 'lualatex' | 'xelatex' | 'pdflatex';
9
+ /** TeX engine for this template. Default: `auto` (chosen from the packages the template loads). */
10
+ engine?: 'auto' | 'lualatex' | 'xelatex' | 'pdflatex' | 'pdfps';
11
11
  title?: string;
12
12
  }
13
13
  export interface TemplateSchema {
package/esm/types.d.ts CHANGED
@@ -16,11 +16,13 @@ export declare const Address: z.ZodObject<{
16
16
  }, z.core.$strip>;
17
17
  /** Someone who signs: a name and, usually, a title. */
18
18
  export declare const Signer: z.ZodObject<{
19
+ signature: z.ZodOptional<z.ZodString>;
19
20
  name: z.ZodString;
20
21
  title: z.ZodOptional<z.ZodString>;
21
22
  email: z.ZodOptional<z.ZodString>;
22
23
  }, z.core.$strip>;
23
24
  export declare const Person: z.ZodObject<{
25
+ signature: z.ZodOptional<z.ZodString>;
24
26
  name: z.ZodString;
25
27
  title: z.ZodOptional<z.ZodString>;
26
28
  email: z.ZodOptional<z.ZodString>;
@@ -46,6 +48,7 @@ export declare const Organization: z.ZodObject<{
46
48
  country: z.ZodOptional<z.ZodString>;
47
49
  }, z.core.$strip>>;
48
50
  signer: z.ZodOptional<z.ZodObject<{
51
+ signature: z.ZodOptional<z.ZodString>;
49
52
  name: z.ZodString;
50
53
  title: z.ZodOptional<z.ZodString>;
51
54
  email: z.ZodOptional<z.ZodString>;
@@ -53,6 +56,7 @@ export declare const Organization: z.ZodObject<{
53
56
  }, z.core.$strip>;
54
57
  /** One side of an agreement: a person or an organization. */
55
58
  export declare const Party: z.ZodObject<{
59
+ signature: z.ZodOptional<z.ZodString>;
56
60
  kind: z.ZodOptional<z.ZodEnum<{
57
61
  person: "person";
58
62
  organization: "organization";
@@ -71,6 +75,7 @@ export declare const Party: z.ZodObject<{
71
75
  country: z.ZodOptional<z.ZodString>;
72
76
  }, z.core.$strip>>;
73
77
  signer: z.ZodOptional<z.ZodObject<{
78
+ signature: z.ZodOptional<z.ZodString>;
74
79
  name: z.ZodString;
75
80
  title: z.ZodOptional<z.ZodString>;
76
81
  email: z.ZodOptional<z.ZodString>;
package/esm/types.js CHANGED
@@ -14,17 +14,27 @@ export const Address = z.object({
14
14
  postalCode: z.string().optional(),
15
15
  country: z.string().optional(),
16
16
  });
17
+ /** For `\\signature` in signed builds. (Signing dates go in the record's `signed`.) */
18
+ const signing = {
19
+ /**
20
+ * File name of this person's signature image in the signatures folder
21
+ * (`--signatures`, never in the repo). Default: the name, slugged.
22
+ */
23
+ signature: z.string().optional(),
24
+ };
17
25
  /** Someone who signs: a name and, usually, a title. */
18
26
  export const Signer = z.object({
19
27
  name: z.string(),
20
28
  title: z.string().optional(),
21
29
  email: z.string().optional(),
30
+ ...signing,
22
31
  });
23
32
  export const Person = z.object({
24
33
  name: z.string(),
25
34
  title: z.string().optional(),
26
35
  email: z.string().optional(),
27
36
  address: Address.optional(),
37
+ ...signing,
28
38
  });
29
39
  export const Organization = z.object({
30
40
  name: z.string(),
@@ -44,6 +54,7 @@ export const Party = z.object({
44
54
  email: z.string().optional(),
45
55
  address: Address.optional(),
46
56
  signer: Signer.optional(),
57
+ ...signing,
47
58
  });
48
59
  /** Text in several languages, keyed by language tag: `{ en: "…", ja: "…" }`. */
49
60
  export const Localized = z.record(z.string(), z.string());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@latex-kit/schema",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Typed data for latex-kit documents: zod schemas, shared types, references, document IDs",
5
5
  "author": "Constructive <developers@constructive.io>",
6
6
  "main": "index.js",
@@ -44,5 +44,5 @@
44
44
  "yaml": "^2.9.1",
45
45
  "zod": "^4.6.7"
46
46
  },
47
- "gitHead": "023ede48131a32e9250ec48b0dde17e71c6a1536"
47
+ "gitHead": "b169d7b7a018ec26e5099e021b3529dcf6c006fa"
48
48
  }
package/record.d.ts CHANGED
@@ -1,13 +1,46 @@
1
1
  import { z } from 'zod';
2
+ import { Ref } from './ref';
3
+ /** How one party signed this document: `signed: { discloser: { date: 2026-11-25 } }`. */
4
+ export declare const Signing: z.ZodObject<{
5
+ date: z.ZodOptional<z.ZodString>;
6
+ signature: z.ZodOptional<z.ZodString>;
7
+ }, z.core.$strict>;
8
+ export type Signing = z.infer<typeof Signing>;
2
9
  /** The file that describes one document: which template, its ID, its data. */
3
10
  export declare const DocumentRecord: z.ZodObject<{
4
11
  $schema: z.ZodOptional<z.ZodString>;
5
12
  template: z.ZodString;
6
13
  docId: z.ZodOptional<z.ZodString>;
7
14
  locale: z.ZodOptional<z.ZodString>;
15
+ signed: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
16
+ date: z.ZodOptional<z.ZodString>;
17
+ signature: z.ZodOptional<z.ZodString>;
18
+ }, z.core.$strict>>>;
8
19
  data: z.ZodUnknown;
9
20
  }, z.core.$strict>;
10
21
  export type DocumentRecord = z.infer<typeof DocumentRecord>;
22
+ /** `T`, where any object may instead be a reference to a shared record. */
23
+ export type WithRefs<T> = T extends readonly (infer U)[] ? WithRefs<U>[] : T extends object ? {
24
+ [K in keyof T]: WithRefs<T[K]>;
25
+ } | Ref : T;
26
+ /** A document record written in TypeScript. */
27
+ export interface DocumentInput<Data = unknown> {
28
+ template: string;
29
+ docId?: string;
30
+ locale?: string;
31
+ signed?: Record<string, Signing>;
32
+ data: WithRefs<Data>;
33
+ }
34
+ /**
35
+ * Type a TypeScript record: `documents/<name>.ts`. Pass the template's
36
+ * generated type to check the data as you write it:
37
+ *
38
+ * import type { InvoiceData } from '../templates/invoice/types';
39
+ * export default defineDocument<InvoiceData>({ template: 'invoice', data: { … } });
40
+ *
41
+ * Computed values (totals, dates) belong here, not in the template.
42
+ */
43
+ export declare function defineDocument<Data = unknown>(record: DocumentInput<Data>): DocumentInput<Data>;
11
44
  export interface LoadedDocument {
12
45
  file: string;
13
46
  record: DocumentRecord;
package/record.js CHANGED
@@ -1,10 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RecordError = exports.DocumentRecord = void 0;
3
+ exports.RecordError = exports.DocumentRecord = exports.Signing = void 0;
4
+ exports.defineDocument = defineDocument;
4
5
  exports.loadDocument = loadDocument;
5
6
  const zod_1 = require("zod");
6
7
  const load_1 = require("./load");
7
8
  const ref_1 = require("./ref");
9
+ const types_1 = require("./types");
10
+ /** How one party signed this document: `signed: { discloser: { date: 2026-11-25 } }`. */
11
+ exports.Signing = zod_1.z
12
+ .object({
13
+ /** Printed on the signature block's date line in signed builds. */
14
+ date: types_1.ISODate.optional(),
15
+ /** Signature image file for this document, instead of the signer's default. */
16
+ signature: zod_1.z.string().optional(),
17
+ })
18
+ .strict();
8
19
  /** The file that describes one document: which template, its ID, its data. */
9
20
  exports.DocumentRecord = zod_1.z
10
21
  .object({
@@ -12,9 +23,23 @@ exports.DocumentRecord = zod_1.z
12
23
  template: zod_1.z.string().min(1),
13
24
  docId: zod_1.z.string().min(1).optional(),
14
25
  locale: zod_1.z.string().optional(),
26
+ /** Signing facts per party, keyed by the path \\signature uses. */
27
+ signed: zod_1.z.record(zod_1.z.string(), exports.Signing).optional(),
15
28
  data: zod_1.z.unknown(),
16
29
  })
17
30
  .strict();
31
+ /**
32
+ * Type a TypeScript record: `documents/<name>.ts`. Pass the template's
33
+ * generated type to check the data as you write it:
34
+ *
35
+ * import type { InvoiceData } from '../templates/invoice/types';
36
+ * export default defineDocument<InvoiceData>({ template: 'invoice', data: { … } });
37
+ *
38
+ * Computed values (totals, dates) belong here, not in the template.
39
+ */
40
+ function defineDocument(record) {
41
+ return record;
42
+ }
18
43
  class RecordError extends Error {
19
44
  file;
20
45
  constructor(message, file) {
@@ -29,7 +54,7 @@ async function loadDocument(file, options) {
29
54
  const parsed = exports.DocumentRecord.safeParse(raw);
30
55
  if (!parsed.success) {
31
56
  const msg = parsed.error.issues.map((i) => `${i.path.join('.') || 'record'}: ${i.message}`).join('; ');
32
- throw new RecordError(`${file}: not a document record (${msg}). Expected { template, docId?, locale?, data }`, file);
57
+ throw new RecordError(`${file}: not a document record (${msg}). Expected { template, docId?, locale?, signed?, data }`, file);
33
58
  }
34
59
  const { value, files } = await (0, ref_1.resolveRefs)(parsed.data.data ?? {}, { dataDir: options.dataDir });
35
60
  return { file, record: parsed.data, data: value, refFiles: files };
package/schema.d.ts CHANGED
@@ -6,8 +6,8 @@ export interface TemplateMeta {
6
6
  docIdPrefix?: string;
7
7
  /** Default locale for records that do not set one. */
8
8
  locale?: string;
9
- /** TeX engine for this template. Default: lualatex. */
10
- engine?: 'lualatex' | 'xelatex' | 'pdflatex';
9
+ /** TeX engine for this template. Default: `auto` (chosen from the packages the template loads). */
10
+ engine?: 'auto' | 'lualatex' | 'xelatex' | 'pdflatex' | 'pdfps';
11
11
  title?: string;
12
12
  }
13
13
  export interface TemplateSchema {
package/types.d.ts CHANGED
@@ -16,11 +16,13 @@ export declare const Address: z.ZodObject<{
16
16
  }, z.core.$strip>;
17
17
  /** Someone who signs: a name and, usually, a title. */
18
18
  export declare const Signer: z.ZodObject<{
19
+ signature: z.ZodOptional<z.ZodString>;
19
20
  name: z.ZodString;
20
21
  title: z.ZodOptional<z.ZodString>;
21
22
  email: z.ZodOptional<z.ZodString>;
22
23
  }, z.core.$strip>;
23
24
  export declare const Person: z.ZodObject<{
25
+ signature: z.ZodOptional<z.ZodString>;
24
26
  name: z.ZodString;
25
27
  title: z.ZodOptional<z.ZodString>;
26
28
  email: z.ZodOptional<z.ZodString>;
@@ -46,6 +48,7 @@ export declare const Organization: z.ZodObject<{
46
48
  country: z.ZodOptional<z.ZodString>;
47
49
  }, z.core.$strip>>;
48
50
  signer: z.ZodOptional<z.ZodObject<{
51
+ signature: z.ZodOptional<z.ZodString>;
49
52
  name: z.ZodString;
50
53
  title: z.ZodOptional<z.ZodString>;
51
54
  email: z.ZodOptional<z.ZodString>;
@@ -53,6 +56,7 @@ export declare const Organization: z.ZodObject<{
53
56
  }, z.core.$strip>;
54
57
  /** One side of an agreement: a person or an organization. */
55
58
  export declare const Party: z.ZodObject<{
59
+ signature: z.ZodOptional<z.ZodString>;
56
60
  kind: z.ZodOptional<z.ZodEnum<{
57
61
  person: "person";
58
62
  organization: "organization";
@@ -71,6 +75,7 @@ export declare const Party: z.ZodObject<{
71
75
  country: z.ZodOptional<z.ZodString>;
72
76
  }, z.core.$strip>>;
73
77
  signer: z.ZodOptional<z.ZodObject<{
78
+ signature: z.ZodOptional<z.ZodString>;
74
79
  name: z.ZodString;
75
80
  title: z.ZodOptional<z.ZodString>;
76
81
  email: z.ZodOptional<z.ZodString>;
package/types.js CHANGED
@@ -17,17 +17,27 @@ exports.Address = zod_1.z.object({
17
17
  postalCode: zod_1.z.string().optional(),
18
18
  country: zod_1.z.string().optional(),
19
19
  });
20
+ /** For `\\signature` in signed builds. (Signing dates go in the record's `signed`.) */
21
+ const signing = {
22
+ /**
23
+ * File name of this person's signature image in the signatures folder
24
+ * (`--signatures`, never in the repo). Default: the name, slugged.
25
+ */
26
+ signature: zod_1.z.string().optional(),
27
+ };
20
28
  /** Someone who signs: a name and, usually, a title. */
21
29
  exports.Signer = zod_1.z.object({
22
30
  name: zod_1.z.string(),
23
31
  title: zod_1.z.string().optional(),
24
32
  email: zod_1.z.string().optional(),
33
+ ...signing,
25
34
  });
26
35
  exports.Person = zod_1.z.object({
27
36
  name: zod_1.z.string(),
28
37
  title: zod_1.z.string().optional(),
29
38
  email: zod_1.z.string().optional(),
30
39
  address: exports.Address.optional(),
40
+ ...signing,
31
41
  });
32
42
  exports.Organization = zod_1.z.object({
33
43
  name: zod_1.z.string(),
@@ -47,6 +57,7 @@ exports.Party = zod_1.z.object({
47
57
  email: zod_1.z.string().optional(),
48
58
  address: exports.Address.optional(),
49
59
  signer: exports.Signer.optional(),
60
+ ...signing,
50
61
  });
51
62
  /** Text in several languages, keyed by language tag: `{ en: "…", ja: "…" }`. */
52
63
  exports.Localized = zod_1.z.record(zod_1.z.string(), zod_1.z.string());