@valbuild/shared 0.126.0 → 0.127.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.
@@ -1,4 +1,4 @@
1
- import { Internal, CODE_LANGUAGES, DEFAULT_COLOR_FORMAT } from '@valbuild/core';
1
+ import { Internal, CODE_LANGUAGES, resolveSettingsModule, missingDeclaredKeys, localeOfValue, discriminatedUnionBranchOf, DEFAULT_COLOR_FORMAT, declaredKeySetOf } from '@valbuild/core';
2
2
  import { _ as _createForOfIteratorHelper, n as _objectSpread2, o as _arrayLikeToArray, q as _unsupportedIterableToArray, r as _defineProperty, s as _typeof } from '../../dist/getSitemapTree-a6cb37cb.esm.js';
3
3
  export { b as VAL_AI_SESSION_STORAGE_KEY, d as VAL_CANVAS_MESSAGE, c as VAL_CANVAS_PARAM, V as VAL_CONFIG_SESSION_STORAGE_KEY, a as VAL_THEME_SESSION_STORAGE_KEY, g as getNextAppRouterSourceFolder, l as getPageRouterSitemapTree, j as getPageRouterSourceFolder, m as getPatternFromModuleFilePath, h as getTanStackRouterSourceFolder, k as isTanStackRoutesFolder, f as isValCanvasFrame, i as isValCanvasPageMessage, e as isValCanvasStudioMessage, p as parseRoutePattern, w as withValCanvasParam } from '../../dist/getSitemapTree-a6cb37cb.esm.js';
4
4
  import { z } from 'zod';
@@ -1118,6 +1118,16 @@ var SerializedCodeSchema = z.object(_objectSpread2(_objectSpread2({}, commonSche
1118
1118
  options: CodeOptions.optional(),
1119
1119
  opt: z["boolean"]()
1120
1120
  }));
