{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "field-value-errors",
  "title": "Field value errors",
  "description": "Checks a form's answers against the fields' schema and words one message per field, from the block content.",
  "dependencies": [
    "@registrystack/app-runtime@~0.43.0-next.0"
  ],
  "registryDependencies": [
    "@registrystack/lib-content",
    "@registrystack/lib-format"
  ],
  "files": [
    {
      "path": "fields/value-errors.ts",
      "content": "import {\n  validateValues,\n  type FieldModel,\n  type JsonValue,\n  type ValueIssue,\n} from \"@registrystack/app-runtime\";\nimport type { BlockContent } from \"@/blocks/lib/content\";\nimport { fill, recordedNumber } from \"@/blocks/lib/format\";\n\n/**\n * What an answer check says about one field, in `locale`: the content's\n * wording for that field first, then the wording for the kind of problem,\n * then the registry's own message where an issue carries one.\n */\nexport function issueMessage(\n  field: FieldModel,\n  issue: Pick<ValueIssue, \"code\" | \"message\" | \"limit\">,\n  c: BlockContent,\n  locale: string,\n): string {\n  return (\n    c.fieldErrors[field.id] ??\n    (issue.code === \"required\"\n      ? c.requiredError\n      : issue.code === \"format\" && field.format === \"date\"\n        ? c.dateError\n        : (issue.message ?? boundMessage(issue, c, locale) ?? c.invalidError))\n  );\n}\n\n/**\n * The wording for an answer outside a number's bounds or a decimal's places.\n * A decimal's bound arrives as its exact text and is written as recorded.\n */\nfunction boundMessage(\n  issue: Pick<ValueIssue, \"code\" | \"limit\">,\n  c: BlockContent,\n  locale: string,\n): string | undefined {\n  if (issue.limit === undefined) return undefined;\n  if (issue.code === \"scale\")\n    return issue.limit === 0\n      ? c.wholeNumberError\n      : fill(c.decimalPlacesError, { places: issue.limit }, locale);\n  const limit =\n    typeof issue.limit === \"string\"\n      ? recordedNumber(issue.limit, locale)\n      : issue.limit;\n  if (issue.code === \"minimum\") return fill(c.minimumError, { limit }, locale);\n  if (issue.code === \"maximum\") return fill(c.maximumError, { limit }, locale);\n  return undefined;\n}\n\n/**\n * What a check of these answers finds, one message per field in `locale`:\n * the registry's schema, and a required list left empty or holding a value\n * its field does not offer.\n */\nexport function valueErrors(\n  fields: readonly FieldModel[],\n  values: Readonly<Record<string, JsonValue>>,\n  c: BlockContent,\n  locale: string,\n): Record<string, string> {\n  const next: Record<string, string> = {};\n  for (const issue of validateValues(fields, values)) {\n    const field = fields.find((item) => item.id === issue.field);\n    if (field && !next[field.id])\n      next[field.id] = issueMessage(field, issue, c, locale);\n  }\n  for (const field of fields) {\n    const value = values[field.id];\n    if (next[field.id] || field.type !== \"array\") continue;\n    if (field.required && (!Array.isArray(value) || value.length === 0))\n      next[field.id] = issueMessage(field, { code: \"required\" }, c, locale);\n    else if (\n      field.options &&\n      Array.isArray(value) &&\n      value.some(\n        (item) => !field.options!.some((option) => option.value === item),\n      )\n    )\n      next[field.id] = issueMessage(field, { code: \"enum\" }, c, locale);\n  }\n  return next;\n}\n",
      "type": "registry:component",
      "target": "@components/fields/value-errors.ts"
    }
  ],
  "type": "registry:component"
}