@brydio/manifest 0.1.0-alpha.33 → 0.1.0-alpha.35
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/package.json +2 -2
- package/src/confirm-email.d.ts +40 -0
- package/src/confirm-email.js +78 -0
- package/src/field-types.d.ts +6 -0
- package/src/field-types.js +2 -2
- package/src/index.d.ts +2 -0
- package/src/index.js +2 -0
- package/src/open-schema.d.ts +128 -0
- package/src/open-schema.js +444 -0
- package/src/schema.d.ts +64 -1
- package/src/schema.js +57 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brydio/manifest",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.35",
|
|
4
4
|
"description": "The shape of a Brydio app's manifest, its collections' schemas, and the checks that say what is wrong with one.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"url": "https://github.com/nakel-ola/brydio-sdk",
|
|
11
11
|
"directory": "packages/manifest"
|
|
12
12
|
},
|
|
13
|
-
"gitHead": "
|
|
13
|
+
"gitHead": "5bd1e984602ccea955d5755e613fe773ae4899ca",
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
16
|
"types": "./src/index.d.ts",
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* The confirmation email of a public form (FO04). Mirrors Brydio's
|
|
4
|
+
* `apps/api/src/apps/manifest/confirm-email.ts`; the two change together.
|
|
5
|
+
*
|
|
6
|
+
* An app only declares it: which field of a `publicSubmit` collection holds
|
|
7
|
+
* the visitor's address, and a short plain text to say. Brydio decides the
|
|
8
|
+
* rest — when it is sent, to whom, how often, from whom — so an anonymous
|
|
9
|
+
* page can never be used to send anybody anything else.
|
|
10
|
+
*/
|
|
11
|
+
export declare const CONFIRM_LIMITS: {
|
|
12
|
+
readonly subjectChars: 120;
|
|
13
|
+
readonly messageChars: 600;
|
|
14
|
+
};
|
|
15
|
+
export declare const confirmEmailSchema: z.ZodObject<{
|
|
16
|
+
field: z.ZodString;
|
|
17
|
+
subject: z.ZodOptional<z.ZodString>;
|
|
18
|
+
message: z.ZodOptional<z.ZodString>;
|
|
19
|
+
link: z.ZodOptional<z.ZodBoolean>;
|
|
20
|
+
}, z.core.$strict>;
|
|
21
|
+
export type ConfirmEmailDeclared = z.infer<typeof confirmEmailSchema>;
|
|
22
|
+
/** As the host reads it, on `CollectionSpec.confirmEmail`. */
|
|
23
|
+
export interface ConfirmEmailSpec {
|
|
24
|
+
field: string;
|
|
25
|
+
subject: string | null;
|
|
26
|
+
message: string | null;
|
|
27
|
+
link: boolean;
|
|
28
|
+
}
|
|
29
|
+
export type ConfirmProblemCode = 'data_confirm_submit' | 'data_confirm_field' | 'data_confirm_text';
|
|
30
|
+
export declare function confirmEmailProblems(collection: string, declared: {
|
|
31
|
+
schema: Record<string, unknown>;
|
|
32
|
+
publicSubmit?: boolean;
|
|
33
|
+
confirmEmail?: ConfirmEmailDeclared;
|
|
34
|
+
}): {
|
|
35
|
+
code: ConfirmProblemCode;
|
|
36
|
+
collection: string;
|
|
37
|
+
field?: string;
|
|
38
|
+
message: string;
|
|
39
|
+
}[];
|
|
40
|
+
export declare function confirmEmailSpec(declared: ConfirmEmailDeclared): ConfirmEmailSpec;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { FIELD_LIMITS, parseFieldType } from "./field-types.js";
|
|
3
|
+
/**
|
|
4
|
+
* The confirmation email of a public form (FO04). Mirrors Brydio's
|
|
5
|
+
* `apps/api/src/apps/manifest/confirm-email.ts`; the two change together.
|
|
6
|
+
*
|
|
7
|
+
* An app only declares it: which field of a `publicSubmit` collection holds
|
|
8
|
+
* the visitor's address, and a short plain text to say. Brydio decides the
|
|
9
|
+
* rest — when it is sent, to whom, how often, from whom — so an anonymous
|
|
10
|
+
* page can never be used to send anybody anything else.
|
|
11
|
+
*/
|
|
12
|
+
export const CONFIRM_LIMITS = {
|
|
13
|
+
subjectChars: 120,
|
|
14
|
+
messageChars: 600,
|
|
15
|
+
};
|
|
16
|
+
export const confirmEmailSchema = z
|
|
17
|
+
.object({
|
|
18
|
+
/** A `string` field of the same collection: the visitor's address. */
|
|
19
|
+
field: z.string().min(1).max(FIELD_LIMITS.nameChars),
|
|
20
|
+
subject: z.string().min(1).max(CONFIRM_LIMITS.subjectChars).optional(),
|
|
21
|
+
/** Plain text; a blank line starts a new paragraph. */
|
|
22
|
+
message: z.string().min(1).max(CONFIRM_LIMITS.messageChars).optional(),
|
|
23
|
+
/** One button back to the public page the visitor answered. */
|
|
24
|
+
link: z.boolean().optional(),
|
|
25
|
+
})
|
|
26
|
+
.strict();
|
|
27
|
+
/** A link, an address a client would turn into one, or markup. */
|
|
28
|
+
const NOT_PLAIN = /(?:[a-z][a-z0-9+.-]*:\/\/|www\.|[<>]|\]\(|mailto:)/i;
|
|
29
|
+
export function confirmEmailProblems(collection, declared) {
|
|
30
|
+
const confirm = declared.confirmEmail;
|
|
31
|
+
if (!confirm)
|
|
32
|
+
return [];
|
|
33
|
+
const problems = [];
|
|
34
|
+
if (!declared.publicSubmit) {
|
|
35
|
+
problems.push({
|
|
36
|
+
code: 'data_confirm_submit',
|
|
37
|
+
collection,
|
|
38
|
+
message: `${collection} sends a confirmation email but visitors can't submit to it: mark it publicSubmit.`,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
const raw = declared.schema[confirm.field];
|
|
42
|
+
let kind = null;
|
|
43
|
+
try {
|
|
44
|
+
kind = raw === undefined ? null : parseFieldType(raw).kind;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
kind = null;
|
|
48
|
+
}
|
|
49
|
+
if (kind !== 'string') {
|
|
50
|
+
problems.push({
|
|
51
|
+
code: 'data_confirm_field',
|
|
52
|
+
collection,
|
|
53
|
+
field: confirm.field,
|
|
54
|
+
message: `${collection}.confirmEmail.field must name one of its string fields; ${confirm.field} is not.`,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
for (const [key, text] of [
|
|
58
|
+
['subject', confirm.subject],
|
|
59
|
+
['message', confirm.message],
|
|
60
|
+
]) {
|
|
61
|
+
if (text !== undefined && NOT_PLAIN.test(text)) {
|
|
62
|
+
problems.push({
|
|
63
|
+
code: 'data_confirm_text',
|
|
64
|
+
collection,
|
|
65
|
+
message: `${collection}.confirmEmail.${key} is plain text: no links, addresses or markup.`,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return problems;
|
|
70
|
+
}
|
|
71
|
+
export function confirmEmailSpec(declared) {
|
|
72
|
+
return {
|
|
73
|
+
field: declared.field,
|
|
74
|
+
subject: declared.subject?.trim() || null,
|
|
75
|
+
message: declared.message?.trim() || null,
|
|
76
|
+
link: declared.link === true,
|
|
77
|
+
};
|
|
78
|
+
}
|
package/src/field-types.d.ts
CHANGED
|
@@ -34,6 +34,12 @@ export interface FieldType {
|
|
|
34
34
|
* check, and a tool's change to it merges into the live text.
|
|
35
35
|
*/
|
|
36
36
|
coedit?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Kept in the plain `fields` column although its kind is not (P5): an
|
|
39
|
+
* open-schema table's multi-select and link lists, and the field naming a
|
|
40
|
+
* row's table. Set by the host only, never parsed from a manifest.
|
|
41
|
+
*/
|
|
42
|
+
plain?: boolean;
|
|
37
43
|
}
|
|
38
44
|
/**
|
|
39
45
|
* The bounds a schema and a record are held to (A3-F01-S03).
|
package/src/field-types.js
CHANGED
|
@@ -209,9 +209,9 @@ function parseEnumeration(values) {
|
|
|
209
209
|
return { kind: 'enum', optional: false, values: values };
|
|
210
210
|
}
|
|
211
211
|
/** True for a type whose value lands in the plain `fields` column. */
|
|
212
|
-
export const isStructured = (type) => STRUCTURED.has(type.kind);
|
|
212
|
+
export const isStructured = (type) => STRUCTURED.has(type.kind) || type.plain === true;
|
|
213
213
|
/** True for a type a list can be ordered by: structured, and one value. */
|
|
214
|
-
export const isSortable = (type) => STRUCTURED.has(type.kind);
|
|
214
|
+
export const isSortable = (type) => STRUCTURED.has(type.kind) || (type.plain === true && type.kind !== 'string[]');
|
|
215
215
|
export const isSearchable = (type) => SEARCHABLE.has(type.kind);
|
|
216
216
|
/**
|
|
217
217
|
* The names of a schema's structured fields: the ones kept in plain beside the
|
package/src/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export { BRAND_FIELDS, BRAND_LIMITS, brandFilePath, brandPathsOf, brandProblems,
|
|
|
8
8
|
export { BUNDLE_MANIFEST, BUNDLE_MAX_BYTES, BUNDLE_PATH_MAX_CHARS, bundleBytes, bundleHash, brandOf, bundleProblem, codeOf, isBundlePath, isScriptPath, sizeOf, type BundleFiles, type BundleProblem, type BundleProblemCode, } from './bundle.js';
|
|
9
9
|
export { COLOUR_TOKENS, coeditedFields, FIELD_LIMITS, RESERVED_FIELDS, describeType, isCoedited, isSearchable, isSortable, isStructured, parseFieldType, structuredFields, valueProblem, valueSchema, FieldTypeInvalid, type DocumentOf, type FieldKind, type FieldType, type FieldValue, type FieldsOf, } from './field-types.js';
|
|
10
10
|
export { appManifestSchema, collectionOf, collectionsOf, dataProblems, effectivePlacementKey, HOME_CARD_SIZES, labelOf, manifestExtensionsSchema, newAppManifestSchema, SIZED_PLACEMENT_KINDS, MAX_APP_SECRETS, MAX_SECRET_CHARS, PLACEMENT_KEY, SECRET_NAME, storedExtensionsSchema, type AppManifestWithData, type CollectionSpec, type DataProblem, type DataProblemCode, type DataProblemOptions, type HomeCardSize, type ManifestExtensions, type SecretSpec, } from './schema.js';
|
|
11
|
+
export { OPEN_LIMITS, OPEN_TYPES, choicesProblem, definitionOf, definitionTypeProblem, describeField, fieldTypeOf, hasChoices, isPlainOpenType, keyFromName, keyProblem, missingRequired, moveValue, openSchemaProblems, openSpec, openValueProblem, type Moved, type OpenField, type OpenFieldChange, type OpenFieldValue, type OpenProblem, type OpenProblemCode, type OpenSchemaFlag, type OpenType, } from './open-schema.js';
|
|
12
|
+
export { CONFIRM_LIMITS, confirmEmailProblems, confirmEmailSpec, type ConfirmEmailDeclared, type ConfirmEmailSpec, type ConfirmProblemCode, } from './confirm-email.js';
|
|
11
13
|
export { DOCUMENT_LIMITS } from './document-limits.js';
|
|
12
14
|
export { HOST_CAPABILITIES, KNOWN_HOST_GRANTS, isHostCapability, unknownHostGrant } from './grants.js';
|
|
13
15
|
export { diffSchemas, migrationProblems, migrationSchema, migrationStepSchema, migrationsSchema, publishedMigrationProblems, schemaOf, type Migration, type MigrationCode, type MigrationProblem, type MigrationStep, type SchemaChange, } from './migrations.js';
|
package/src/index.js
CHANGED
|
@@ -8,6 +8,8 @@ export { BRAND_FIELDS, BRAND_LIMITS, brandFilePath, brandPathsOf, brandProblems,
|
|
|
8
8
|
export { BUNDLE_MANIFEST, BUNDLE_MAX_BYTES, BUNDLE_PATH_MAX_CHARS, bundleBytes, bundleHash, brandOf, bundleProblem, codeOf, isBundlePath, isScriptPath, sizeOf, } from "./bundle.js";
|
|
9
9
|
export { COLOUR_TOKENS, coeditedFields, FIELD_LIMITS, RESERVED_FIELDS, describeType, isCoedited, isSearchable, isSortable, isStructured, parseFieldType, structuredFields, valueProblem, valueSchema, FieldTypeInvalid, } from "./field-types.js";
|
|
10
10
|
export { appManifestSchema, collectionOf, collectionsOf, dataProblems, effectivePlacementKey, HOME_CARD_SIZES, labelOf, manifestExtensionsSchema, newAppManifestSchema, SIZED_PLACEMENT_KINDS, MAX_APP_SECRETS, MAX_SECRET_CHARS, PLACEMENT_KEY, SECRET_NAME, storedExtensionsSchema, } from "./schema.js";
|
|
11
|
+
export { OPEN_LIMITS, OPEN_TYPES, choicesProblem, definitionOf, definitionTypeProblem, describeField, fieldTypeOf, hasChoices, isPlainOpenType, keyFromName, keyProblem, missingRequired, moveValue, openSchemaProblems, openSpec, openValueProblem, } from "./open-schema.js";
|
|
12
|
+
export { CONFIRM_LIMITS, confirmEmailProblems, confirmEmailSpec, } from "./confirm-email.js";
|
|
11
13
|
export { DOCUMENT_LIMITS } from "./document-limits.js";
|
|
12
14
|
export { HOST_CAPABILITIES, KNOWN_HOST_GRANTS, isHostCapability, unknownHostGrant } from "./grants.js";
|
|
13
15
|
export { diffSchemas, migrationProblems, migrationSchema, migrationStepSchema, migrationsSchema, publishedMigrationProblems, schemaOf, } from "./migrations.js";
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { type FieldType } from './field-types.js';
|
|
2
|
+
import type { CollectionSpec } from './schema.js';
|
|
3
|
+
/**
|
|
4
|
+
* Open-schema collections (P5).
|
|
5
|
+
*
|
|
6
|
+
* A collection flagged `openSchema: { fields: "columns", table?: "table" }`
|
|
7
|
+
* keeps the fields its manifest declares, and also the fields its companion
|
|
8
|
+
* collection's records define, per table. This file is the manifest half:
|
|
9
|
+
* which types a definition may name, what the companion must declare, how a
|
|
10
|
+
* definition reads as a `FieldType` the store already knows, and how a value
|
|
11
|
+
* moves when its field changes type. The store half is
|
|
12
|
+
* `apps/data/open-schema.ts`.
|
|
13
|
+
*/
|
|
14
|
+
/** The types a field definition may name. */
|
|
15
|
+
export declare const OPEN_TYPES: readonly ["text", "long_text", "number", "currency", "percent", "rating", "date", "datetime", "checkbox", "select", "multi_select", "person", "link", "url", "email", "phone", "attachment"];
|
|
16
|
+
export type OpenType = (typeof OPEN_TYPES)[number];
|
|
17
|
+
export declare const OPEN_LIMITS: {
|
|
18
|
+
/** Live rows one table may hold (DB-Q2, owner 30 Sep): measured at this size. */
|
|
19
|
+
readonly rowsPerTable: 50000;
|
|
20
|
+
/** Live field definitions one table may hold. */
|
|
21
|
+
readonly fieldsPerTable: 200;
|
|
22
|
+
/** The highest a rating may be. */
|
|
23
|
+
readonly ratingMax: 10;
|
|
24
|
+
};
|
|
25
|
+
/** The flag as the manifest writes it. */
|
|
26
|
+
export interface OpenSchemaFlag {
|
|
27
|
+
/** The companion collection whose records define the fields. */
|
|
28
|
+
fields: string;
|
|
29
|
+
/** A `string` field on both collections naming a row's or a definition's table. */
|
|
30
|
+
table?: string;
|
|
31
|
+
}
|
|
32
|
+
/** One live field, read from a companion record. */
|
|
33
|
+
export interface OpenField {
|
|
34
|
+
/** The companion record's id. */
|
|
35
|
+
id: string;
|
|
36
|
+
/** Stable across renames: what rows, tools, filters and sorts use. */
|
|
37
|
+
key: string;
|
|
38
|
+
name: string;
|
|
39
|
+
type: OpenType;
|
|
40
|
+
/** Null for a collection with no `table`. */
|
|
41
|
+
table: string | null;
|
|
42
|
+
choices: string[];
|
|
43
|
+
required: boolean;
|
|
44
|
+
currency?: string;
|
|
45
|
+
precision?: number;
|
|
46
|
+
linkTo?: string;
|
|
47
|
+
description?: string;
|
|
48
|
+
}
|
|
49
|
+
export type OpenProblemCode = 'data_open_fields_unknown' | 'data_open_fields_shape' | 'data_open_table_unknown';
|
|
50
|
+
export interface OpenProblem {
|
|
51
|
+
code: OpenProblemCode;
|
|
52
|
+
field?: string;
|
|
53
|
+
message: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Everything wrong with one collection's `openSchema`, given every
|
|
57
|
+
* collection's raw schema. Empty when it is right.
|
|
58
|
+
*/
|
|
59
|
+
export declare function openSchemaProblems(collection: string, flag: OpenSchemaFlag, data: Readonly<Record<string, {
|
|
60
|
+
schema: Record<string, unknown>;
|
|
61
|
+
openSchema?: OpenSchemaFlag;
|
|
62
|
+
}>>): OpenProblem[];
|
|
63
|
+
/** A definition's type as the store's own field type: what `checked` and the list path read. */
|
|
64
|
+
export declare function fieldTypeOf(field: OpenField): FieldType;
|
|
65
|
+
/** True for a type whose values a definition must list. */
|
|
66
|
+
export declare const hasChoices: (type: OpenType) => boolean;
|
|
67
|
+
/** A definition in a few words, for an answer or a tool: `Amount (deal_size): currency, USD`. */
|
|
68
|
+
export declare function describeField(field: OpenField): string;
|
|
69
|
+
/**
|
|
70
|
+
* A key made from a name: `"Deal size"` → `deal_size`, suffixed when taken.
|
|
71
|
+
* Always a field name the tools can use, never a reserved one.
|
|
72
|
+
*/
|
|
73
|
+
export declare function keyFromName(name: string, taken: ReadonlySet<string>): string;
|
|
74
|
+
/** Why a key cannot be a field's key here, or null. */
|
|
75
|
+
export declare function keyProblem(key: string, fixed: ReadonlySet<string>, taken: ReadonlySet<string>): string | null;
|
|
76
|
+
/** The choices a definition lists, checked: some, short, distinct. */
|
|
77
|
+
export declare function choicesProblem(type: OpenType, choices: readonly string[]): string | null;
|
|
78
|
+
/**
|
|
79
|
+
* What the store's own field type cannot say about an open value: a web
|
|
80
|
+
* address, an email, a rating's range, a date with no time, the choices of a
|
|
81
|
+
* multi-select. Null when it is fine; `checked` then checks the rest.
|
|
82
|
+
*/
|
|
83
|
+
export declare function openValueProblem(field: OpenField, value: unknown): string | null;
|
|
84
|
+
/** True when a create leaves a required field out. */
|
|
85
|
+
export declare const missingRequired: (field: OpenField, value: unknown) => boolean;
|
|
86
|
+
/** What happened to one value when its field changed. */
|
|
87
|
+
export type Moved = {
|
|
88
|
+
outcome: 'kept' | 'converted';
|
|
89
|
+
value: unknown;
|
|
90
|
+
} | {
|
|
91
|
+
outcome: 'cleared';
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* One value moved from a field's old definition to its new one (P5 retype):
|
|
95
|
+
* kept when it is still right, converted when it can be read as the new type,
|
|
96
|
+
* else cleared. Never invents a choice.
|
|
97
|
+
*/
|
|
98
|
+
export declare function moveValue(value: unknown, from: OpenField, to: OpenField): Moved;
|
|
99
|
+
/**
|
|
100
|
+
* What a field definition's change did to its table's rows: counts, never
|
|
101
|
+
* values. `rows` is how many held a value for the field. A definition's
|
|
102
|
+
* `update` or `delete` answer carries it as `fieldChange` when values moved.
|
|
103
|
+
*/
|
|
104
|
+
export interface OpenFieldChange {
|
|
105
|
+
field: string;
|
|
106
|
+
op: 'retype' | 'choices' | 'delete';
|
|
107
|
+
rows: number;
|
|
108
|
+
kept: number;
|
|
109
|
+
converted: number;
|
|
110
|
+
cleared: number;
|
|
111
|
+
}
|
|
112
|
+
/** One value an open field may hold, as a row hands it out. */
|
|
113
|
+
export type OpenFieldValue = string | number | boolean | string[] | null;
|
|
114
|
+
/** Open types whose values are kept in the plain column: filtered, and all but the two lists sorted. */
|
|
115
|
+
export declare const isPlainOpenType: (type: OpenType) => boolean;
|
|
116
|
+
/**
|
|
117
|
+
* One companion record as a definition, or null when it cannot be one.
|
|
118
|
+
* `table` is the field naming its table, if the collection has tables.
|
|
119
|
+
*/
|
|
120
|
+
export declare function definitionOf(id: string, body: Record<string, unknown>, table: string | null): OpenField | null;
|
|
121
|
+
/**
|
|
122
|
+
* The collection's spec with one table's live fields folded in: what a row
|
|
123
|
+
* is checked against and what a list filters and sorts on. A fixed field
|
|
124
|
+
* always wins over a definition of the same key.
|
|
125
|
+
*/
|
|
126
|
+
export declare function openSpec(base: CollectionSpec, fields: readonly OpenField[]): CollectionSpec;
|
|
127
|
+
/** Why a definition's type and choices don't go together, or null. */
|
|
128
|
+
export declare function definitionTypeProblem(body: Record<string, unknown>): string | null;
|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
// A copy of Brydio's `apps/api/src/apps/manifest/open-schema.ts`, kept exact so
|
|
2
|
+
// a companion the SDK accepts is one the server accepts, and a value moved by
|
|
3
|
+
// the fake host is moved as the real store moves it. Only the part after
|
|
4
|
+
// "The SDK's own" at the end is not the server's.
|
|
5
|
+
import { FIELD_LIMITS, FIELD_NAME, isSortable, isStructured, parseFieldType, quoted, RESERVED_FIELDS, } from "./field-types.js";
|
|
6
|
+
/**
|
|
7
|
+
* Open-schema collections (P5).
|
|
8
|
+
*
|
|
9
|
+
* A collection flagged `openSchema: { fields: "columns", table?: "table" }`
|
|
10
|
+
* keeps the fields its manifest declares, and also the fields its companion
|
|
11
|
+
* collection's records define, per table. This file is the manifest half:
|
|
12
|
+
* which types a definition may name, what the companion must declare, how a
|
|
13
|
+
* definition reads as a `FieldType` the store already knows, and how a value
|
|
14
|
+
* moves when its field changes type. The store half is
|
|
15
|
+
* `apps/data/open-schema.ts`.
|
|
16
|
+
*/
|
|
17
|
+
/** The types a field definition may name. */
|
|
18
|
+
export const OPEN_TYPES = [
|
|
19
|
+
'text',
|
|
20
|
+
'long_text',
|
|
21
|
+
'number',
|
|
22
|
+
'currency',
|
|
23
|
+
'percent',
|
|
24
|
+
'rating',
|
|
25
|
+
'date',
|
|
26
|
+
'datetime',
|
|
27
|
+
'checkbox',
|
|
28
|
+
'select',
|
|
29
|
+
'multi_select',
|
|
30
|
+
'person',
|
|
31
|
+
'link',
|
|
32
|
+
'url',
|
|
33
|
+
'email',
|
|
34
|
+
'phone',
|
|
35
|
+
'attachment',
|
|
36
|
+
];
|
|
37
|
+
export const OPEN_LIMITS = {
|
|
38
|
+
/** Live rows one table may hold (DB-Q2, owner 30 Sep): measured at this size. */
|
|
39
|
+
rowsPerTable: 50_000,
|
|
40
|
+
/** Live field definitions one table may hold. */
|
|
41
|
+
fieldsPerTable: 200,
|
|
42
|
+
/** The highest a rating may be. */
|
|
43
|
+
ratingMax: 10,
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* What the companion must declare: the three it must have, and the kinds of
|
|
47
|
+
* the ones the host reads when they are there. Anything else is the app's own.
|
|
48
|
+
*/
|
|
49
|
+
const REQUIRED_COMPANION = { key: 'string', name: 'string', type: 'enum' };
|
|
50
|
+
const OPTIONAL_COMPANION = {
|
|
51
|
+
choices: ['string[]'],
|
|
52
|
+
required: ['boolean'],
|
|
53
|
+
currency: ['string'],
|
|
54
|
+
precision: ['number'],
|
|
55
|
+
linkTo: ['string'],
|
|
56
|
+
description: ['string', 'text'],
|
|
57
|
+
};
|
|
58
|
+
const kindOf = (raw) => {
|
|
59
|
+
try {
|
|
60
|
+
return parseFieldType(raw);
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// Its own problem is reported by the field check; nothing to add here.
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Everything wrong with one collection's `openSchema`, given every
|
|
69
|
+
* collection's raw schema. Empty when it is right.
|
|
70
|
+
*/
|
|
71
|
+
export function openSchemaProblems(collection, flag, data) {
|
|
72
|
+
const companion = data[flag.fields];
|
|
73
|
+
if (!companion || flag.fields === collection) {
|
|
74
|
+
return [
|
|
75
|
+
{
|
|
76
|
+
code: 'data_open_fields_unknown',
|
|
77
|
+
message: `${collection}.openSchema.fields names ${flag.fields}, which must be another collection of this app.`,
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
if (companion.openSchema) {
|
|
82
|
+
return [
|
|
83
|
+
{
|
|
84
|
+
code: 'data_open_fields_unknown',
|
|
85
|
+
message: `${flag.fields} defines ${collection}'s fields, so it cannot have open fields itself.`,
|
|
86
|
+
},
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
const problems = [];
|
|
90
|
+
const shape = `${flag.fields} defines ${collection}'s fields`;
|
|
91
|
+
for (const [field, kind] of Object.entries(REQUIRED_COMPANION)) {
|
|
92
|
+
const type = field in companion.schema ? kindOf(companion.schema[field]) : null;
|
|
93
|
+
if (!type || type.kind !== kind || (field !== 'key' && type.optional)) {
|
|
94
|
+
problems.push({
|
|
95
|
+
code: 'data_open_fields_shape',
|
|
96
|
+
field,
|
|
97
|
+
message: kind === 'enum'
|
|
98
|
+
? `${shape}, so it needs type: a choice of some of ${quoted(OPEN_TYPES)}.`
|
|
99
|
+
: `${shape}, so it needs ${field}: "${kind}${field === 'key' ? '?' : ''}".`,
|
|
100
|
+
});
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (kind === 'enum') {
|
|
104
|
+
const unknown = (type.values ?? []).find(value => !OPEN_TYPES.includes(value));
|
|
105
|
+
if (unknown) {
|
|
106
|
+
problems.push({
|
|
107
|
+
code: 'data_open_fields_shape',
|
|
108
|
+
field,
|
|
109
|
+
message: `${flag.fields}.type offers "${unknown}", which is not an open field type; use some of ${quoted(OPEN_TYPES)}.`,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
for (const [field, kinds] of Object.entries(OPTIONAL_COMPANION)) {
|
|
115
|
+
if (!(field in companion.schema))
|
|
116
|
+
continue;
|
|
117
|
+
const type = kindOf(companion.schema[field]);
|
|
118
|
+
if (type && !kinds.includes(type.kind)) {
|
|
119
|
+
problems.push({
|
|
120
|
+
code: 'data_open_fields_shape',
|
|
121
|
+
field,
|
|
122
|
+
message: `${flag.fields}.${field} is read by Brydio as ${kinds.join(' or ')}; declare it so.`,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
if (flag.table !== undefined) {
|
|
127
|
+
for (const [name, schema] of [
|
|
128
|
+
[collection, data[collection]?.schema ?? {}],
|
|
129
|
+
[flag.fields, companion.schema],
|
|
130
|
+
]) {
|
|
131
|
+
const type = flag.table in schema ? kindOf(schema[flag.table]) : null;
|
|
132
|
+
if (!type || type.kind !== 'string' || type.optional) {
|
|
133
|
+
problems.push({
|
|
134
|
+
code: 'data_open_table_unknown',
|
|
135
|
+
field: flag.table,
|
|
136
|
+
message: `${collection}.openSchema.table is ${flag.table}, so ${name} needs ${flag.table}: "string", naming each record's table.`,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return problems;
|
|
142
|
+
}
|
|
143
|
+
// ---------------------------------------------------------------------------
|
|
144
|
+
// Definitions
|
|
145
|
+
/** A definition's type as the store's own field type: what `checked` and the list path read. */
|
|
146
|
+
export function fieldTypeOf(field) {
|
|
147
|
+
switch (field.type) {
|
|
148
|
+
case 'text':
|
|
149
|
+
case 'url':
|
|
150
|
+
case 'email':
|
|
151
|
+
case 'phone':
|
|
152
|
+
return { kind: 'string', optional: true };
|
|
153
|
+
case 'long_text':
|
|
154
|
+
return { kind: 'text', optional: true };
|
|
155
|
+
case 'number':
|
|
156
|
+
case 'currency':
|
|
157
|
+
case 'percent':
|
|
158
|
+
case 'rating':
|
|
159
|
+
return { kind: 'number', optional: true };
|
|
160
|
+
case 'date':
|
|
161
|
+
case 'datetime':
|
|
162
|
+
return { kind: 'date', optional: true };
|
|
163
|
+
case 'checkbox':
|
|
164
|
+
return { kind: 'boolean', optional: true };
|
|
165
|
+
case 'select':
|
|
166
|
+
return { kind: 'enum', optional: true, values: field.choices };
|
|
167
|
+
case 'multi_select':
|
|
168
|
+
return { kind: 'string[]', optional: true, plain: true, values: field.choices };
|
|
169
|
+
case 'person':
|
|
170
|
+
return { kind: 'member', optional: true };
|
|
171
|
+
case 'link':
|
|
172
|
+
return { kind: 'string[]', optional: true, plain: true };
|
|
173
|
+
case 'attachment':
|
|
174
|
+
return { kind: 'string[]', optional: true };
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/** True for a type whose values a definition must list. */
|
|
178
|
+
export const hasChoices = (type) => type === 'select' || type === 'multi_select';
|
|
179
|
+
/** A definition in a few words, for an answer or a tool: `Amount (deal_size): currency, USD`. */
|
|
180
|
+
export function describeField(field) {
|
|
181
|
+
const extra = hasChoices(field.type)
|
|
182
|
+
? `, one of ${quoted(field.choices)}`
|
|
183
|
+
: field.type === 'currency' && field.currency
|
|
184
|
+
? `, ${field.currency}`
|
|
185
|
+
: '';
|
|
186
|
+
return `${field.key} ("${field.name}"): ${field.type.replace('_', ' ')}${extra}${field.required ? ', required' : ''}`;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* A key made from a name: `"Deal size"` → `deal_size`, suffixed when taken.
|
|
190
|
+
* Always a field name the tools can use, never a reserved one.
|
|
191
|
+
*/
|
|
192
|
+
export function keyFromName(name, taken) {
|
|
193
|
+
const slug = name
|
|
194
|
+
.normalize('NFKD')
|
|
195
|
+
.replace(/[̀-ͯ]/g, '')
|
|
196
|
+
.toLowerCase()
|
|
197
|
+
.replace(/[^a-z0-9]+/g, '_')
|
|
198
|
+
.replace(/^_+|_+$/g, '')
|
|
199
|
+
.slice(0, FIELD_LIMITS.nameChars - 4) || 'field';
|
|
200
|
+
const base = /^[a-z]/.test(slug) ? slug : `f_${slug}`.slice(0, FIELD_LIMITS.nameChars - 4);
|
|
201
|
+
const free = (key) => !taken.has(key) && !RESERVED_FIELDS.has(key);
|
|
202
|
+
if (free(base))
|
|
203
|
+
return base;
|
|
204
|
+
for (let n = 2;; n += 1) {
|
|
205
|
+
if (free(`${base}_${n}`))
|
|
206
|
+
return `${base}_${n}`;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
/** Why a key cannot be a field's key here, or null. */
|
|
210
|
+
export function keyProblem(key, fixed, taken) {
|
|
211
|
+
if (!FIELD_NAME.test(key) || key.length > FIELD_LIMITS.nameChars) {
|
|
212
|
+
return `key "${key}" must be letters, digits and _, starting with a lower-case letter, at most ${FIELD_LIMITS.nameChars} characters.`;
|
|
213
|
+
}
|
|
214
|
+
if (RESERVED_FIELDS.has(key) || fixed.has(key))
|
|
215
|
+
return `key "${key}" is a field Brydio or the app already keeps.`;
|
|
216
|
+
if (taken.has(key))
|
|
217
|
+
return `key "${key}" is already a field of this table.`;
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
/** The choices a definition lists, checked: some, short, distinct. */
|
|
221
|
+
export function choicesProblem(type, choices) {
|
|
222
|
+
if (!hasChoices(type))
|
|
223
|
+
return null;
|
|
224
|
+
if (!choices.length)
|
|
225
|
+
return `A ${type.replace('_', ' ')} field needs at least one choice.`;
|
|
226
|
+
if (choices.length > FIELD_LIMITS.enumValues)
|
|
227
|
+
return `A field may offer at most ${FIELD_LIMITS.enumValues} choices.`;
|
|
228
|
+
if (choices.some(choice => !choice.trim() || choice.length > FIELD_LIMITS.enumValueChars)) {
|
|
229
|
+
return `Each choice is 1 to ${FIELD_LIMITS.enumValueChars} characters.`;
|
|
230
|
+
}
|
|
231
|
+
if (new Set(choices).size !== choices.length)
|
|
232
|
+
return 'A field offers the same choice twice.';
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
// ---------------------------------------------------------------------------
|
|
236
|
+
// Values
|
|
237
|
+
const DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
|
|
238
|
+
const DATE_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d{1,6})?)?(Z|[+-]\d{2}:\d{2})$/;
|
|
239
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
240
|
+
const PHONE = /^[+()0-9 .\-/]{3,40}$/;
|
|
241
|
+
const isEmpty = (value) => value === undefined || value === null || value === '' || (Array.isArray(value) && value.length === 0);
|
|
242
|
+
/**
|
|
243
|
+
* What the store's own field type cannot say about an open value: a web
|
|
244
|
+
* address, an email, a rating's range, a date with no time, the choices of a
|
|
245
|
+
* multi-select. Null when it is fine; `checked` then checks the rest.
|
|
246
|
+
*/
|
|
247
|
+
export function openValueProblem(field, value) {
|
|
248
|
+
if (value === null || value === undefined)
|
|
249
|
+
return null;
|
|
250
|
+
const named = `${field.key} ("${field.name}")`;
|
|
251
|
+
switch (field.type) {
|
|
252
|
+
case 'url':
|
|
253
|
+
return typeof value === 'string' && isWebAddress(value) ? null : `${named} must be a web address, starting http:// or https://.`;
|
|
254
|
+
case 'email':
|
|
255
|
+
return typeof value === 'string' && EMAIL.test(value) ? null : `${named} must be an email address.`;
|
|
256
|
+
case 'phone':
|
|
257
|
+
return typeof value === 'string' && PHONE.test(value) ? null : `${named} must be a phone number.`;
|
|
258
|
+
case 'rating':
|
|
259
|
+
return Number.isInteger(value) && value >= 0 && value <= OPEN_LIMITS.ratingMax
|
|
260
|
+
? null
|
|
261
|
+
: `${named} must be a whole number from 0 to ${OPEN_LIMITS.ratingMax}.`;
|
|
262
|
+
case 'date':
|
|
263
|
+
return typeof value === 'string' && DATE_ONLY.test(value) && !Number.isNaN(Date.parse(value))
|
|
264
|
+
? null
|
|
265
|
+
: `${named} must be a date, written YYYY-MM-DD.`;
|
|
266
|
+
case 'datetime':
|
|
267
|
+
return typeof value === 'string' && DATE_TIME.test(value) && !Number.isNaN(Date.parse(value))
|
|
268
|
+
? null
|
|
269
|
+
: `${named} must be a date and time, written YYYY-MM-DDTHH:MM:SSZ.`;
|
|
270
|
+
case 'multi_select': {
|
|
271
|
+
if (!Array.isArray(value))
|
|
272
|
+
return null;
|
|
273
|
+
const wrong = value.find(one => typeof one !== 'string' || !field.choices.includes(one));
|
|
274
|
+
if (wrong !== undefined)
|
|
275
|
+
return `${named} may hold only ${quoted(field.choices)}.`;
|
|
276
|
+
return new Set(value).size === value.length ? null : `${named} names the same choice twice.`;
|
|
277
|
+
}
|
|
278
|
+
default:
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/** True when a create leaves a required field out. */
|
|
283
|
+
export const missingRequired = (field, value) => field.required && isEmpty(value);
|
|
284
|
+
function isWebAddress(value) {
|
|
285
|
+
try {
|
|
286
|
+
const url = new URL(value);
|
|
287
|
+
return (url.protocol === 'http:' || url.protocol === 'https:') && value.length <= FIELD_LIMITS.stringChars;
|
|
288
|
+
}
|
|
289
|
+
catch {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const textOf = (value) => {
|
|
294
|
+
if (typeof value === 'string')
|
|
295
|
+
return value;
|
|
296
|
+
if (typeof value === 'number' && Number.isFinite(value))
|
|
297
|
+
return String(value);
|
|
298
|
+
if (typeof value === 'boolean')
|
|
299
|
+
return value ? 'true' : 'false';
|
|
300
|
+
if (Array.isArray(value))
|
|
301
|
+
return value.map(one => textOf(one) ?? '').filter(Boolean).join(', ');
|
|
302
|
+
return null;
|
|
303
|
+
};
|
|
304
|
+
const choiceOf = (choices, value) => choices.find(choice => choice === value) ?? choices.find(choice => choice.toLowerCase() === value.trim().toLowerCase());
|
|
305
|
+
/**
|
|
306
|
+
* One value moved from a field's old definition to its new one (P5 retype):
|
|
307
|
+
* kept when it is still right, converted when it can be read as the new type,
|
|
308
|
+
* else cleared. Never invents a choice.
|
|
309
|
+
*/
|
|
310
|
+
export function moveValue(value, from, to) {
|
|
311
|
+
const moved = convert(value, from, to);
|
|
312
|
+
if (moved === undefined || isEmpty(moved))
|
|
313
|
+
return { outcome: 'cleared' };
|
|
314
|
+
const settled = openValueProblem(to, moved) === null;
|
|
315
|
+
if (!settled)
|
|
316
|
+
return { outcome: 'cleared' };
|
|
317
|
+
return { outcome: JSON.stringify(moved) === JSON.stringify(value) ? 'kept' : 'converted', value: moved };
|
|
318
|
+
}
|
|
319
|
+
function convert(value, from, to) {
|
|
320
|
+
const text = textOf(value);
|
|
321
|
+
switch (to.type) {
|
|
322
|
+
case 'text':
|
|
323
|
+
return text === null ? undefined : text.slice(0, FIELD_LIMITS.stringChars);
|
|
324
|
+
case 'long_text':
|
|
325
|
+
return text === null ? undefined : text.slice(0, FIELD_LIMITS.textChars);
|
|
326
|
+
case 'url':
|
|
327
|
+
case 'email':
|
|
328
|
+
case 'phone':
|
|
329
|
+
return text === null ? undefined : text.trim();
|
|
330
|
+
case 'number':
|
|
331
|
+
case 'currency':
|
|
332
|
+
case 'percent':
|
|
333
|
+
case 'rating': {
|
|
334
|
+
const number = typeof value === 'number'
|
|
335
|
+
? value
|
|
336
|
+
: typeof value === 'boolean'
|
|
337
|
+
? Number(value)
|
|
338
|
+
: typeof value === 'string' && value.trim()
|
|
339
|
+
? Number(value.trim().replace(/[\s,%$€£¥]/g, ''))
|
|
340
|
+
: Number.NaN;
|
|
341
|
+
if (!Number.isFinite(number))
|
|
342
|
+
return undefined;
|
|
343
|
+
return to.type === 'rating' ? Math.round(number) : number;
|
|
344
|
+
}
|
|
345
|
+
case 'checkbox':
|
|
346
|
+
if (typeof value === 'boolean')
|
|
347
|
+
return value;
|
|
348
|
+
if (typeof value === 'number')
|
|
349
|
+
return value !== 0;
|
|
350
|
+
if (typeof value === 'string') {
|
|
351
|
+
if (/^(true|yes|y|1|x|checked|done)$/i.test(value.trim()))
|
|
352
|
+
return true;
|
|
353
|
+
if (/^(false|no|n|0|unchecked)$/i.test(value.trim()))
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
return undefined;
|
|
357
|
+
case 'date':
|
|
358
|
+
case 'datetime': {
|
|
359
|
+
if (typeof value !== 'string')
|
|
360
|
+
return undefined;
|
|
361
|
+
const head = value.trim();
|
|
362
|
+
if (to.type === 'date' && /^\d{4}-\d{2}-\d{2}/.test(head))
|
|
363
|
+
return head.slice(0, 10);
|
|
364
|
+
const at = Date.parse(DATE_ONLY.test(head) ? `${head}T00:00:00Z` : head);
|
|
365
|
+
if (Number.isNaN(at))
|
|
366
|
+
return undefined;
|
|
367
|
+
return to.type === 'date' ? new Date(at).toISOString().slice(0, 10) : new Date(at).toISOString();
|
|
368
|
+
}
|
|
369
|
+
case 'select': {
|
|
370
|
+
const candidates = Array.isArray(value) ? value : [text ?? ''];
|
|
371
|
+
for (const one of candidates) {
|
|
372
|
+
const found = typeof one === 'string' ? choiceOf(to.choices, one) : undefined;
|
|
373
|
+
if (found)
|
|
374
|
+
return found;
|
|
375
|
+
}
|
|
376
|
+
return undefined;
|
|
377
|
+
}
|
|
378
|
+
case 'multi_select': {
|
|
379
|
+
const candidates = Array.isArray(value) ? value : (text ?? '').split(',');
|
|
380
|
+
const found = candidates
|
|
381
|
+
.map(one => (typeof one === 'string' ? choiceOf(to.choices, one) : undefined))
|
|
382
|
+
.filter((one) => Boolean(one));
|
|
383
|
+
return [...new Set(found)];
|
|
384
|
+
}
|
|
385
|
+
case 'person':
|
|
386
|
+
return from.type === 'person' ? value : undefined;
|
|
387
|
+
case 'link':
|
|
388
|
+
case 'attachment':
|
|
389
|
+
return from.type === to.type ? value : undefined;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
/** Open types whose values are kept in the plain column: filtered, and all but the two lists sorted. */
|
|
393
|
+
export const isPlainOpenType = (type) => !['text', 'long_text', 'url', 'email', 'phone', 'attachment'].includes(type);
|
|
394
|
+
/**
|
|
395
|
+
* One companion record as a definition, or null when it cannot be one.
|
|
396
|
+
* `table` is the field naming its table, if the collection has tables.
|
|
397
|
+
*/
|
|
398
|
+
export function definitionOf(id, body, table) {
|
|
399
|
+
const { key, name, type } = body;
|
|
400
|
+
if (typeof key !== 'string' || typeof name !== 'string' || !OPEN_TYPES.includes(type)) {
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
const tableValue = table ? body[table] : null;
|
|
404
|
+
if (table && typeof tableValue !== 'string')
|
|
405
|
+
return null;
|
|
406
|
+
return {
|
|
407
|
+
id,
|
|
408
|
+
key,
|
|
409
|
+
name,
|
|
410
|
+
type: type,
|
|
411
|
+
table: tableValue ?? null,
|
|
412
|
+
choices: Array.isArray(body.choices) ? body.choices.filter((one) => typeof one === 'string') : [],
|
|
413
|
+
required: body.required === true,
|
|
414
|
+
...(typeof body.currency === 'string' ? { currency: body.currency } : {}),
|
|
415
|
+
...(typeof body.precision === 'number' ? { precision: body.precision } : {}),
|
|
416
|
+
...(typeof body.linkTo === 'string' ? { linkTo: body.linkTo } : {}),
|
|
417
|
+
...(typeof body.description === 'string' ? { description: body.description } : {}),
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* The collection's spec with one table's live fields folded in: what a row
|
|
422
|
+
* is checked against and what a list filters and sorts on. A fixed field
|
|
423
|
+
* always wins over a definition of the same key.
|
|
424
|
+
*/
|
|
425
|
+
export function openSpec(base, fields) {
|
|
426
|
+
const added = fields.filter(field => !base.fields[field.key]);
|
|
427
|
+
const types = Object.fromEntries(added.map(field => [field.key, fieldTypeOf(field)]));
|
|
428
|
+
const keys = added.map(field => field.key);
|
|
429
|
+
return {
|
|
430
|
+
...base,
|
|
431
|
+
fields: { ...base.fields, ...types },
|
|
432
|
+
structured: [...base.structured, ...keys.filter(key => isStructured(types[key]))],
|
|
433
|
+
sortable: [...base.sortable, ...keys.filter(key => isSortable(types[key]))],
|
|
434
|
+
search: [...base.search, ...added.filter(field => field.type === 'text' || field.type === 'long_text').map(field => field.key)],
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
/** Why a definition's type and choices don't go together, or null. */
|
|
438
|
+
export function definitionTypeProblem(body) {
|
|
439
|
+
const type = body.type;
|
|
440
|
+
if (typeof type !== 'string' || !OPEN_TYPES.includes(type))
|
|
441
|
+
return null;
|
|
442
|
+
const choices = Array.isArray(body.choices) ? body.choices.filter((one) => typeof one === 'string') : [];
|
|
443
|
+
return choicesProblem(type, choices);
|
|
444
|
+
}
|
package/src/schema.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
import { FieldTypeInvalid, type FieldType } from './field-types.js';
|
|
3
|
+
import { type OpenProblemCode } from './open-schema.js';
|
|
4
|
+
import { type ConfirmProblemCode } from './confirm-email.js';
|
|
3
5
|
export { COLOUR_TOKENS, FIELD_LIMITS, parseFieldType, RESERVED_FIELDS, structuredFields, type FieldKind, type FieldType, } from './field-types.js';
|
|
4
6
|
/**
|
|
5
7
|
* What an app adds to `.brydio/app.json` to be more than a bundle of servers
|
|
@@ -143,8 +145,18 @@ declare const extensionShape: {
|
|
|
143
145
|
schema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
144
146
|
search: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
145
147
|
label: z.ZodOptional<z.ZodString>;
|
|
148
|
+
openSchema: z.ZodOptional<z.ZodObject<{
|
|
149
|
+
fields: z.ZodString;
|
|
150
|
+
table: z.ZodOptional<z.ZodString>;
|
|
151
|
+
}, z.core.$strict>>;
|
|
146
152
|
publicRead: z.ZodOptional<z.ZodBoolean>;
|
|
147
153
|
publicSubmit: z.ZodOptional<z.ZodBoolean>;
|
|
154
|
+
confirmEmail: z.ZodOptional<z.ZodObject<{
|
|
155
|
+
field: z.ZodString;
|
|
156
|
+
subject: z.ZodOptional<z.ZodString>;
|
|
157
|
+
message: z.ZodOptional<z.ZodString>;
|
|
158
|
+
link: z.ZodOptional<z.ZodBoolean>;
|
|
159
|
+
}, z.core.$strict>>;
|
|
148
160
|
}, z.core.$strip>>>;
|
|
149
161
|
tools: z.ZodOptional<z.ZodObject<{
|
|
150
162
|
generated: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -223,7 +235,7 @@ export interface DataProblem {
|
|
|
223
235
|
field?: string;
|
|
224
236
|
message: string;
|
|
225
237
|
}
|
|
226
|
-
export type DataProblemCode = FieldTypeInvalid['code'] | 'data_too_many_collections' | 'data_too_many_fields' | 'data_collection_name_format' | 'data_field_name_format' | 'data_field_reserved' | 'data_label_format' | 'data_label_taken' | 'data_project_field_twice' | 'data_search_unknown_field' | 'data_search_not_text' | 'grant_collection_missing' | 'grant_tool_missing' | 'custom_name_taken' | 'custom_collection_unknown' | 'custom_input_invalid' | 'placement_key_taken' | 'placement_screen_unknown' | 'placement_home_sizes' | 'placement_sizes_not_home' | 'placement_widget_sizes' | 'placement_tab_retired' | 'placement_public_shape' | 'placement_public_nothing' | 'placement_children_too_deep' | 'placement_create_tool_unknown' | 'placement_create_not_write' | 'secret_name_taken' | 'grant_secrets_missing';
|
|
238
|
+
export type DataProblemCode = FieldTypeInvalid['code'] | OpenProblemCode | ConfirmProblemCode | 'data_too_many_collections' | 'data_too_many_fields' | 'data_collection_name_format' | 'data_field_name_format' | 'data_field_reserved' | 'data_label_format' | 'data_label_taken' | 'data_project_field_twice' | 'data_search_unknown_field' | 'data_search_not_text' | 'grant_collection_missing' | 'grant_tool_missing' | 'custom_name_taken' | 'custom_collection_unknown' | 'custom_input_invalid' | 'placement_key_taken' | 'placement_screen_unknown' | 'placement_home_sizes' | 'placement_sizes_not_home' | 'placement_widget_sizes' | 'placement_tab_retired' | 'placement_public_shape' | 'placement_public_nothing' | 'placement_children_too_deep' | 'placement_create_tool_unknown' | 'placement_create_not_write' | 'secret_name_taken' | 'grant_secrets_missing';
|
|
227
239
|
type Additions = z.infer<z.ZodObject<typeof extensionShape>>;
|
|
228
240
|
/**
|
|
229
241
|
* Everything wrong with an app's additions that a type cannot say.
|
|
@@ -294,8 +306,18 @@ export declare const manifestExtensionsSchema: z.ZodObject<{
|
|
|
294
306
|
schema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
295
307
|
search: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
296
308
|
label: z.ZodOptional<z.ZodString>;
|
|
309
|
+
openSchema: z.ZodOptional<z.ZodObject<{
|
|
310
|
+
fields: z.ZodString;
|
|
311
|
+
table: z.ZodOptional<z.ZodString>;
|
|
312
|
+
}, z.core.$strict>>;
|
|
297
313
|
publicRead: z.ZodOptional<z.ZodBoolean>;
|
|
298
314
|
publicSubmit: z.ZodOptional<z.ZodBoolean>;
|
|
315
|
+
confirmEmail: z.ZodOptional<z.ZodObject<{
|
|
316
|
+
field: z.ZodString;
|
|
317
|
+
subject: z.ZodOptional<z.ZodString>;
|
|
318
|
+
message: z.ZodOptional<z.ZodString>;
|
|
319
|
+
link: z.ZodOptional<z.ZodBoolean>;
|
|
320
|
+
}, z.core.$strict>>;
|
|
299
321
|
}, z.core.$strip>>>;
|
|
300
322
|
tools: z.ZodOptional<z.ZodObject<{
|
|
301
323
|
generated: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -415,8 +437,18 @@ export declare const storedExtensionsSchema: z.ZodObject<{
|
|
|
415
437
|
schema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
416
438
|
search: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
417
439
|
label: z.ZodOptional<z.ZodString>;
|
|
440
|
+
openSchema: z.ZodOptional<z.ZodObject<{
|
|
441
|
+
fields: z.ZodString;
|
|
442
|
+
table: z.ZodOptional<z.ZodString>;
|
|
443
|
+
}, z.core.$strict>>;
|
|
418
444
|
publicRead: z.ZodOptional<z.ZodBoolean>;
|
|
419
445
|
publicSubmit: z.ZodOptional<z.ZodBoolean>;
|
|
446
|
+
confirmEmail: z.ZodOptional<z.ZodObject<{
|
|
447
|
+
field: z.ZodString;
|
|
448
|
+
subject: z.ZodOptional<z.ZodString>;
|
|
449
|
+
message: z.ZodOptional<z.ZodString>;
|
|
450
|
+
link: z.ZodOptional<z.ZodBoolean>;
|
|
451
|
+
}, z.core.$strict>>;
|
|
420
452
|
}, z.core.$strip>>>;
|
|
421
453
|
tools: z.ZodOptional<z.ZodObject<{
|
|
422
454
|
generated: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -571,8 +603,18 @@ export declare const appManifestSchema: z.ZodObject<{
|
|
|
571
603
|
schema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
572
604
|
search: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
573
605
|
label: z.ZodOptional<z.ZodString>;
|
|
606
|
+
openSchema: z.ZodOptional<z.ZodObject<{
|
|
607
|
+
fields: z.ZodString;
|
|
608
|
+
table: z.ZodOptional<z.ZodString>;
|
|
609
|
+
}, z.core.$strict>>;
|
|
574
610
|
publicRead: z.ZodOptional<z.ZodBoolean>;
|
|
575
611
|
publicSubmit: z.ZodOptional<z.ZodBoolean>;
|
|
612
|
+
confirmEmail: z.ZodOptional<z.ZodObject<{
|
|
613
|
+
field: z.ZodString;
|
|
614
|
+
subject: z.ZodOptional<z.ZodString>;
|
|
615
|
+
message: z.ZodOptional<z.ZodString>;
|
|
616
|
+
link: z.ZodOptional<z.ZodBoolean>;
|
|
617
|
+
}, z.core.$strict>>;
|
|
576
618
|
}, z.core.$strip>>>;
|
|
577
619
|
tools: z.ZodOptional<z.ZodObject<{
|
|
578
620
|
generated: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -731,8 +773,18 @@ export declare const newAppManifestSchema: z.ZodObject<{
|
|
|
731
773
|
schema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
|
|
732
774
|
search: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
733
775
|
label: z.ZodOptional<z.ZodString>;
|
|
776
|
+
openSchema: z.ZodOptional<z.ZodObject<{
|
|
777
|
+
fields: z.ZodString;
|
|
778
|
+
table: z.ZodOptional<z.ZodString>;
|
|
779
|
+
}, z.core.$strict>>;
|
|
734
780
|
publicRead: z.ZodOptional<z.ZodBoolean>;
|
|
735
781
|
publicSubmit: z.ZodOptional<z.ZodBoolean>;
|
|
782
|
+
confirmEmail: z.ZodOptional<z.ZodObject<{
|
|
783
|
+
field: z.ZodString;
|
|
784
|
+
subject: z.ZodOptional<z.ZodString>;
|
|
785
|
+
message: z.ZodOptional<z.ZodString>;
|
|
786
|
+
link: z.ZodOptional<z.ZodBoolean>;
|
|
787
|
+
}, z.core.$strict>>;
|
|
736
788
|
}, z.core.$strip>>>;
|
|
737
789
|
tools: z.ZodOptional<z.ZodObject<{
|
|
738
790
|
generated: z.ZodOptional<z.ZodBoolean>;
|
|
@@ -812,6 +864,17 @@ export interface CollectionSpec {
|
|
|
812
864
|
search: string[];
|
|
813
865
|
/** The one `project` field, whose value is copied to `app_document.project_id`. */
|
|
814
866
|
projectField: string | null;
|
|
867
|
+
/**
|
|
868
|
+
* Fields defined at runtime (P5): the companion collection, and the field
|
|
869
|
+
* naming a row's table (null: one table per instance). The store reads the
|
|
870
|
+
* live definitions; `fields` here holds only the manifest's own.
|
|
871
|
+
*/
|
|
872
|
+
openSchema?: {
|
|
873
|
+
fields: string;
|
|
874
|
+
table: string | null;
|
|
875
|
+
};
|
|
876
|
+
/** On a companion: the open collection whose fields its records define (P5). */
|
|
877
|
+
definesFieldsOf?: string;
|
|
815
878
|
}
|
|
816
879
|
/**
|
|
817
880
|
* The singular a collection's tools are named with: the manifest's, else the
|
package/src/schema.js
CHANGED
|
@@ -2,6 +2,8 @@ import { z } from 'zod';
|
|
|
2
2
|
import { MANIFEST_LIMITS, SEMVER_FORMAT, baseManifestSchema as manifestSchema } from "./base.js";
|
|
3
3
|
import { COLLECTION_NAME, FIELD_LIMITS, FIELD_NAME, FieldTypeInvalid, isSearchable, isSortable, isStructured, parseFieldType, RESERVED_FIELDS, } from "./field-types.js";
|
|
4
4
|
import { migrationsSchema } from "./migrations.js";
|
|
5
|
+
import { openSchemaProblems } from "./open-schema.js";
|
|
6
|
+
import { confirmEmailProblems, confirmEmailSchema } from "./confirm-email.js";
|
|
5
7
|
export { COLOUR_TOKENS, FIELD_LIMITS, parseFieldType, RESERVED_FIELDS, structuredFields, } from "./field-types.js";
|
|
6
8
|
/**
|
|
7
9
|
* What an app adds to `.brydio/app.json` to be more than a bundle of servers
|
|
@@ -106,6 +108,18 @@ const collectionSchema = z.object({
|
|
|
106
108
|
search: z.array(z.string()).optional(),
|
|
107
109
|
/** The singular noun the tools are named with: `issue` gives `create_issue`. */
|
|
108
110
|
label: z.string().optional(),
|
|
111
|
+
/**
|
|
112
|
+
* Fields defined at runtime as records of another collection (P5): `fields`
|
|
113
|
+
* names it, and `table`, when given, the `string` field on both naming
|
|
114
|
+
* which table a row or a definition belongs to.
|
|
115
|
+
*/
|
|
116
|
+
openSchema: z
|
|
117
|
+
.object({
|
|
118
|
+
fields: z.string().min(1).max(FIELD_LIMITS.nameChars),
|
|
119
|
+
table: z.string().min(1).max(FIELD_LIMITS.nameChars).optional(),
|
|
120
|
+
})
|
|
121
|
+
.strict()
|
|
122
|
+
.optional(),
|
|
109
123
|
/**
|
|
110
124
|
* A visitor on one of the app's public pages may `get` and `list` every
|
|
111
125
|
* record of this collection in that instance (P3).
|
|
@@ -117,6 +131,13 @@ const collectionSchema = z.object({
|
|
|
117
131
|
* `publicRead` is set too (P3).
|
|
118
132
|
*/
|
|
119
133
|
publicSubmit: z.boolean().optional(),
|
|
134
|
+
/**
|
|
135
|
+
* Brydio emails the visitor once after a public submission here (FO04):
|
|
136
|
+
* `field` names a `string` field holding their address; `subject` and
|
|
137
|
+
* `message` are short plain text; `link` adds a button back to the page.
|
|
138
|
+
* Needs `publicSubmit`. Brydio decides when, how often and from whom.
|
|
139
|
+
*/
|
|
140
|
+
confirmEmail: confirmEmailSchema.optional(),
|
|
120
141
|
});
|
|
121
142
|
const screenSchema = z.object({
|
|
122
143
|
/**
|
|
@@ -327,6 +348,29 @@ export function dataProblems(additions, options = {}) {
|
|
|
327
348
|
}
|
|
328
349
|
}
|
|
329
350
|
}
|
|
351
|
+
// Open-schema collections (P5): the companion is declared, shaped as the
|
|
352
|
+
// host reads it, and defines one collection's fields at most.
|
|
353
|
+
const companions = new Map();
|
|
354
|
+
for (const [collection, declared] of collections) {
|
|
355
|
+
if (!declared.openSchema)
|
|
356
|
+
continue;
|
|
357
|
+
for (const problem of openSchemaProblems(collection, declared.openSchema, additions.data ?? {})) {
|
|
358
|
+
problems.push({ ...problem, collection });
|
|
359
|
+
}
|
|
360
|
+
const other = companions.get(declared.openSchema.fields);
|
|
361
|
+
if (other) {
|
|
362
|
+
problems.push({
|
|
363
|
+
code: 'data_open_fields_unknown',
|
|
364
|
+
collection,
|
|
365
|
+
message: `${declared.openSchema.fields} already defines ${other}'s fields; give ${collection} its own.`,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
companions.set(declared.openSchema.fields, collection);
|
|
369
|
+
}
|
|
370
|
+
// A confirmation email names a string field of a collection visitors submit to (FO04).
|
|
371
|
+
for (const [collection, declared] of collections) {
|
|
372
|
+
problems.push(...confirmEmailProblems(collection, declared));
|
|
373
|
+
}
|
|
330
374
|
// What the app keeps must be what it asks to keep (A3-F06-S01): a
|
|
331
375
|
// collection the grants leave out would be data a workspace never agreed to
|
|
332
376
|
// hold, found only when the first write is refused.
|
|
@@ -612,8 +656,16 @@ export function labelOf(collection, label) {
|
|
|
612
656
|
*/
|
|
613
657
|
export function collectionsOf(manifest) {
|
|
614
658
|
const parsed = storedExtensionsSchema.parse({ data: manifest.data ?? {} });
|
|
659
|
+
const owners = new Map(Object.entries(parsed.data ?? {}).flatMap(([name, declared]) => declared.openSchema ? [[declared.openSchema.fields, { name, flag: declared.openSchema }]] : []));
|
|
615
660
|
return Object.entries(parsed.data ?? {}).map(([name, declared]) => {
|
|
616
|
-
const
|
|
661
|
+
const owner = owners.get(name);
|
|
662
|
+
// The field naming a row's table is kept in plain on both sides, so a
|
|
663
|
+
// table is filtered and counted without decrypting a row (P5).
|
|
664
|
+
const tableField = declared.openSchema?.table ?? owner?.flag.table;
|
|
665
|
+
const fields = Object.fromEntries(Object.entries(declared.schema).map(([field, raw]) => {
|
|
666
|
+
const type = parseFieldType(raw);
|
|
667
|
+
return [field, field === tableField ? { ...type, plain: true } : type];
|
|
668
|
+
}));
|
|
617
669
|
const label = labelOf(name, declared.label);
|
|
618
670
|
const names = Object.keys(fields);
|
|
619
671
|
return {
|
|
@@ -625,6 +677,10 @@ export function collectionsOf(manifest) {
|
|
|
625
677
|
sortable: names.filter(field => isSortable(fields[field])),
|
|
626
678
|
search: declared.search ?? [],
|
|
627
679
|
projectField: names.find(field => fields[field].kind === 'project') ?? null,
|
|
680
|
+
...(declared.openSchema
|
|
681
|
+
? { openSchema: { fields: declared.openSchema.fields, table: declared.openSchema.table ?? null } }
|
|
682
|
+
: {}),
|
|
683
|
+
...(owner ? { definesFieldsOf: owner.name } : {}),
|
|
628
684
|
};
|
|
629
685
|
});
|
|
630
686
|
}
|