fragmentor 0.2.1 → 0.2.2
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/dist/src/build/compile/index.js +3 -3
- package/dist/src/build/resolve/index.d.ts +3 -3
- package/dist/src/build/resolve/index.js +10 -11
- package/dist/src/build/validate/schema.js +0 -6
- package/dist/src/model/types/fields.d.ts +4 -4
- package/dist/src/model/types/fields.js +4 -2
- package/dist/src/model/types/schema.d.ts +2 -2
- package/package.json +1 -1
|
@@ -59,9 +59,9 @@ export function compile(sources, { parse, dev = false, url, route, published })
|
|
|
59
59
|
const types = [...builtins, ...schemas];
|
|
60
60
|
/* Completed up front so null references fill in on referenced documents too (e.g. a Site's navigation), not only on the page */
|
|
61
61
|
const completed = documents.map((d) => {
|
|
62
|
-
const
|
|
63
|
-
const document = complete({ document: d, schemas: types, references: documents, warnings:
|
|
64
|
-
for (const message of
|
|
62
|
+
const many = [];
|
|
63
|
+
const document = complete({ document: d, schemas: types, references: documents, warnings: many });
|
|
64
|
+
for (const message of many)
|
|
65
65
|
warnings.push({ path: `/documents${d._path}`, message });
|
|
66
66
|
return document;
|
|
67
67
|
});
|
|
@@ -29,7 +29,7 @@ export declare function resolve({ document, references }: {
|
|
|
29
29
|
export declare function resolver({ references }: {
|
|
30
30
|
references: Document[];
|
|
31
31
|
}): (document: Document) => Resolve<Document>;
|
|
32
|
-
export declare const
|
|
32
|
+
export declare const MANY_REFERENCES = 100;
|
|
33
33
|
/**
|
|
34
34
|
* Fills in every non-nullable `reference`/`references` field left `null` on a document with matching
|
|
35
35
|
* candidates from `references`, so the document validates and `resolve` has something to resolve. Meant to run before
|
|
@@ -37,9 +37,9 @@ export declare const DEFAULT_MAX_REFERENCES = 20;
|
|
|
37
37
|
* @param document The document to complete.
|
|
38
38
|
* @param schemas The list of all schemas, used to look up field definitions.
|
|
39
39
|
* @param references The candidate documents to fill non-nullable null references with.
|
|
40
|
-
* @param warnings Collects a warning for every `references` field without a `max`
|
|
40
|
+
* @param warnings Collects a warning for every `references` field without a `max` filled with more than MANY_REFERENCES documents.
|
|
41
41
|
* @returns The document with eligible null references filled in.
|
|
42
|
-
* @example complete({ document, schemas, references: documents, warnings }) // warnings: ["related:
|
|
42
|
+
* @example complete({ document, schemas, references: documents, warnings }) // warnings: ["related: Null, so filled with all 143 matching documents (set max to render fewer)"]
|
|
43
43
|
*/
|
|
44
44
|
export declare function complete({ document, schemas, references, warnings }: {
|
|
45
45
|
document: Document;
|
|
@@ -53,19 +53,19 @@ export function resolver({ references }) {
|
|
|
53
53
|
/* A document reached earlier through a reference is already linked, so it comes back as that same object */
|
|
54
54
|
return (document) => link(document);
|
|
55
55
|
}
|
|
56
|
-
/*
|
|
57
|
-
export const
|
|
56
|
+
/* Past this many documents, filling an empty references field without a max is a warning: every page that renders it renders them all */
|
|
57
|
+
export const MANY_REFERENCES = 100;
|
|
58
58
|
/**
|
|
59
59
|
* Walks a value against its field definitions, filling in any non-nullable `reference`/`references` field
|
|
60
60
|
* left `null` with matching candidates from `references` (matching `field.allow` when set, capped at
|
|
61
|
-
* `field.max` for `references`, or
|
|
61
|
+
* `field.max` for `references`, or every match without one), so `resolve` has something to resolve. Fragments are walked recursively
|
|
62
62
|
* using `schemas` to find their field definitions.
|
|
63
63
|
* @param value The value to walk (document or fragment).
|
|
64
64
|
* @param fields The field definitions for `value`.
|
|
65
65
|
* @param schemas The list of all schemas, used for nested fragments.
|
|
66
66
|
* @param references The candidate documents to fill non-nullable null references with.
|
|
67
67
|
* @param path The keys from the document to `value`, to say where a field is in a warning.
|
|
68
|
-
* @param warnings Collects a warning for every field
|
|
68
|
+
* @param warnings Collects a warning for every field without a `max` filled with more than MANY_REFERENCES documents.
|
|
69
69
|
* @returns The value with eligible null references filled in.
|
|
70
70
|
*/
|
|
71
71
|
function completeProp(value, fields, schemas, references, path, warnings) {
|
|
@@ -82,11 +82,10 @@ function completeProp(value, fields, schemas, references, path, warnings) {
|
|
|
82
82
|
else if (field.type === "references") {
|
|
83
83
|
if (prop === null && !field.nullable) {
|
|
84
84
|
const candidates = references.filter(r => !field.allow.length || field.allow.includes(r._type));
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
warning(`${[...path, key].join(".")}: Left empty, so filled with the first ${max} of ${candidates.length} matching documents. Set a max to choose how many, or nullable`, null, warnings);
|
|
85
|
+
result[key] = candidates.slice(0, field.max).map(r => ({ _ref: r._id }));
|
|
86
|
+
/* A field's own max is a choice, so only a large unbounded fill is worth a warning */
|
|
87
|
+
if (field.max === undefined && candidates.length > MANY_REFERENCES) {
|
|
88
|
+
warning(`${[...path, key].join(".")}: Null, so filled with all ${candidates.length} matching documents (set max to render fewer)`, null, warnings);
|
|
90
89
|
}
|
|
91
90
|
}
|
|
92
91
|
}
|
|
@@ -113,9 +112,9 @@ function completeProp(value, fields, schemas, references, path, warnings) {
|
|
|
113
112
|
* @param document The document to complete.
|
|
114
113
|
* @param schemas The list of all schemas, used to look up field definitions.
|
|
115
114
|
* @param references The candidate documents to fill non-nullable null references with.
|
|
116
|
-
* @param warnings Collects a warning for every `references` field without a `max`
|
|
115
|
+
* @param warnings Collects a warning for every `references` field without a `max` filled with more than MANY_REFERENCES documents.
|
|
117
116
|
* @returns The document with eligible null references filled in.
|
|
118
|
-
* @example complete({ document, schemas, references: documents, warnings }) // warnings: ["related:
|
|
117
|
+
* @example complete({ document, schemas, references: documents, warnings }) // warnings: ["related: Null, so filled with all 143 matching documents (set max to render fewer)"]
|
|
119
118
|
*/
|
|
120
119
|
export function complete({ document, schemas, references, warnings }) {
|
|
121
120
|
const def = getSchema({ schemas, type: document._type });
|
|
@@ -2,7 +2,6 @@ import { EmailSchema, FieldSchema, isFragment, isFragments, isReserved, Identifi
|
|
|
2
2
|
import { error, warning } from "../../utils/report.js";
|
|
3
3
|
import { empty } from "../../model/schema.js";
|
|
4
4
|
import { rejects } from "./document.js";
|
|
5
|
-
import { DEFAULT_MAX_REFERENCES } from "../resolve/index.js";
|
|
6
5
|
/**
|
|
7
6
|
* Validates a single field value considering all schemas.
|
|
8
7
|
* @param field The field to validate.
|
|
@@ -28,11 +27,6 @@ export function validateField({ field, path = [], errors, warnings }) {
|
|
|
28
27
|
return field;
|
|
29
28
|
field.default = fail(`Field default can only be null when the field is nullable`, empty(field));
|
|
30
29
|
}
|
|
31
|
-
/* `complete` fills such a field, when left null, with up to DEFAULT_MAX_REFERENCES documents: on a site-wide fragment that's that many cards on every page */
|
|
32
|
-
if (field.type === "references" && !field.nullable && field.max === undefined) {
|
|
33
|
-
const at = path.length ? `${path.join(".")}: ` : "";
|
|
34
|
-
warning(`${at}Left empty, this references field is filled with up to ${DEFAULT_MAX_REFERENCES} matching documents. Set a max to choose how many, or nullable`, null, warnings);
|
|
35
|
-
}
|
|
36
30
|
if ("min" in field && "max" in field && field.min !== undefined && field.max !== undefined && field.min > field.max) {
|
|
37
31
|
field.min = fail(`Field min ${field.min} is greater than max ${field.max}`, field.max);
|
|
38
32
|
}
|
|
@@ -136,7 +136,7 @@ export declare const UrlFieldSchema: z.ZodObject<{
|
|
|
136
136
|
immutable: z.ZodOptional<z.ZodBoolean>;
|
|
137
137
|
nullable: z.ZodOptional<z.ZodBoolean>;
|
|
138
138
|
type: z.ZodLiteral<"url">;
|
|
139
|
-
default: z.ZodOptional<z.ZodNullable<z.ZodString
|
|
139
|
+
default: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodLiteral<"">]>>>;
|
|
140
140
|
}, z.core.$strip>;
|
|
141
141
|
export type UrlField = z.infer<typeof UrlFieldSchema>;
|
|
142
142
|
export declare const EmailFieldSchema: z.ZodObject<{
|
|
@@ -147,7 +147,7 @@ export declare const EmailFieldSchema: z.ZodObject<{
|
|
|
147
147
|
immutable: z.ZodOptional<z.ZodBoolean>;
|
|
148
148
|
nullable: z.ZodOptional<z.ZodBoolean>;
|
|
149
149
|
type: z.ZodLiteral<"email">;
|
|
150
|
-
default: z.ZodOptional<z.ZodNullable<z.ZodString
|
|
150
|
+
default: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodLiteral<"">]>>>;
|
|
151
151
|
min: z.ZodOptional<z.ZodNumber>;
|
|
152
152
|
max: z.ZodOptional<z.ZodNumber>;
|
|
153
153
|
}, z.core.$strip>;
|
|
@@ -350,7 +350,7 @@ export declare const FieldSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
|
350
350
|
immutable: z.ZodOptional<z.ZodBoolean>;
|
|
351
351
|
nullable: z.ZodOptional<z.ZodBoolean>;
|
|
352
352
|
type: z.ZodLiteral<"url">;
|
|
353
|
-
default: z.ZodOptional<z.ZodNullable<z.ZodString
|
|
353
|
+
default: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodLiteral<"">]>>>;
|
|
354
354
|
}, z.core.$strip>, z.ZodObject<{
|
|
355
355
|
label: z.ZodOptional<z.ZodString>;
|
|
356
356
|
description: z.ZodOptional<z.ZodString>;
|
|
@@ -359,7 +359,7 @@ export declare const FieldSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
|
359
359
|
immutable: z.ZodOptional<z.ZodBoolean>;
|
|
360
360
|
nullable: z.ZodOptional<z.ZodBoolean>;
|
|
361
361
|
type: z.ZodLiteral<"email">;
|
|
362
|
-
default: z.ZodOptional<z.ZodNullable<z.ZodString
|
|
362
|
+
default: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodLiteral<"">]>>>;
|
|
363
363
|
min: z.ZodOptional<z.ZodNumber>;
|
|
364
364
|
max: z.ZodOptional<z.ZodNumber>;
|
|
365
365
|
}, z.core.$strip>, z.ZodObject<{
|
|
@@ -59,11 +59,13 @@ export const ReferencesFieldSchema = BaseFieldSchema.extend({
|
|
|
59
59
|
});
|
|
60
60
|
export const UrlFieldSchema = BaseFieldSchema.extend({
|
|
61
61
|
type: z.literal("url"),
|
|
62
|
-
|
|
62
|
+
/* "" is a url field's empty value (see `empty`), as it is for a document's value */
|
|
63
|
+
default: UrlSchema.or(z.literal("")).nullable().optional(),
|
|
63
64
|
});
|
|
64
65
|
export const EmailFieldSchema = BaseFieldSchema.extend({
|
|
65
66
|
type: z.literal("email"),
|
|
66
|
-
|
|
67
|
+
/* "" is an email field's empty value (see `empty`), as it is for a document's value */
|
|
68
|
+
default: EmailSchema.or(z.literal("")).nullable().optional(),
|
|
67
69
|
min: z.number().int().min(0).optional(),
|
|
68
70
|
max: z.number().int().min(0).optional(),
|
|
69
71
|
});
|
|
@@ -176,7 +176,7 @@ export declare const SchemaSchema: z.ZodObject<{
|
|
|
176
176
|
immutable: z.ZodOptional<z.ZodBoolean>;
|
|
177
177
|
nullable: z.ZodOptional<z.ZodBoolean>;
|
|
178
178
|
type: z.ZodLiteral<"url">;
|
|
179
|
-
default: z.ZodOptional<z.ZodNullable<z.ZodString
|
|
179
|
+
default: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodLiteral<"">]>>>;
|
|
180
180
|
}, z.core.$strip>, z.ZodObject<{
|
|
181
181
|
label: z.ZodOptional<z.ZodString>;
|
|
182
182
|
description: z.ZodOptional<z.ZodString>;
|
|
@@ -185,7 +185,7 @@ export declare const SchemaSchema: z.ZodObject<{
|
|
|
185
185
|
immutable: z.ZodOptional<z.ZodBoolean>;
|
|
186
186
|
nullable: z.ZodOptional<z.ZodBoolean>;
|
|
187
187
|
type: z.ZodLiteral<"email">;
|
|
188
|
-
default: z.ZodOptional<z.ZodNullable<z.ZodString
|
|
188
|
+
default: z.ZodOptional<z.ZodNullable<z.ZodUnion<[z.ZodString, z.ZodLiteral<"">]>>>;
|
|
189
189
|
min: z.ZodOptional<z.ZodNumber>;
|
|
190
190
|
max: z.ZodOptional<z.ZodNumber>;
|
|
191
191
|
}, z.core.$strip>, z.ZodObject<{
|