|
| 1 | +/** |
| 2 | + * The document checks the schema alone decides. The server's write path runs |
| 3 | + * them on every write, and form validation in the browser runs the same |
| 4 | + * functions, so the two can't disagree. |
| 5 | + */ |
| 6 | +import { contentLocales } from '../config' |
| 7 | +import { isLocalizedValue, primaryLocaleValue } from '../i18n' |
| 8 | +import { |
| 9 | + blockTypeMap, |
| 10 | + fillDerivedFields, |
| 11 | + isFieldVisible, |
| 12 | + schemaFor, |
| 13 | +} from '../schema' |
| 14 | +import type { Collection, Doc, Field, JsonValue } from '../schema' |
| 15 | + |
| 16 | +/** A failed check: the value's path from the document root, and why. */ |
| 17 | +export interface SchemaIssue { |
| 18 | + path: Array<string> |
| 19 | + message: string |
| 20 | +} |
| 21 | + |
| 22 | +/** A field's value somewhere in a document. */ |
| 23 | +export interface FieldValueVisit { |
| 24 | + field: Field |
| 25 | + value: JsonValue | undefined |
| 26 | + path: Array<string> |
| 27 | +} |
| 28 | + |
| 29 | +/** Whether a value counts as "empty" for a `required` check. */ |
| 30 | +export function isEmptyFieldValue(v: JsonValue | undefined): boolean { |
| 31 | + if (v === undefined || v === null || v === '') return true |
| 32 | + if (Array.isArray(v)) return v.length === 0 |
| 33 | + return false |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Every field value a document shows, at any depth (groups, array items, |
| 38 | + * blocks), parents before their sub-fields. A field hidden by `showWhen` is |
| 39 | + * skipped with everything under it. A localized value is its default |
| 40 | + * locale's entry: a missing translation falls back on read. |
| 41 | + */ |
| 42 | +export function visibleFieldValues( |
| 43 | + fields: ReadonlyArray<Field>, |
| 44 | + doc: Record<string, unknown> | undefined, |
| 45 | +): Array<FieldValueVisit> { |
| 46 | + const out: Array<FieldValueVisit> = [] |
| 47 | + const walk = ( |
| 48 | + list: ReadonlyArray<Field>, |
| 49 | + value: Record<string, unknown> | undefined, |
| 50 | + path: Array<string>, |
| 51 | + ): void => { |
| 52 | + for (const f of list) { |
| 53 | + if (f.showWhen && !isFieldVisible(f, value as Doc | undefined)) continue |
| 54 | + let v = value?.[f.name] as JsonValue | undefined |
| 55 | + const at = [...path, f.name] |
| 56 | + if (f.localized && isLocalizedValue(v)) { |
| 57 | + v = primaryLocaleValue(v, contentLocales()?.defaultLocale ?? 'en') as |
| 58 | + | JsonValue |
| 59 | + | undefined |
| 60 | + } |
| 61 | + out.push({ field: f, value: v, path: at }) |
| 62 | + if (f.type === 'group' && f.fields && v && typeof v === 'object') { |
| 63 | + walk(f.fields, v as Record<string, unknown>, at) |
| 64 | + } |
| 65 | + if (f.type === 'array' && f.fields && Array.isArray(v)) { |
| 66 | + for (const [i, item] of v.entries()) { |
| 67 | + walk(f.fields, item as Record<string, unknown>, [...at, String(i)]) |
| 68 | + } |
| 69 | + } |
| 70 | + if (f.type === 'blocks' && Array.isArray(v)) { |
| 71 | + const types = blockTypeMap() |
| 72 | + for (const [i, block] of v.entries()) { |
| 73 | + if (!block || typeof block !== 'object' || Array.isArray(block)) |
| 74 | + continue |
| 75 | + const b = block as Record<string, unknown> |
| 76 | + const typeName = |
| 77 | + typeof b._type === 'string' |
| 78 | + ? b._type |
| 79 | + : typeof b.type === 'string' |
| 80 | + ? b.type |
| 81 | + : undefined |
| 82 | + const bt = typeName ? types[typeName] : undefined |
| 83 | + if (bt) walk(bt.fields, b, [...at, String(i)]) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + walk(fields, doc, []) |
| 89 | + return out |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * `required` on a visible conditional field. The Zod schema relaxes it, |
| 94 | + * because only the sibling values decide whether the field is shown. |
| 95 | + */ |
| 96 | +export function conditionalRequiredIssue( |
| 97 | + visit: FieldValueVisit, |
| 98 | +): SchemaIssue | undefined { |
| 99 | + const { field, value, path } = visit |
| 100 | + if (!field.showWhen || field.required !== true) return undefined |
| 101 | + if (!isEmptyFieldValue(value)) return undefined |
| 102 | + return { path, message: `${field.label} is required` } |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * What the server's schema checks report for a document: types, `required`, |
| 107 | + * `min`/`max`, formats (slug, email, url, color), options, and `required` on |
| 108 | + * visible conditional fields, after the same derived-field fill (a slug from |
| 109 | + * its source field). The rules only the server can run aren't here: custom |
| 110 | + * `validate`, relation targets, `unique`, hooks, and access. |
| 111 | + */ |
| 112 | +export function schemaIssues( |
| 113 | + collection: Collection, |
| 114 | + doc: Record<string, unknown>, |
| 115 | +): Array<SchemaIssue> { |
| 116 | + const filled = fillDerivedFields(collection, doc) |
| 117 | + const parsed = schemaFor(collection).safeParse(filled) |
| 118 | + const issues: Array<SchemaIssue> = parsed.success |
| 119 | + ? [] |
| 120 | + : parsed.error.issues.map((i) => ({ |
| 121 | + path: i.path.map(String), |
| 122 | + message: i.message, |
| 123 | + })) |
| 124 | + const checked = parsed.success ? (parsed.data as Doc) : filled |
| 125 | + for (const visit of visibleFieldValues(collection.fields, checked)) { |
| 126 | + const issue = conditionalRequiredIssue(visit) |
| 127 | + if (issue) issues.push(issue) |
| 128 | + } |
| 129 | + return issues |
| 130 | +} |
0 commit comments