1121
+ var SerializedLocaleSchema = z.object({
1122
+ type: z.literal("locale"),
1123
+ render: FieldRender,
1124
+ preview: z.literal(true).optional(),
1125
+ opt: z["boolean"](),
1126
+ customValidate: z["boolean"]().optional(),
1127
+ readonly: z["boolean"]().optional(),
1128
+ hidden: z["boolean"]().optional(),
1129
+ description: z.string().optional()
1130
+ });
1121
1131
  var SerializedRouteSchema = z.object(_objectSpread2(_objectSpread2({}, commonSchemaFields), {}, {
1122
1132
  type: z.literal("route"),
1123
1133
  options: z.object({
@@ -1143,10 +1153,15 @@ var SerializedSettingsSchema = z.lazy(function () {
1143
1153
  return z.object(_objectSpread2(_objectSpread2({}, commonSchemaFields), {}, {
1144
1154
  type: z.literal("settings"),
1145
1155
  items: z.record(z.string(), SerializedSchema),
1146
- opt: z["boolean"]()
1156
+ opt: z["boolean"](),
1157
+ // Which section this is, where Val has rules about it beyond its keys.
1158
+ // Dropping it would leave the Studio's validation worker running every
1159
+ // field's own rules and none of the cross-field ones — see
1160
+ // `sectionValidators` in core/src/schema/settings.ts.
1161
+ section: z.string().optional()
1147
1162
  }));
1148
1163
  });
1149
- var SerializedSchema = z.union([SerializedStringSchema, SerializedLiteralSchema, SerializedBooleanSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedArraySchema, SerializedDiscriminatedUnionSchema, SerializedEnumSchema, SerializedRichTextSchema, SerializedRecordSchema, SerializedKeyOfSchema, SerializedRouteSchema, SerializedFileSchema, SerializedDateSchema, SerializedDateTimeSchema, SerializedColorSchema, SerializedCodeSchema, SerializedSettingsSchema, SerializedImageSchema]);
1164
+ var SerializedSchema = z.union([SerializedStringSchema, SerializedLiteralSchema, SerializedBooleanSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedArraySchema, SerializedDiscriminatedUnionSchema, SerializedEnumSchema, SerializedRichTextSchema, SerializedRecordSchema, SerializedKeyOfSchema, SerializedRouteSchema, SerializedLocaleSchema, SerializedFileSchema, SerializedDateSchema, SerializedDateTimeSchema, SerializedColorSchema, SerializedCodeSchema, SerializedSettingsSchema, SerializedImageSchema]);
1150
1165
 
1151
1166
  var ValCommit = z.object({
1152
1167
  commitSha: z.string(),
@@ -1322,7 +1337,7 @@ var ValConfig = z.object({
1322
1337
  gitCommit: z.string().optional(),
1323
1338
  gitBranch: z.string().optional()
1324
1339
  });
1325
- var ValidationFixZ = z.union([z.literal("image:add-metadata"), z.literal("image:check-metadata"), z.literal("image:check-remote"), z.literal("image:upload-remote"), z.literal("image:download-remote"), z.literal("images:check-remote"), z.literal("images:upload-remote"), z.literal("file:add-metadata"), z.literal("file:check-metadata"), z.literal("file:check-remote"), z.literal("file:upload-remote"), z.literal("file:download-remote"), z.literal("files:check-remote"), z.literal("files:upload-remote"), z.literal("keyof:check-keys"), z.literal("router:check-route"), z.literal("images:check-unique-folder"), z.literal("files:check-unique-folder"), z.literal("images:check-all-files"), z.literal("files:check-all-files"), z.literal("jsonValues:extract-entry")]);
1340
+ var ValidationFixZ = z.union([z.literal("image:add-metadata"), z.literal("image:check-metadata"), z.literal("image:check-remote"), z.literal("image:upload-remote"), z.literal("image:download-remote"), z.literal("images:check-remote"), z.literal("images:upload-remote"), z.literal("file:add-metadata"), z.literal("file:check-metadata"), z.literal("file:check-remote"), z.literal("file:upload-remote"), z.literal("file:download-remote"), z.literal("files:check-remote"), z.literal("files:upload-remote"), z.literal("keyof:check-keys"), z.literal("router:check-route"), z.literal("locale:check-locale"), z.literal("record:fill-keys"), z.literal("images:check-unique-folder"), z.literal("files:check-unique-folder"), z.literal("images:check-all-files"), z.literal("files:check-all-files"), z.literal("jsonValues:extract-entry")]);
1326
1341
  var ValidationError = z.object({
1327
1342
  message: z.string(),
1328
1343
  value: z.unknown().optional(),
@@ -3229,7 +3244,7 @@ var compatible = {
3229
3244
  * `readonly Json[]` — so the check that reads naturally does not type. This
3230
3245
  * predicate is that check, said once.
3231
3246
  */
3232
- function isJsonObject(value) {
3247
+ function isJsonObject$1(value) {
3233
3248
  return _typeof(value) === "object" && value !== null && !Array.isArray(value);
3234
3249
  }
3235
3250
  function incompatible(reason) {
@@ -3286,7 +3301,7 @@ function variantOf(schema, value) {
3286
3301
  return undefined;
3287
3302
  }
3288
3303
  var discriminator = schema.key;
3289
- if (!isJsonObject(value)) {
3304
+ if (!isJsonObject$1(value)) {
3290
3305
  return undefined;
3291
3306
  }
3292
3307
  var tag = value[discriminator];
@@ -3414,7 +3429,7 @@ function checkCompatibility(from, to) {
3414
3429
  }
3415
3430
  case "object":
3416
3431
  {
3417
- if (from.schema.type !== "object" || !isJsonObject(from.value)) {
3432
+ if (from.schema.type !== "object" || !isJsonObject$1(from.value)) {
3418
3433
  return {
3419
3434
  status: "unknown",
3420
3435
  message: "The old value is not shaped like an object."
@@ -3517,7 +3532,7 @@ function checkCompatibility(from, to) {
3517
3532
  }
3518
3533
  case "record":
3519
3534
  {
3520
- if (from.schema.type !== "record" || !isJsonObject(from.value)) {
3535
+ if (from.schema.type !== "record" || !isJsonObject$1(from.value)) {
3521
3536
  return {
3522
3537
  status: "unknown",
3523
3538
  message: "The old value is not a record."
@@ -3953,6 +3968,59 @@ function checkRouteIsValid(route, includePattern, excludePattern, snapshot) {
3953
3968
  error: false
3954
3969
  };
3955
3970
  }
3971
+
3972
+ /**
3973
+ * The languages the project declares, from its settings module.
3974
+ *
3975
+ * Empty where there is no settings module, no `locales` section, or nothing in
3976
+ * it — all of which mean the same thing: this project has not said it is
3977
+ * translated.
3978
+ */
3979
+ function declaredLocales(snapshot) {
3980
+ var _resolveSettingsModul = resolveSettingsModule(snapshot.schemas),
3981
+ moduleFilePath = _resolveSettingsModul.moduleFilePath;
3982
+ if (moduleFilePath === null) {
3983
+ return [];
3984
+ }
3985
+ var source = snapshot.sources[moduleFilePath];
3986
+ if (_typeof(source) !== "object" || source === null || Array.isArray(source)) {
3987
+ return [];
3988
+ }
3989
+ var locales = source["locales"];
3990
+ if (_typeof(locales) !== "object" || locales === null || Array.isArray(locales)) {
3991
+ return [];
3992
+ }
3993
+ var available = locales["available"];
3994
+ if (!Array.isArray(available)) {
3995
+ return [];
3996
+ }
3997
+ return available.filter(function (tag) {
3998
+ return typeof tag === "string";
3999
+ });
4000
+ }
4001
+ /**
4002
+ * The fixes {@link resolveSchemaSourceFixForError} answers for.
4003
+ *
4004
+ * A marker error carrying one of these is NOT a message: it says "ask the whole
4005
+ * project about this" and carries the value to ask with, because the schema
4006
+ * that raised it can only see its own module. Left unresolved, what a user sees
4007
+ * is the marker's own placeholder text — "should typically be processed by Val
4008
+ * internally … version mismatch" — which is alarming and says nothing.
4009
+ *
4010
+ * A list rather than a check per call site: every caller that resolves some of
4011
+ * these has to resolve all of them, and the two that leaked did so by adding a
4012
+ * fix here and not to a condition somewhere else.
4013
+ */
4014
+ var SCHEMA_SOURCE_FIXES = ["keyof:check-keys", "router:check-route", "locale:check-locale", "record:fill-keys"];
4015
+
4016
+ /** Whether an error is a marker only the whole project can answer. */
4017
+ function isSchemaSourceFixError(error) {
4018
+ var _error$fixes;
4019
+ return ((_error$fixes = error.fixes) !== null && _error$fixes !== void 0 ? _error$fixes : []).some(function (fix) {
4020
+ return SCHEMA_SOURCE_FIXES.includes(fix);
4021
+ });
4022
+ }
4023
+
3956
4024
  /**
3957
4025
  * The record a `keyof:check-keys` error resolves against, or null for any other
3958
4026
  * error — including a malformed marker, which
@@ -3964,8 +4032,8 @@ function checkRouteIsValid(route, includePattern, excludePattern, snapshot) {
3964
4032
  * to the referenced record can invalidate the module holding the reference.
3965
4033
  */
3966
4034
  function keyOfRecordPath(error) {
3967
- var _error$fixes;
3968
- if (!((_error$fixes = error.fixes) !== null && _error$fixes !== void 0 ? _error$fixes : []).includes("keyof:check-keys")) {
4035
+ var _error$fixes2;
4036
+ if (!((_error$fixes2 = error.fixes) !== null && _error$fixes2 !== void 0 ? _error$fixes2 : []).includes("keyof:check-keys")) {
3969
4037
  return null;
3970
4038
  }
3971
4039
  var value = error.value;
@@ -3977,15 +4045,15 @@ function keyOfRecordPath(error) {
3977
4045
  }
3978
4046
 
3979
4047
  /**
3980
- * Resolves a single `keyof:check-keys` or `router:check-route` error against
3981
- * the given schema/source snapshot. Returns:
4048
+ * Resolves a single marker error (see {@link SCHEMA_SOURCE_FIXES}) against the
4049
+ * given schema/source snapshot. Returns:
3982
4050
  * - null if the error is not one of these fixes (caller should pass through)
3983
- * - { status: "resolved" } if the referenced key/route is valid (drop error)
4051
+ * - { status: "resolved" } if the reference is valid (drop error)
3984
4052
  * - { status: "remaining"; error } if invalid (rewritten error to surface)
3985
4053
  */
3986
4054
  function resolveSchemaSourceFixForError(error, snapshot) {
3987
- var _error$fixes2;
3988
- var fixes = (_error$fixes2 = error.fixes) !== null && _error$fixes2 !== void 0 ? _error$fixes2 : [];
4055
+ var _error$fixes3;
4056
+ var fixes = (_error$fixes3 = error.fixes) !== null && _error$fixes3 !== void 0 ? _error$fixes3 : [];
3989
4057
  if (fixes.includes("keyof:check-keys")) {
3990
4058
  if (!error.value) {
3991
4059
  return {
@@ -4044,6 +4112,120 @@ function resolveSchemaSourceFixForError(error, snapshot) {
4044
4112
  status: "resolved"
4045
4113
  };
4046
4114
  }
4115
+ if (fixes.includes("locale:check-locale")) {
4116
+ var value = error.value;
4117
+ if (_typeof(value) !== "object" || value === null) {
4118
+ return {
4119
+ status: "remaining",
4120
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
4121
+ message: "Expected locale validation error to have a 'value' object. Found: ".concat(_typeof(value), ". ").concat(TYPE_ERROR_MESSAGE),
4122
+ typeError: true,
4123
+ fixes: undefined
4124
+ })
4125
+ };
4126
+ }
4127
+ var _ref6 = value,
4128
+ locale = _ref6.locale;
4129
+ if (typeof locale !== "string") {
4130
+ return {
4131
+ status: "remaining",
4132
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
4133
+ message: "Expected locale validation error 'value' to have property 'locale' of type 'string'. Found: ".concat(_typeof(locale), ". ").concat(TYPE_ERROR_MESSAGE),
4134
+ typeError: true,
4135
+ fixes: undefined
4136
+ })
4137
+ };
4138
+ }
4139
+ var available = declaredLocales(snapshot);
4140
+ if (available.length === 0) {
4141
+ return {
4142
+ status: "remaining",
4143
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
4144
+ // The project has not said it is translated, so the value is not
4145
+ // wrong so much as premature — and the fix is in another file.
4146
+ message: "This project has no languages. Declare them under 'locales.available' in the settings module before using s.locale().",
4147
+ fixes: undefined
4148
+ })
4149
+ };
4150
+ }
4151
+ if (available.includes(locale)) {
4152
+ return {
4153
+ status: "resolved"
4154
+ };
4155
+ }
4156
+ return {
4157
+ status: "remaining",
4158
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
4159
+ message: "'".concat(locale, "' is not one of this project's languages: ").concat(available.map(function (each) {
4160
+ return "'".concat(each, "'");
4161
+ }).join(", ")),
4162
+ fixes: undefined
4163
+ })
4164
+ };
4165
+ }
4166
+ if (fixes.includes("record:fill-keys")) {
4167
+ var _value = error.value;
4168
+ if (_typeof(_value) !== "object" || _value === null) {
4169
+ return {
4170
+ status: "remaining",
4171
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
4172
+ message: "Expected record key validation error to have a 'value' object. Found: ".concat(_typeof(_value), ". ").concat(TYPE_ERROR_MESSAGE),
4173
+ typeError: true,
4174
+ fixes: undefined
4175
+ })
4176
+ };
4177
+ }
4178
+ var _ref7 = _value,
4179
+ present = _ref7.present,
4180
+ declared = _ref7.declared;
4181
+ if (!Array.isArray(present)) {
4182
+ return {
4183
+ status: "remaining",
4184
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
4185
+ message: "Expected record key validation error 'value' to have property 'present' of type 'array'. Found: ".concat(_typeof(present), ". ").concat(TYPE_ERROR_MESSAGE),
4186
+ typeError: true,
4187
+ fixes: undefined
4188
+ })
4189
+ };
4190
+ }
4191
+ var presentKeys = present.filter(function (key) {
4192
+ return typeof key === "string";
4193
+ });
4194
+ // A literal union brought its keys along; a locale record left them to be
4195
+ // read out of the settings module, which is the whole reason this check is
4196
+ // resolved here rather than in the schema.
4197
+ var declaredKeys = Array.isArray(declared) ? declared.filter(function (key) {
4198
+ return typeof key === "string";
4199
+ }) : declaredLocales(snapshot);
4200
+ if (declaredKeys.length === 0) {
4201
+ // A locale record on a project that has declared no languages. There is
4202
+ // nothing to require, and the locale field's own check already says the
4203
+ // project has not declared any — saying it twice on one record would not
4204
+ // add anything.
4205
+ return {
4206
+ status: "resolved"
4207
+ };
4208
+ }
4209
+ var missing = missingDeclaredKeys(declaredKeys, presentKeys);
4210
+ if (missing.length === 0) {
4211
+ return {
4212
+ status: "resolved"
4213
+ };
4214
+ }
4215
+ return {
4216
+ status: "remaining",
4217
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
4218
+ message: "Missing ".concat(missing.length === 1 ? "key" : "keys", ": ").concat(missing.map(function (each) {
4219
+ return "'".concat(each, "'");
4220
+ }).join(", "), ". This record's keys are declared by its schema, so every one of them is an entry \u2014 an entry nobody has written yet is null, not absent."),
4221
+ value: {
4222
+ missing: missing,
4223
+ declared: declaredKeys
4224
+ },
4225
+ fixes: undefined
4226
+ })
4227
+ };
4228
+ }
4047
4229
  if (fixes.includes("router:check-route")) {
4048
4230
  if (!error.value) {
4049
4231
  return {
@@ -4105,10 +4287,10 @@ function asRegExpPattern(value) {
4105
4287
  /**
4106
4288
  * Apply schema/source fix resolution across a map of validation errors.
4107
4289
  *
4108
- * Errors with `keyof:check-keys` or `router:check-route` are resolved against
4109
- * the in-memory schema/source snapshot — valid references drop the error,
4110
- * invalid ones are rewritten with a "did you mean…" message and have their
4111
- * `fixes` cleared (so downstream code reports them as plain validation errors).
4290
+ * Errors carrying one of {@link SCHEMA_SOURCE_FIXES} are resolved against the
4291
+ * in-memory schema/source snapshot — valid references drop the error, invalid
4292
+ * ones are rewritten with a "did you mean…" message and have their `fixes`
4293
+ * cleared (so downstream code reports them as plain validation errors).
4112
4294
  * All other errors pass through untouched.
4113
4295
  *
4114
4296
  * Source paths with no remaining errors are removed from the result.
@@ -4142,6 +4324,189 @@ function resolveSchemaSourceFixes(validationErrors, snapshot) {
4142
4324
  return result;
4143
4325
  }
4144
4326
 
4327
+ /**
4328
+ * Which of the project's languages governs a path, or `null` for none.
4329
+ *
4330
+ * THE question the whole feature turns on. A locale scope is a subtree in one
4331
+ * language, and three things open one: a `locale` field on an object, a record
4332
+ * keyed by `s.locale()`, and a locale segment in a segmented key. Once this can
4333
+ * be answered from the schema and the source alone, everything downstream falls
4334
+ * out of it — the Studio's locale filter, deep links, `<html lang>`, and knowing
4335
+ * what to translate from and into.
4336
+ *
4337
+ * One implementation, so the Studio, the server and the validation worker cannot
4338
+ * disagree about what language a piece of content is in.
4339
+ *
4340
+ * `null` means no scope governs the path — which is the answer for most content
4341
+ * in most projects, and for every project that has not declared any languages.
4342
+ */
4343
+ function localeAt(path, snapshot) {
4344
+ var available = declaredLocales(snapshot);
4345
+ if (available.length === 0) {
4346
+ // Nothing can be one of no languages. Answering `null` rather than reading
4347
+ // the content is also what makes this cheap for the projects that are not
4348
+ // translated at all.
4349
+ return null;
4350
+ }
4351
+ var _Internal$splitModule = Internal.splitModuleFilePathAndModulePath(path),
4352
+ _Internal$splitModule2 = _slicedToArray(_Internal$splitModule, 2),
4353
+ moduleFilePath = _Internal$splitModule2[0],
4354
+ modulePath = _Internal$splitModule2[1];
4355
+ var schema = snapshot.schemas[moduleFilePath];
4356
+ var source = snapshot.sources[moduleFilePath];
4357
+ if (schema === undefined || source === undefined) {
4358
+ return null;
4359
+ }
4360
+ var segments = modulePath ? Internal.splitModulePath(modulePath) : [];
4361
+ return walk(schema, source, segments, available);
4362
+ }
4363
+
4364
+ /**
4365
+ * Walk from a node towards `segments`, answering with the innermost scope.
4366
+ *
4367
+ * The scope is read on ARRIVAL at each node — the node the path names included,
4368
+ * not only the ones passed through on the way to it. So `localeAt` of a
4369
+ * scope-opening object is that object's own locale rather than its parent's,
4370
+ * and a path that stops AT a block still answers with the block's language.
4371
+ *
4372
+ * A scope may not contain another (see `localeScopeErrors`), so at most one of
4373
+ * these can fire on a well-formed path; taking the innermost is what makes the
4374
+ * answer well-defined while a project is mid-fix.
4375
+ */
4376
+ function walk(schema, source, segments, available) {
4377
+ var entered = enter(schema, source, available);
4378
+ var locale = entered.locale;
4379
+ var currentSchema = entered.schema;
4380
+ var currentSource = source;
4381
+ var _iterator = _createForOfIteratorHelper(segments),
4382
+ _step;
4383
+ try {
4384
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
4385
+ var segment = _step.value;
4386
+ if (currentSchema === undefined) {
4387
+ return locale;
4388
+ }
4389
+ if (currentSchema.type === "record") {
4390
+ var _currentSchema$key;
4391
+ if (((_currentSchema$key = currentSchema.key) === null || _currentSchema$key === void 0 ? void 0 : _currentSchema$key.type) === "locale") {
4392
+ // In a locale-keyed record the KEY is the language, so the segment we
4393
+ // are about to take is the answer.
4394
+ var resolved = localeOfValue(segment, available);
4395
+ if (resolved !== null) {
4396
+ locale = resolved;
4397
+ }
4398
+ }
4399
+ currentSchema = currentSchema.item;
4400
+ } else if (currentSchema.type === "array") {
4401
+ currentSchema = currentSchema.item;
4402
+ } else if (currentSchema.type === "object" || currentSchema.type === "settings") {
4403
+ currentSchema = currentSchema.items[segment];
4404
+ } else {
4405
+ // A leaf, or richtext's internal structure. Nothing below opens a scope.
4406
+ return locale;
4407
+ }
4408
+ currentSource = childSource(currentSource, segment);
4409
+ entered = enter(currentSchema, currentSource, available);
4410
+ currentSchema = entered.schema;
4411
+ if (entered.locale !== null) {
4412
+ locale = entered.locale;
4413
+ }
4414
+ }
4415
+ } catch (err) {
4416
+ _iterator.e(err);
4417
+ } finally {
4418
+ _iterator.f();
4419
+ }
4420
+ return locale;
4421
+ }
4422
+
4423
+ /**
4424
+ * Arrive at a node: resolve what it really is, and read the scope it opens.
4425
+ *
4426
+ * A discriminated union is a fork rather than a level — the variant the value
4427
+ * takes IS the node — so it is resolved here, before anything asks what the
4428
+ * node holds. Doing that on arrival rather than on the way down is the
4429
+ * difference between a block's own path answering with its language and
4430
+ * answering with nothing.
4431
+ */
4432
+ function enter(schema, source, available) {
4433
+ var resolved = (schema === null || schema === void 0 ? void 0 : schema.type) === "discriminated-union" ? variantOfUnion(schema, source) : schema;
4434
+ return {
4435
+ schema: resolved,
4436
+ locale: resolved === undefined ? null : localeOfObjectField(resolved, source, available)
4437
+ };
4438
+ }
4439
+
4440
+ /**
4441
+ * The language an object's own `locale` field says it is in, if it has one.
4442
+ *
4443
+ * Reads the SOURCE, since unlike a record key the value is content rather than
4444
+ * part of the path. A field that has not been filled in, or holds something
4445
+ * that is not one of the project's languages, is not an answer — validation is
4446
+ * already reporting that, and guessing here would put a language in
4447
+ * `<html lang>` that nobody chose.
4448
+ */
4449
+ function localeOfObjectField(schema, source, available) {
4450
+ if (schema.type !== "object" || !isJsonObject(source)) {
4451
+ return null;
4452
+ }
4453
+ for (var _i = 0, _Object$entries = Object.entries(schema.items); _i < _Object$entries.length; _i++) {
4454
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
4455
+ key = _Object$entries$_i[0],
4456
+ item = _Object$entries$_i[1];
4457
+ if (item.type !== "locale") {
4458
+ continue;
4459
+ }
4460
+ var value = source[key];
4461
+ if (typeof value !== "string") {
4462
+ return null;
4463
+ }
4464
+ return localeOfValue(value, available);
4465
+ }
4466
+ return null;
4467
+ }
4468
+
4469
+ /**
4470
+ * The variant a value takes, or `undefined` if the tag matches none.
4471
+ *
4472
+ * The tag is read off the source here; picking the variant is
4473
+ * {@link discriminatedUnionBranchOf}, shared with the Studio's locale filter so
4474
+ * the two cannot disagree about which variant a row is. `s.enum()` never
4475
+ * reaches this — it is a leaf, with nothing under it to walk into.
4476
+ */
4477
+ function variantOfUnion(schema, source) {
4478
+ if (!isJsonObject(source)) {
4479
+ return undefined;
4480
+ }
4481
+ return discriminatedUnionBranchOf(schema, source[schema.key]);
4482
+ }
4483
+
4484
+ /** The child of a source value at a segment, or `null` where there is none. */
4485
+ function childSource(source, segment) {
4486
+ var _source$segment;
4487
+ if (Array.isArray(source)) {
4488
+ var _source$index;
4489
+ var index = Number(segment);
4490
+ return Number.isInteger(index) ? (_source$index = source[index]) !== null && _source$index !== void 0 ? _source$index : null : null;
4491
+ }
4492
+ if (!isJsonObject(source)) {
4493
+ return null;
4494
+ }
4495
+ return (_source$segment = source[segment]) !== null && _source$segment !== void 0 ? _source$segment : null;
4496
+ }
4497
+
4498
+ /**
4499
+ * Whether `source` is a JSON object rather than an array or a primitive.
4500
+ *
4501
+ * A type predicate rather than the inline checks, because `Array.isArray` does
4502
+ * NOT narrow `JsonArray` out of `Json` — it is `readonly Json[]`, and the
4503
+ * built-in guard only narrows mutable arrays. Without this, every read of a
4504
+ * property would need an assertion.
4505
+ */
4506
+ function isJsonObject(source) {
4507
+ return _typeof(source) === "object" && source !== null && !Array.isArray(source);
4508
+ }
4509
+
4145
4510
  /**
4146
4511
  * Local `yyyy-MM-dd`, which is the one thing this module used `date-fns` for.
4147
4512
  *
@@ -4178,15 +4543,27 @@ function clampDateTimeString(value, options) {
4178
4543
  }
4179
4544
  return value;
4180
4545
  }
4181
- function emptyOf(schema) {
4546
+
4547
+ /**
4548
+ * What `emptyOf` cannot read off a serialized schema.
4549
+ *
4550
+ * The project's languages, and which one is being worked in. Neither is in the
4551
+ * schema: a locale-keyed record has one entry per language and the languages
4552
+ * are declared in the settings module, and "which language am I writing" is a
4553
+ * fact about the editor, not about the content. Optional throughout: a caller
4554
+ * that has no context gets an empty record and an unset locale, which
4555
+ * validation then reports, rather than wrong ones.
4556
+ */
4557
+
4558
+ function emptyOf(schema, context) {
4182
4559
  if (schema.type === "object") {
4183
4560
  return Object.fromEntries(Object.keys(schema.items).map(function (key) {
4184
- return [key, emptyOf(schema.items[key])];
4561
+ return [key, emptyOf(schema.items[key], context)];
4185
4562
  }));
4186
4563
  } else if (schema.type === "array") {
4187
4564
  return [];
4188
4565
  } else if (schema.type === "record") {
4189
- return {};
4566
+ return emptyRecord(schema, context);
4190
4567
  } else if (schema.type === "settings") {
4191
4568
  // Not an object of empty sections: every settings key is optional, and
4192
4569
  // absent IS the empty value. Filling the sections in would write a shape
@@ -4210,6 +4587,16 @@ function emptyOf(schema) {
4210
4587
  }
4211
4588
  } else if (schema.type === "route") {
4212
4589
  return ""; // Empty string as default route value
4590
+ } else if (schema.type === "locale") {
4591
+ var _context$selectedLoca;
4592
+ // Which languages exist is in the settings module, which `emptyOf` has no
4593
+ // access to — it works from a serialized schema alone. So a caller that
4594
+ // has narrowed to one language says so and gets it; a caller that has not
4595
+ // gets the empty string, which is not a language and which validation
4596
+ // reports. Guessing `locales[0]` instead would file content under a
4597
+ // language nobody chose, which is the thing this whole feature exists to
4598
+ // make visible.
4599
+ return (_context$selectedLoca = context === null || context === void 0 ? void 0 : context.selectedLocale) !== null && _context$selectedLoca !== void 0 ? _context$selectedLoca : "";
4213
4600
  } else if (schema.type === "file" || schema.type === "image") {
4214
4601
  return null; // returning null is the only thing we can do, however, it means that the patches cannot be applied yet since that might fail
4215
4602
  } else if (schema.type === "literal") {
@@ -4222,7 +4609,7 @@ function emptyOf(schema) {
4222
4609
  if (schema.items.length === 0) {
4223
4610
  throw Error("Cannot create an empty value for a discriminated union with no variants");
4224
4611
  }
4225
- return emptyOf(schema.items[0]);
4612
+ return emptyOf(schema.items[0], context);
4226
4613
  } else if (schema.type === "enum") {
4227
4614
  if (schema.values.length === 0) {
4228
4615
  throw Error("Cannot create an empty value for an enum with no values");
@@ -4248,6 +4635,32 @@ function emptyOf(schema) {
4248
4635
  throw Error("Unexpected schema type: " + JSON.stringify(_exhaustiveCheck));
4249
4636
  }
4250
4637
 
4638
+ /**
4639
+ * An empty record — with every key its schema declares already in it.
4640
+ *
4641
+ * An open record (`s.record(s.string(), item)`) starts empty, because there is
4642
+ * no key anyone could mean. A record whose key schema enumerates its keys is
4643
+ * the opposite: the keys are part of the schema, so an empty one is already
4644
+ * missing them, and handing back `{}` would create content that fails
4645
+ * validation the moment it is written.
4646
+ *
4647
+ * The entries are `null` rather than `emptyOf(item)`. A null entry reads as "not
4648
+ * filled in yet", which is what a language nobody has translated into IS —
4649
+ * whereas an entry of empty strings claims someone wrote it and left it blank,
4650
+ * and would count as translated in every list and filter downstream.
4651
+ */
4652
+ function emptyRecord(schema, context) {
4653
+ var _context$locales;
4654
+ var declared = declaredKeySetOf(schema.key);
4655
+ if (declared === null) {
4656
+ return {};
4657
+ }
4658
+ var keys = declared.kind === "literals" ? declared.keys : (_context$locales = context === null || context === void 0 ? void 0 : context.locales) !== null && _context$locales !== void 0 ? _context$locales : [];
4659
+ return Object.fromEntries(keys.map(function (key) {
4660
+ return [key, null];
4661
+ }));
4662
+ }
4663
+
4251
4664
  var toolNames = ["get_all_schema", "get_source", "search_content", "validate_content", "create_patch", "add_session_image_to_gallery", "remove_image_gallery_entry", "navigate_to", "get_current_context", "get_patches", "get_source_path_from_route", "set_session_name", "show_compare_view", "ask_user_question", "duplicate_source", "empty_at_path", "count_entries", "get_record_keys"];
4252
4665
 
4253
4666
  function buildFileRefValue(filePath, metadata) {
@@ -4624,7 +5037,13 @@ function buildEmptyAtPathPatch(args, moduleSchema,
4624
5037
  // Required, not optional: the occupied-key check below is the only thing
4625
5038
  // standing between "scaffold an entry" and "delete the entry that was there",
4626
5039
  // and an optional argument is one a caller forgets.
4627
- moduleSource) {
5040
+ moduleSource,
5041
+ // The project's languages, for a locale-keyed record: its entries ARE the
5042
+ // languages, and they are declared in another module, so an empty one built
5043
+ // without them is `{}` — which the completeness rule then reports as every
5044
+ // language missing. Both callers can answer: the Studio from the context, the
5045
+ // MCP tools from the snapshot they already hold. See `EmptyOfContext`.
5046
+ context) {
4628
5047
  var destinationSchema;
4629
5048
  if (args.destinationPath.length === 0) {
4630
5049
  destinationSchema = moduleSchema;
@@ -4670,7 +5089,7 @@ moduleSource) {
4670
5089
  if (occupied) {
4671
5090
  return occupied;
4672
5091
  }
4673
- var value = emptyOf(destinationSchema);
5092
+ var value = emptyOf(destinationSchema, context);
4674
5093
  return safeParsePatch([{
4675
5094
  op: decision.op,
4676
5095
  path: args.destinationPath,
@@ -4861,6 +5280,8 @@ function isSkippableFixCode(fix) {
4861
5280
  return true;
4862
5281
  case "keyof:check-keys":
4863
5282
  case "router:check-route":
5283
+ case "locale:check-locale":
5284
+ case "record:fill-keys":
4864
5285
  case "jsonValues:extract-entry":
4865
5286
  // `jsonValues:extract-entry` is an entry inlined in the `.val.ts`. The
4866
5287
  // browser cannot fix it (that means writing a new `*.val.json` and
@@ -5268,8 +5689,8 @@ function traverseSchemaSource(source, schema, path, callback) {
5268
5689
  return;
5269
5690
  }
5270
5691
 
5271
- // Handle route
5272
- if (schema.type === "route") {
5692
+ // Handle route and locale: both are plain strings at a leaf
5693
+ if (schema.type === "route" || schema.type === "locale") {
5273
5694
  if (typeof source === "string") {
5274
5695
  callback({
5275
5696
  source: source,
@@ -5496,7 +5917,13 @@ function indexModule(searchIndex, moduleFilePath, source, schema) {
5496
5917
  // An enum's value is a string the editor chose; it is as searchable as
5497
5918
  // any other string leaf. The old string union was never indexed at all,
5498
5919
  // which is the gap this closes.
5499
- schema.type === "enum") {
5920
+ schema.type === "enum" ||
5921
+ // A locale tag, for the same reason: it is what the editor picked and
5922
+ // what they see in the field, so searching "nb-NO" has to find the
5923
+ // Norwegian rows. `traverseSchemaSource` visits these, and visiting a
5924
+ // leaf only to index it as the empty string is worse than not visiting
5925
+ // it — it looks like content that does not exist.
5926
+ schema.type === "locale") {
5500
5927
  var _source$toString, _source$toString2;
5501
5928
  searchText = (_source$toString = source === null || source === void 0 ? void 0 : source.toString()) !== null && _source$toString !== void 0 ? _source$toString : "";
5502
5929
  label = (_source$toString2 = source === null || source === void 0 ? void 0 : source.toString()) !== null && _source$toString2 !== void 0 ? _source$toString2 : "";
@@ -5637,4 +6064,4 @@ function performSearch(searchIndex, query) {
5637
6064
  };
5638
6065
  }
5639
6066
 
5640
- export { Api, BinaryFileRef, CodeOptions, ColorOptions, DEFAULT_ERROR_MESSAGE, DateOptions, ENCODE_DEFAULTS, ENCODE_MIME_TYPE_OF, FileOptions, HistoricalCommit, HistoricalModule, HistoricalPatchSet, HistoryError, ImageEncodeOption, ImageOptions, JSONValue, JSON_ENTRIES_BATCH_MAX, ModuleComparison, NEVER_ENCODED, ParentRef$1 as ParentRef, Patch, PatchBlock, PatchGroup, PatchId, PatchJSON, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, RestoreVerdict, RichTextOptions, SerializedArraySchema, SerializedBooleanSchema, SerializedCodeSchema, SerializedColorSchema, SerializedDateSchema, SerializedDateTimeSchema, SerializedDiscriminatedUnionSchema, SerializedEnumSchema, SerializedFileSchema, SerializedImageSchema, SerializedKeyOfSchema, SerializedLiteralSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedRecordSchema, SerializedRichTextSchema, SerializedRouteSchema, SerializedSchema, SerializedSettingsSchema, SerializedStringSchema, SharedValConfig, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValCommit, ValDeployment, buildDuplicatePatch, buildEmptyAtPathPatch, buildFileRefValue, buildImageGalleryPatch, buildRemoveImageGalleryEntryPatch, buildSearchIndex, checkCompatibility, chooseEncoded, concatModulePath, createRegExpFromPattern, createSearchIndex, createValClient, describeContainerAtPath, emptyOf, explainIncompatible, filterBlockingValidationErrors, filterRoutesByPatterns, findSimilar, fitWithin, flattenRichText, getErrorMessageFromUnknownJson, getFilenameFromRef, getInlineImgInfo, getKeyOfRecordAt, getRefParts, getRoutesOf, getRoutesWithModulePaths, getSourceAt, indexModule, isMimeTypeAccepted, isRemoteSchema, isSkippedSource, keyOfRecordPath, levenshtein, newestCommitSha, partitionValidationErrors, performSearch, remirrorToRichTextSource, removeModule, resolveEncodeSettings, resolveSchemaSourceFixForError, resolveSchemaSourceFixes, resolveSerializedSchemaAtPath, richTextToRemirror, safeParsePatch, sourcePathOfChild, toolNames, traverseSchemaSource, urlOf, validateRoutePatterns, withExtension };
6067
+ export { Api, BinaryFileRef, CodeOptions, ColorOptions, DEFAULT_ERROR_MESSAGE, DateOptions, ENCODE_DEFAULTS, ENCODE_MIME_TYPE_OF, FileOptions, HistoricalCommit, HistoricalModule, HistoricalPatchSet, HistoryError, ImageEncodeOption, ImageOptions, JSONValue, JSON_ENTRIES_BATCH_MAX, ModuleComparison, NEVER_ENCODED, ParentRef$1 as ParentRef, Patch, PatchBlock, PatchGroup, PatchId, PatchJSON, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, RestoreVerdict, RichTextOptions, SCHEMA_SOURCE_FIXES, SerializedArraySchema, SerializedBooleanSchema, SerializedCodeSchema, SerializedColorSchema, SerializedDateSchema, SerializedDateTimeSchema, SerializedDiscriminatedUnionSchema, SerializedEnumSchema, SerializedFileSchema, SerializedImageSchema, SerializedKeyOfSchema, SerializedLiteralSchema, SerializedLocaleSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedRecordSchema, SerializedRichTextSchema, SerializedRouteSchema, SerializedSchema, SerializedSettingsSchema, SerializedStringSchema, SharedValConfig, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, ValCommit, ValDeployment, buildDuplicatePatch, buildEmptyAtPathPatch, buildFileRefValue, buildImageGalleryPatch, buildRemoveImageGalleryEntryPatch, buildSearchIndex, checkCompatibility, chooseEncoded, concatModulePath, createRegExpFromPattern, createSearchIndex, createValClient, declaredLocales, describeContainerAtPath, emptyOf, explainIncompatible, filterBlockingValidationErrors, filterRoutesByPatterns, findSimilar, fitWithin, flattenRichText, getErrorMessageFromUnknownJson, getFilenameFromRef, getInlineImgInfo, getKeyOfRecordAt, getRefParts, getRoutesOf, getRoutesWithModulePaths, getSourceAt, indexModule, isMimeTypeAccepted, isRemoteSchema, isSchemaSourceFixError, isSkippedSource, keyOfRecordPath, levenshtein, localeAt, newestCommitSha, partitionValidationErrors, performSearch, remirrorToRichTextSource, removeModule, resolveEncodeSettings, resolveSchemaSourceFixForError, resolveSchemaSourceFixes, resolveSerializedSchemaAtPath, richTextToRemirror, safeParsePatch, sourcePathOfChild, toolNames, traverseSchemaSource, urlOf, validateRoutePatterns, withExtension };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valbuild/shared",
3
- "version": "0.126.0",
3
+ "version": "0.127.0",
4
4
  "private": false,
5
5
  "description": "Val shared types and utilities",
6
6
  "repository": {
@@ -37,7 +37,7 @@
37
37
  "flexsearch": "^0.8.212",
38
38
  "zod": "^4.4.3",
39
39
  "zod-validation-error": "^5.0.0",
40
- "@valbuild/core": "0.126.0"
40
+ "@valbuild/core": "0.127.0"
41
41
  },
42
42
  "files": [
43
43
  "CHANGELOG.md",