@valbuild/shared 0.97.0 → 0.97.1

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.
@@ -758,6 +758,7 @@ export declare const Api: {
758
758
  validate_sources: z.ZodOptional<z.ZodPipe<z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<string, string[]>> & z.ZodType<"true" | "false", string[], z.core.$ZodTypeInternals<"true" | "false", string[]>>, z.ZodTransform<boolean, "true" | "false">>>;
759
759
  validate_binary_files: z.ZodOptional<z.ZodPipe<z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<string, string[]>> & z.ZodType<"true" | "false", string[], z.core.$ZodTypeInternals<"true" | "false", string[]>>, z.ZodTransform<boolean, "true" | "false">>>;
760
760
  exclude_patches: z.ZodOptional<z.ZodPipe<z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<string, string[]>> & z.ZodType<"true" | "false", string[], z.core.$ZodTypeInternals<"true" | "false", string[]>>, z.ZodTransform<boolean, "true" | "false">>>;
761
+ apply_patches: z.ZodOptional<z.ZodPipe<z.ZodPipe<z.ZodArray<z.ZodString>, z.ZodTransform<string, string[]>> & z.ZodType<"true" | "false", string[], z.core.$ZodTypeInternals<"true" | "false", string[]>>, z.ZodTransform<boolean, "true" | "false">>>;
761
762
  };
762
763
  cookies: {
763
764
  val_session: z.ZodOptional<z.ZodString>;
@@ -12,5 +12,6 @@ export * from "./getSitemapTree.js";
12
12
  export * from "./parseRoutePattern.js";
13
13
  export * from "./getNextAppRouterSourceFolder.js";
14
14
  export * from "./routeValidation.js";
15
+ export * from "./resolveSchemaSourceFixes.js";
15
16
  export * from "./getErrorMessageFromUnknownJson.js";
16
17
  export * from "./zod/SerializedSchema.js";
@@ -0,0 +1,42 @@
1
+ import { type Json, type ModuleFilePath, type SerializedSchema, type SourcePath, type ValidationError } from "@valbuild/core";
2
+ export type SchemaSourceSnapshot = {
3
+ schemas: Record<ModuleFilePath, SerializedSchema>;
4
+ sources: Record<ModuleFilePath, Json>;
5
+ };
6
+ export type RouteInfo = {
7
+ route: string;
8
+ moduleFilePath: ModuleFilePath;
9
+ };
10
+ export declare function getRoutesWithModulePaths(schemas: Record<ModuleFilePath, SerializedSchema>, sources: Record<ModuleFilePath, Json>): RouteInfo[];
11
+ export declare function getRoutesOf(schemas: Record<ModuleFilePath, SerializedSchema>, sources: Record<ModuleFilePath, Json>): string[];
12
+ export declare const levenshtein: (a: string, b: string) => number;
13
+ export declare function findSimilar(key: string, targets: string[]): {
14
+ target: string;
15
+ distance: number;
16
+ }[];
17
+ export type ResolvedFix = {
18
+ status: "resolved";
19
+ } | {
20
+ status: "remaining";
21
+ error: ValidationError;
22
+ };
23
+ /**
24
+ * Resolves a single `keyof:check-keys` or `router:check-route` error against
25
+ * the given schema/source snapshot. Returns:
26
+ * - null if the error is not one of these fixes (caller should pass through)
27
+ * - { status: "resolved" } if the referenced key/route is valid (drop error)
28
+ * - { status: "remaining"; error } if invalid (rewritten error to surface)
29
+ */
30
+ export declare function resolveSchemaSourceFixForError(error: ValidationError, snapshot: SchemaSourceSnapshot): ResolvedFix | null;
31
+ /**
32
+ * Apply schema/source fix resolution across a map of validation errors.
33
+ *
34
+ * Errors with `keyof:check-keys` or `router:check-route` are resolved against
35
+ * the in-memory schema/source snapshot — valid references drop the error,
36
+ * invalid ones are rewritten with a "did you mean…" message and have their
37
+ * `fixes` cleared (so downstream code reports them as plain validation errors).
38
+ * All other errors pass through untouched.
39
+ *
40
+ * Source paths with no remaining errors are removed from the result.
41
+ */
42
+ export declare function resolveSchemaSourceFixes(validationErrors: Record<SourcePath, ValidationError[]>, snapshot: SchemaSourceSnapshot): Record<SourcePath, ValidationError[]>;
@@ -1841,7 +1841,8 @@ var Api = {
1841
1841
  query: {
1842
1842
  validate_sources: onlyOneBooleanQueryParam.optional(),
1843
1843
  validate_binary_files: onlyOneBooleanQueryParam.optional(),
1844
- exclude_patches: onlyOneBooleanQueryParam.optional()
1844
+ exclude_patches: onlyOneBooleanQueryParam.optional(),
1845
+ apply_patches: onlyOneBooleanQueryParam.optional()
1845
1846
  },
1846
1847
  cookies: {
1847
1848
  val_session: zod.z.string().optional()
@@ -1878,7 +1879,7 @@ var Api = {
1878
1879
  source: zod.z.any().optional(),
1879
1880
  //.optional(), // TODO: Json zod type
1880
1881
  baseSource: zod.z.any().optional(),
1881
- // pre-patch source for compare view; set only when the module has pending patches
1882
+ // pre-patch source for compare view; only set when the server applies patches (apply_patches=true) and the module has pending patches
1882
1883
  patches: zod.z.object({
1883
1884
  applied: zod.z.array(PatchId),
1884
1885
  skipped: zod.z.array(PatchId).optional(),
@@ -2794,6 +2795,381 @@ function createRegExpFromPattern(pattern) {
2794
2795
  }
2795
2796
  }
2796
2797
 
2798
+ var TYPE_ERROR_MESSAGE = "This is most likely a Val version mismatch or Val bug.";
2799
+ function getRoutesWithModulePaths(schemas, sources) {
2800
+ var routeMap = new Map();
2801
+ for (var moduleFilePathS in schemas) {
2802
+ var moduleFilePath = moduleFilePathS;
2803
+ var schema = schemas[moduleFilePath];
2804
+ var source = sources[moduleFilePath];
2805
+ if (schema && schema.type === "record" && schema.router && source && _typeof(source) === "object" && !Array.isArray(source)) {
2806
+ for (var key in source) {
2807
+ if (!routeMap.has(key)) {
2808
+ routeMap.set(key, moduleFilePath);
2809
+ }
2810
+ }
2811
+ }
2812
+ }
2813
+ var routes = Array.from(routeMap.entries()).map(function (_ref) {
2814
+ var _ref2 = _slicedToArray(_ref, 2),
2815
+ route = _ref2[0],
2816
+ moduleFilePath = _ref2[1];
2817
+ return {
2818
+ route: route,
2819
+ moduleFilePath: moduleFilePath
2820
+ };
2821
+ });
2822
+ return routes.sort(function (a, b) {
2823
+ return a.route.localeCompare(b.route);
2824
+ });
2825
+ }
2826
+ function getRoutesOf(schemas, sources) {
2827
+ return getRoutesWithModulePaths(schemas, sources).map(function (r) {
2828
+ return r.route;
2829
+ });
2830
+ }
2831
+
2832
+ // GPT generated levenshtein distance algorithm
2833
+ var levenshtein = function levenshtein(a, b) {
2834
+ var _ref3 = [a.length, b.length],
2835
+ m = _ref3[0],
2836
+ n = _ref3[1];
2837
+ if (!m || !n) return Math.max(m, n);
2838
+ var dp = Array.from({
2839
+ length: m + 1
2840
+ }, function (_, i) {
2841
+ return i;
2842
+ });
2843
+ for (var j = 1; j <= n; j++) {
2844
+ var prev = dp[0];
2845
+ dp[0] = j;
2846
+ for (var i = 1; i <= m; i++) {
2847
+ var temp = dp[i];
2848
+ dp[i] = a[i - 1] === b[j - 1] ? prev : Math.min(prev + 1, dp[i - 1] + 1, dp[i] + 1);
2849
+ prev = temp;
2850
+ }
2851
+ }
2852
+ return dp[m];
2853
+ };
2854
+ function findSimilar(key, targets) {
2855
+ return targets.map(function (target) {
2856
+ return {
2857
+ target: target,
2858
+ distance: levenshtein(key, target)
2859
+ };
2860
+ }).sort(function (a, b) {
2861
+ return a.distance - b.distance;
2862
+ });
2863
+ }
2864
+ function getKeyOfRecordAt(sourcePath, snapshot) {
2865
+ var _Internal$splitModule = core.Internal.splitModuleFilePathAndModulePath(sourcePath),
2866
+ _Internal$splitModule2 = _slicedToArray(_Internal$splitModule, 2),
2867
+ moduleFilePath = _Internal$splitModule2[0],
2868
+ modulePath = _Internal$splitModule2[1];
2869
+ var moduleSource = snapshot.sources[moduleFilePath];
2870
+ var moduleSchema = snapshot.schemas[moduleFilePath];
2871
+ if (!moduleSchema || moduleSource === undefined) {
2872
+ return {
2873
+ source: undefined,
2874
+ schema: undefined
2875
+ };
2876
+ }
2877
+ if (!modulePath) {
2878
+ return {
2879
+ source: moduleSource,
2880
+ schema: moduleSchema
2881
+ };
2882
+ }
2883
+ // Walk both schema and source. This intentionally only follows the
2884
+ // patterns produced by keyOf().sourcePath — typically a top-level record or
2885
+ // an object property pointing at a record. The walk is best-effort and
2886
+ // returns undefined source on any mismatch.
2887
+ var source = moduleSource;
2888
+ var schema = moduleSchema;
2889
+ var _iterator = _createForOfIteratorHelper(core.Internal.splitModulePath(modulePath)),
2890
+ _step;
2891
+ try {
2892
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
2893
+ var part = _step.value;
2894
+ if (source && _typeof(source) === "object" && !Array.isArray(source) && part in source) {
2895
+ source = source[part];
2896
+ } else {
2897
+ source = undefined;
2898
+ }
2899
+ if (schema && schema.type === "object" && schema.items[part]) {
2900
+ schema = schema.items[part];
2901
+ } else if (schema && schema.type === "record") {
2902
+ schema = schema.item;
2903
+ } else {
2904
+ schema = undefined;
2905
+ }
2906
+ }
2907
+ } catch (err) {
2908
+ _iterator.e(err);
2909
+ } finally {
2910
+ _iterator.f();
2911
+ }
2912
+ return {
2913
+ source: source,
2914
+ schema: schema
2915
+ };
2916
+ }
2917
+ function checkKeyIsValid(key, sourcePath, snapshot) {
2918
+ var _getKeyOfRecordAt = getKeyOfRecordAt(sourcePath, snapshot),
2919
+ keyOfModuleSource = _getKeyOfRecordAt.source,
2920
+ keyOfModuleSchema = _getKeyOfRecordAt.schema;
2921
+ if (keyOfModuleSchema && keyOfModuleSchema.type !== "record") {
2922
+ return {
2923
+ error: true,
2924
+ message: "Expected key at ".concat(sourcePath, " to be of type 'record'")
2925
+ };
2926
+ }
2927
+ if (keyOfModuleSource && _typeof(keyOfModuleSource) === "object" && !Array.isArray(keyOfModuleSource) && key in keyOfModuleSource) {
2928
+ return {
2929
+ error: false
2930
+ };
2931
+ }
2932
+ if (!keyOfModuleSource || _typeof(keyOfModuleSource) !== "object" || Array.isArray(keyOfModuleSource)) {
2933
+ return {
2934
+ error: true,
2935
+ message: "Expected ".concat(sourcePath, " to be a truthy object")
2936
+ };
2937
+ }
2938
+ var alternatives = findSimilar(key, Object.keys(keyOfModuleSource));
2939
+ if (alternatives.length === 0) {
2940
+ return {
2941
+ error: true,
2942
+ message: "Key '".concat(key, "' does not exist in ").concat(sourcePath, ".")
2943
+ };
2944
+ }
2945
+ return {
2946
+ error: true,
2947
+ message: "Key '".concat(key, "' does not exist in ").concat(sourcePath, ". Closest match: '").concat(alternatives[0].target, "'. Other similar: ").concat(alternatives.slice(1, 4).map(function (a) {
2948
+ return "'".concat(a.target, "'");
2949
+ }).join(", ")).concat(alternatives.length > 4 ? ", ..." : "")
2950
+ };
2951
+ }
2952
+ function checkRouteIsValid(route, includePattern, excludePattern, snapshot) {
2953
+ var routerModules = {};
2954
+ for (var moduleFilePathS in snapshot.schemas) {
2955
+ var moduleFilePath = moduleFilePathS;
2956
+ var schema = snapshot.schemas[moduleFilePath];
2957
+ if ((schema === null || schema === void 0 ? void 0 : schema.type) === "record" && schema.router) {
2958
+ var source = snapshot.sources[moduleFilePath];
2959
+ if (source && _typeof(source) === "object" && !Array.isArray(source)) {
2960
+ routerModules[moduleFilePath] = Object.keys(source);
2961
+ }
2962
+ }
2963
+ }
2964
+ var moduleEntries = Object.entries(routerModules);
2965
+ var foundInModule = null;
2966
+ for (var _i = 0, _moduleEntries = moduleEntries; _i < _moduleEntries.length; _i++) {
2967
+ var _moduleEntries$_i = _slicedToArray(_moduleEntries[_i], 2),
2968
+ _moduleFilePath = _moduleEntries$_i[0],
2969
+ routes = _moduleEntries$_i[1];
2970
+ if (routes.includes(route)) {
2971
+ foundInModule = _moduleFilePath;
2972
+ break;
2973
+ }
2974
+ }
2975
+ if (!foundInModule) {
2976
+ var allRoutes = moduleEntries.flatMap(function (_ref4) {
2977
+ var _ref5 = _slicedToArray(_ref4, 2),
2978
+ routes = _ref5[1];
2979
+ return routes;
2980
+ });
2981
+ if (allRoutes.length === 0) {
2982
+ return {
2983
+ error: true,
2984
+ message: "Route '".concat(route, "' could not be validated: No router modules found in the project. Use s.record(...).router(...) to define router modules.")
2985
+ };
2986
+ }
2987
+ allRoutes = filterRoutesByPatterns(allRoutes, includePattern, excludePattern);
2988
+ var alternatives = findSimilar(route, allRoutes);
2989
+ return {
2990
+ error: true,
2991
+ message: "Route '".concat(route, "' does not exist in any router module. ").concat(alternatives.length > 0 ? "Closest match: '".concat(alternatives[0].target, "'. Other similar: ").concat(alternatives.slice(1, 4).map(function (a) {
2992
+ return "'".concat(a.target, "'");
2993
+ }).join(", ")).concat(alternatives.length > 4 ? ", ..." : "") : "No similar routes found.")
2994
+ };
2995
+ }
2996
+ var patternValidation = validateRoutePatterns(route, includePattern, excludePattern);
2997
+ if (!patternValidation.valid) {
2998
+ return {
2999
+ error: true,
3000
+ message: patternValidation.message
3001
+ };
3002
+ }
3003
+ return {
3004
+ error: false
3005
+ };
3006
+ }
3007
+ /**
3008
+ * Resolves a single `keyof:check-keys` or `router:check-route` error against
3009
+ * the given schema/source snapshot. Returns:
3010
+ * - null if the error is not one of these fixes (caller should pass through)
3011
+ * - { status: "resolved" } if the referenced key/route is valid (drop error)
3012
+ * - { status: "remaining"; error } if invalid (rewritten error to surface)
3013
+ */
3014
+ function resolveSchemaSourceFixForError(error, snapshot) {
3015
+ var _error$fixes;
3016
+ var fixes = (_error$fixes = error.fixes) !== null && _error$fixes !== void 0 ? _error$fixes : [];
3017
+ if (fixes.includes("keyof:check-keys")) {
3018
+ if (!error.value) {
3019
+ return {
3020
+ status: "remaining",
3021
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3022
+ message: "Could not find a value for keyOf. ".concat(TYPE_ERROR_MESSAGE),
3023
+ typeError: true,
3024
+ fixes: undefined
3025
+ })
3026
+ };
3027
+ }
3028
+ if (_typeof(error.value) !== "object") {
3029
+ return {
3030
+ status: "remaining",
3031
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3032
+ message: "Expected keyOf validation error to have a 'value' property of type 'object'. Found: ".concat(_typeof(error.value), ". ").concat(TYPE_ERROR_MESSAGE),
3033
+ typeError: true,
3034
+ fixes: undefined
3035
+ })
3036
+ };
3037
+ }
3038
+ var valueObj = error.value;
3039
+ var key = valueObj.key;
3040
+ var valueSourcePath = valueObj.sourcePath;
3041
+ if (typeof key !== "string") {
3042
+ return {
3043
+ status: "remaining",
3044
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3045
+ message: "Expected keyOf validation error 'value' to have property 'key' of type 'string'. Found: ".concat(_typeof(key), ". ").concat(TYPE_ERROR_MESSAGE),
3046
+ typeError: true,
3047
+ fixes: undefined
3048
+ })
3049
+ };
3050
+ }
3051
+ if (typeof valueSourcePath !== "string") {
3052
+ return {
3053
+ status: "remaining",
3054
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3055
+ message: "Expected keyOf validation error 'value' to have property 'sourcePath' of type 'string'. Found: ".concat(_typeof(valueSourcePath), ". ").concat(TYPE_ERROR_MESSAGE),
3056
+ typeError: true,
3057
+ fixes: undefined
3058
+ })
3059
+ };
3060
+ }
3061
+ var res = checkKeyIsValid(key, valueSourcePath, snapshot);
3062
+ if (res.error) {
3063
+ return {
3064
+ status: "remaining",
3065
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3066
+ message: res.message,
3067
+ fixes: undefined
3068
+ })
3069
+ };
3070
+ }
3071
+ return {
3072
+ status: "resolved"
3073
+ };
3074
+ }
3075
+ if (fixes.includes("router:check-route")) {
3076
+ if (!error.value) {
3077
+ return {
3078
+ status: "remaining",
3079
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3080
+ message: "Could not find a value for route. ".concat(TYPE_ERROR_MESSAGE),
3081
+ typeError: true,
3082
+ fixes: undefined
3083
+ })
3084
+ };
3085
+ }
3086
+ if (_typeof(error.value) !== "object") {
3087
+ return {
3088
+ status: "remaining",
3089
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3090
+ message: "Expected route validation error to have a 'value' property of type 'object'. Found: ".concat(_typeof(error.value), ". ").concat(TYPE_ERROR_MESSAGE),
3091
+ typeError: true,
3092
+ fixes: undefined
3093
+ })
3094
+ };
3095
+ }
3096
+ var _valueObj = error.value;
3097
+ var route = _valueObj.route;
3098
+ if (typeof route !== "string") {
3099
+ return {
3100
+ status: "remaining",
3101
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3102
+ message: "Expected route validation error 'value' to have property 'route' of type 'string'. Found: ".concat(_typeof(route), ". ").concat(TYPE_ERROR_MESSAGE),
3103
+ typeError: true,
3104
+ fixes: undefined
3105
+ })
3106
+ };
3107
+ }
3108
+ var include = asRegExpPattern(_valueObj.include);
3109
+ var exclude = asRegExpPattern(_valueObj.exclude);
3110
+ var _res = checkRouteIsValid(route, include, exclude, snapshot);
3111
+ if (_res.error) {
3112
+ return {
3113
+ status: "remaining",
3114
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3115
+ message: _res.message,
3116
+ fixes: undefined
3117
+ })
3118
+ };
3119
+ }
3120
+ return {
3121
+ status: "resolved"
3122
+ };
3123
+ }
3124
+ return null;
3125
+ }
3126
+ function asRegExpPattern(value) {
3127
+ if (value && _typeof(value) === "object" && "source" in value && "flags" in value && typeof value.source === "string" && typeof value.flags === "string") {
3128
+ return value;
3129
+ }
3130
+ return undefined;
3131
+ }
3132
+
3133
+ /**
3134
+ * Apply schema/source fix resolution across a map of validation errors.
3135
+ *
3136
+ * Errors with `keyof:check-keys` or `router:check-route` are resolved against
3137
+ * the in-memory schema/source snapshot — valid references drop the error,
3138
+ * invalid ones are rewritten with a "did you mean…" message and have their
3139
+ * `fixes` cleared (so downstream code reports them as plain validation errors).
3140
+ * All other errors pass through untouched.
3141
+ *
3142
+ * Source paths with no remaining errors are removed from the result.
3143
+ */
3144
+ function resolveSchemaSourceFixes(validationErrors, snapshot) {
3145
+ var result = {};
3146
+ for (var sourcePathS in validationErrors) {
3147
+ var sourcePath = sourcePathS;
3148
+ var remaining = [];
3149
+ var _iterator2 = _createForOfIteratorHelper(validationErrors[sourcePath]),
3150
+ _step2;
3151
+ try {
3152
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
3153
+ var error = _step2.value;
3154
+ var resolved = resolveSchemaSourceFixForError(error, snapshot);
3155
+ if (resolved === null) {
3156
+ remaining.push(error);
3157
+ } else if (resolved.status === "remaining") {
3158
+ remaining.push(resolved.error);
3159
+ }
3160
+ }
3161
+ } catch (err) {
3162
+ _iterator2.e(err);
3163
+ } finally {
3164
+ _iterator2.f();
3165
+ }
3166
+ if (remaining.length > 0) {
3167
+ result[sourcePath] = remaining;
3168
+ }
3169
+ }
3170
+ return result;
3171
+ }
3172
+
2797
3173
  exports.Api = Api;
2798
3174
  exports.DEFAULT_ERROR_MESSAGE = DEFAULT_ERROR_MESSAGE;
2799
3175
  exports.DateOptions = DateOptions;
@@ -2843,12 +3219,18 @@ exports.ValDeployment = ValDeployment;
2843
3219
  exports.createRegExpFromPattern = createRegExpFromPattern;
2844
3220
  exports.createValClient = createValClient;
2845
3221
  exports.filterRoutesByPatterns = filterRoutesByPatterns;
3222
+ exports.findSimilar = findSimilar;
2846
3223
  exports.getErrorMessageFromUnknownJson = getErrorMessageFromUnknownJson;
2847
3224
  exports.getNextAppRouterSitemapTree = getNextAppRouterSitemapTree;
2848
3225
  exports.getNextAppRouterSourceFolder = getNextAppRouterSourceFolder;
2849
3226
  exports.getPatternFromModuleFilePath = getPatternFromModuleFilePath;
3227
+ exports.getRoutesOf = getRoutesOf;
3228
+ exports.getRoutesWithModulePaths = getRoutesWithModulePaths;
3229
+ exports.levenshtein = levenshtein;
2850
3230
  exports.parseRoutePattern = parseRoutePattern;
2851
3231
  exports.remirrorToRichTextSource = remirrorToRichTextSource;
3232
+ exports.resolveSchemaSourceFixForError = resolveSchemaSourceFixForError;
3233
+ exports.resolveSchemaSourceFixes = resolveSchemaSourceFixes;
2852
3234
  exports.richTextToRemirror = richTextToRemirror;
2853
3235
  exports.urlOf = urlOf;
2854
3236
  exports.validateRoutePatterns = validateRoutePatterns;
@@ -1841,7 +1841,8 @@ var Api = {
1841
1841
  query: {
1842
1842
  validate_sources: onlyOneBooleanQueryParam.optional(),
1843
1843
  validate_binary_files: onlyOneBooleanQueryParam.optional(),
1844
- exclude_patches: onlyOneBooleanQueryParam.optional()
1844
+ exclude_patches: onlyOneBooleanQueryParam.optional(),
1845
+ apply_patches: onlyOneBooleanQueryParam.optional()
1845
1846
  },
1846
1847
  cookies: {
1847
1848
  val_session: zod.z.string().optional()
@@ -1878,7 +1879,7 @@ var Api = {
1878
1879
  source: zod.z.any().optional(),
1879
1880
  //.optional(), // TODO: Json zod type
1880
1881
  baseSource: zod.z.any().optional(),
1881
- // pre-patch source for compare view; set only when the module has pending patches
1882
+ // pre-patch source for compare view; only set when the server applies patches (apply_patches=true) and the module has pending patches
1882
1883
  patches: zod.z.object({
1883
1884
  applied: zod.z.array(PatchId),
1884
1885
  skipped: zod.z.array(PatchId).optional(),
@@ -2794,6 +2795,381 @@ function createRegExpFromPattern(pattern) {
2794
2795
  }
2795
2796
  }
2796
2797
 
2798
+ var TYPE_ERROR_MESSAGE = "This is most likely a Val version mismatch or Val bug.";
2799
+ function getRoutesWithModulePaths(schemas, sources) {
2800
+ var routeMap = new Map();
2801
+ for (var moduleFilePathS in schemas) {
2802
+ var moduleFilePath = moduleFilePathS;
2803
+ var schema = schemas[moduleFilePath];
2804
+ var source = sources[moduleFilePath];
2805
+ if (schema && schema.type === "record" && schema.router && source && _typeof(source) === "object" && !Array.isArray(source)) {
2806
+ for (var key in source) {
2807
+ if (!routeMap.has(key)) {
2808
+ routeMap.set(key, moduleFilePath);
2809
+ }
2810
+ }
2811
+ }
2812
+ }
2813
+ var routes = Array.from(routeMap.entries()).map(function (_ref) {
2814
+ var _ref2 = _slicedToArray(_ref, 2),
2815
+ route = _ref2[0],
2816
+ moduleFilePath = _ref2[1];
2817
+ return {
2818
+ route: route,
2819
+ moduleFilePath: moduleFilePath
2820
+ };
2821
+ });
2822
+ return routes.sort(function (a, b) {
2823
+ return a.route.localeCompare(b.route);
2824
+ });
2825
+ }
2826
+ function getRoutesOf(schemas, sources) {
2827
+ return getRoutesWithModulePaths(schemas, sources).map(function (r) {
2828
+ return r.route;
2829
+ });
2830
+ }
2831
+
2832
+ // GPT generated levenshtein distance algorithm
2833
+ var levenshtein = function levenshtein(a, b) {
2834
+ var _ref3 = [a.length, b.length],
2835
+ m = _ref3[0],
2836
+ n = _ref3[1];
2837
+ if (!m || !n) return Math.max(m, n);
2838
+ var dp = Array.from({
2839
+ length: m + 1
2840
+ }, function (_, i) {
2841
+ return i;
2842
+ });
2843
+ for (var j = 1; j <= n; j++) {
2844
+ var prev = dp[0];
2845
+ dp[0] = j;
2846
+ for (var i = 1; i <= m; i++) {
2847
+ var temp = dp[i];
2848
+ dp[i] = a[i - 1] === b[j - 1] ? prev : Math.min(prev + 1, dp[i - 1] + 1, dp[i] + 1);
2849
+ prev = temp;
2850
+ }
2851
+ }
2852
+ return dp[m];
2853
+ };
2854
+ function findSimilar(key, targets) {
2855
+ return targets.map(function (target) {
2856
+ return {
2857
+ target: target,
2858
+ distance: levenshtein(key, target)
2859
+ };
2860
+ }).sort(function (a, b) {
2861
+ return a.distance - b.distance;
2862
+ });
2863
+ }
2864
+ function getKeyOfRecordAt(sourcePath, snapshot) {
2865
+ var _Internal$splitModule = core.Internal.splitModuleFilePathAndModulePath(sourcePath),
2866
+ _Internal$splitModule2 = _slicedToArray(_Internal$splitModule, 2),
2867
+ moduleFilePath = _Internal$splitModule2[0],
2868
+ modulePath = _Internal$splitModule2[1];
2869
+ var moduleSource = snapshot.sources[moduleFilePath];
2870
+ var moduleSchema = snapshot.schemas[moduleFilePath];
2871
+ if (!moduleSchema || moduleSource === undefined) {
2872
+ return {
2873
+ source: undefined,
2874
+ schema: undefined
2875
+ };
2876
+ }
2877
+ if (!modulePath) {
2878
+ return {
2879
+ source: moduleSource,
2880
+ schema: moduleSchema
2881
+ };
2882
+ }
2883
+ // Walk both schema and source. This intentionally only follows the
2884
+ // patterns produced by keyOf().sourcePath — typically a top-level record or
2885
+ // an object property pointing at a record. The walk is best-effort and
2886
+ // returns undefined source on any mismatch.
2887
+ var source = moduleSource;
2888
+ var schema = moduleSchema;
2889
+ var _iterator = _createForOfIteratorHelper(core.Internal.splitModulePath(modulePath)),
2890
+ _step;
2891
+ try {
2892
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
2893
+ var part = _step.value;
2894
+ if (source && _typeof(source) === "object" && !Array.isArray(source) && part in source) {
2895
+ source = source[part];
2896
+ } else {
2897
+ source = undefined;
2898
+ }
2899
+ if (schema && schema.type === "object" && schema.items[part]) {
2900
+ schema = schema.items[part];
2901
+ } else if (schema && schema.type === "record") {
2902
+ schema = schema.item;
2903
+ } else {
2904
+ schema = undefined;
2905
+ }
2906
+ }
2907
+ } catch (err) {
2908
+ _iterator.e(err);
2909
+ } finally {
2910
+ _iterator.f();
2911
+ }
2912
+ return {
2913
+ source: source,
2914
+ schema: schema
2915
+ };
2916
+ }
2917
+ function checkKeyIsValid(key, sourcePath, snapshot) {
2918
+ var _getKeyOfRecordAt = getKeyOfRecordAt(sourcePath, snapshot),
2919
+ keyOfModuleSource = _getKeyOfRecordAt.source,
2920
+ keyOfModuleSchema = _getKeyOfRecordAt.schema;
2921
+ if (keyOfModuleSchema && keyOfModuleSchema.type !== "record") {
2922
+ return {
2923
+ error: true,
2924
+ message: "Expected key at ".concat(sourcePath, " to be of type 'record'")
2925
+ };
2926
+ }
2927
+ if (keyOfModuleSource && _typeof(keyOfModuleSource) === "object" && !Array.isArray(keyOfModuleSource) && key in keyOfModuleSource) {
2928
+ return {
2929
+ error: false
2930
+ };
2931
+ }
2932
+ if (!keyOfModuleSource || _typeof(keyOfModuleSource) !== "object" || Array.isArray(keyOfModuleSource)) {
2933
+ return {
2934
+ error: true,
2935
+ message: "Expected ".concat(sourcePath, " to be a truthy object")
2936
+ };
2937
+ }
2938
+ var alternatives = findSimilar(key, Object.keys(keyOfModuleSource));
2939
+ if (alternatives.length === 0) {
2940
+ return {
2941
+ error: true,
2942
+ message: "Key '".concat(key, "' does not exist in ").concat(sourcePath, ".")
2943
+ };
2944
+ }
2945
+ return {
2946
+ error: true,
2947
+ message: "Key '".concat(key, "' does not exist in ").concat(sourcePath, ". Closest match: '").concat(alternatives[0].target, "'. Other similar: ").concat(alternatives.slice(1, 4).map(function (a) {
2948
+ return "'".concat(a.target, "'");
2949
+ }).join(", ")).concat(alternatives.length > 4 ? ", ..." : "")
2950
+ };
2951
+ }
2952
+ function checkRouteIsValid(route, includePattern, excludePattern, snapshot) {
2953
+ var routerModules = {};
2954
+ for (var moduleFilePathS in snapshot.schemas) {
2955
+ var moduleFilePath = moduleFilePathS;
2956
+ var schema = snapshot.schemas[moduleFilePath];
2957
+ if ((schema === null || schema === void 0 ? void 0 : schema.type) === "record" && schema.router) {
2958
+ var source = snapshot.sources[moduleFilePath];
2959
+ if (source && _typeof(source) === "object" && !Array.isArray(source)) {
2960
+ routerModules[moduleFilePath] = Object.keys(source);
2961
+ }
2962
+ }
2963
+ }
2964
+ var moduleEntries = Object.entries(routerModules);
2965
+ var foundInModule = null;
2966
+ for (var _i = 0, _moduleEntries = moduleEntries; _i < _moduleEntries.length; _i++) {
2967
+ var _moduleEntries$_i = _slicedToArray(_moduleEntries[_i], 2),
2968
+ _moduleFilePath = _moduleEntries$_i[0],
2969
+ routes = _moduleEntries$_i[1];
2970
+ if (routes.includes(route)) {
2971
+ foundInModule = _moduleFilePath;
2972
+ break;
2973
+ }
2974
+ }
2975
+ if (!foundInModule) {
2976
+ var allRoutes = moduleEntries.flatMap(function (_ref4) {
2977
+ var _ref5 = _slicedToArray(_ref4, 2),
2978
+ routes = _ref5[1];
2979
+ return routes;
2980
+ });
2981
+ if (allRoutes.length === 0) {
2982
+ return {
2983
+ error: true,
2984
+ message: "Route '".concat(route, "' could not be validated: No router modules found in the project. Use s.record(...).router(...) to define router modules.")
2985
+ };
2986
+ }
2987
+ allRoutes = filterRoutesByPatterns(allRoutes, includePattern, excludePattern);
2988
+ var alternatives = findSimilar(route, allRoutes);
2989
+ return {
2990
+ error: true,
2991
+ message: "Route '".concat(route, "' does not exist in any router module. ").concat(alternatives.length > 0 ? "Closest match: '".concat(alternatives[0].target, "'. Other similar: ").concat(alternatives.slice(1, 4).map(function (a) {
2992
+ return "'".concat(a.target, "'");
2993
+ }).join(", ")).concat(alternatives.length > 4 ? ", ..." : "") : "No similar routes found.")
2994
+ };
2995
+ }
2996
+ var patternValidation = validateRoutePatterns(route, includePattern, excludePattern);
2997
+ if (!patternValidation.valid) {
2998
+ return {
2999
+ error: true,
3000
+ message: patternValidation.message
3001
+ };
3002
+ }
3003
+ return {
3004
+ error: false
3005
+ };
3006
+ }
3007
+ /**
3008
+ * Resolves a single `keyof:check-keys` or `router:check-route` error against
3009
+ * the given schema/source snapshot. Returns:
3010
+ * - null if the error is not one of these fixes (caller should pass through)
3011
+ * - { status: "resolved" } if the referenced key/route is valid (drop error)
3012
+ * - { status: "remaining"; error } if invalid (rewritten error to surface)
3013
+ */
3014
+ function resolveSchemaSourceFixForError(error, snapshot) {
3015
+ var _error$fixes;
3016
+ var fixes = (_error$fixes = error.fixes) !== null && _error$fixes !== void 0 ? _error$fixes : [];
3017
+ if (fixes.includes("keyof:check-keys")) {
3018
+ if (!error.value) {
3019
+ return {
3020
+ status: "remaining",
3021
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3022
+ message: "Could not find a value for keyOf. ".concat(TYPE_ERROR_MESSAGE),
3023
+ typeError: true,
3024
+ fixes: undefined
3025
+ })
3026
+ };
3027
+ }
3028
+ if (_typeof(error.value) !== "object") {
3029
+ return {
3030
+ status: "remaining",
3031
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3032
+ message: "Expected keyOf validation error to have a 'value' property of type 'object'. Found: ".concat(_typeof(error.value), ". ").concat(TYPE_ERROR_MESSAGE),
3033
+ typeError: true,
3034
+ fixes: undefined
3035
+ })
3036
+ };
3037
+ }
3038
+ var valueObj = error.value;
3039
+ var key = valueObj.key;
3040
+ var valueSourcePath = valueObj.sourcePath;
3041
+ if (typeof key !== "string") {
3042
+ return {
3043
+ status: "remaining",
3044
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3045
+ message: "Expected keyOf validation error 'value' to have property 'key' of type 'string'. Found: ".concat(_typeof(key), ". ").concat(TYPE_ERROR_MESSAGE),
3046
+ typeError: true,
3047
+ fixes: undefined
3048
+ })
3049
+ };
3050
+ }
3051
+ if (typeof valueSourcePath !== "string") {
3052
+ return {
3053
+ status: "remaining",
3054
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3055
+ message: "Expected keyOf validation error 'value' to have property 'sourcePath' of type 'string'. Found: ".concat(_typeof(valueSourcePath), ". ").concat(TYPE_ERROR_MESSAGE),
3056
+ typeError: true,
3057
+ fixes: undefined
3058
+ })
3059
+ };
3060
+ }
3061
+ var res = checkKeyIsValid(key, valueSourcePath, snapshot);
3062
+ if (res.error) {
3063
+ return {
3064
+ status: "remaining",
3065
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3066
+ message: res.message,
3067
+ fixes: undefined
3068
+ })
3069
+ };
3070
+ }
3071
+ return {
3072
+ status: "resolved"
3073
+ };
3074
+ }
3075
+ if (fixes.includes("router:check-route")) {
3076
+ if (!error.value) {
3077
+ return {
3078
+ status: "remaining",
3079
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3080
+ message: "Could not find a value for route. ".concat(TYPE_ERROR_MESSAGE),
3081
+ typeError: true,
3082
+ fixes: undefined
3083
+ })
3084
+ };
3085
+ }
3086
+ if (_typeof(error.value) !== "object") {
3087
+ return {
3088
+ status: "remaining",
3089
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3090
+ message: "Expected route validation error to have a 'value' property of type 'object'. Found: ".concat(_typeof(error.value), ". ").concat(TYPE_ERROR_MESSAGE),
3091
+ typeError: true,
3092
+ fixes: undefined
3093
+ })
3094
+ };
3095
+ }
3096
+ var _valueObj = error.value;
3097
+ var route = _valueObj.route;
3098
+ if (typeof route !== "string") {
3099
+ return {
3100
+ status: "remaining",
3101
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3102
+ message: "Expected route validation error 'value' to have property 'route' of type 'string'. Found: ".concat(_typeof(route), ". ").concat(TYPE_ERROR_MESSAGE),
3103
+ typeError: true,
3104
+ fixes: undefined
3105
+ })
3106
+ };
3107
+ }
3108
+ var include = asRegExpPattern(_valueObj.include);
3109
+ var exclude = asRegExpPattern(_valueObj.exclude);
3110
+ var _res = checkRouteIsValid(route, include, exclude, snapshot);
3111
+ if (_res.error) {
3112
+ return {
3113
+ status: "remaining",
3114
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3115
+ message: _res.message,
3116
+ fixes: undefined
3117
+ })
3118
+ };
3119
+ }
3120
+ return {
3121
+ status: "resolved"
3122
+ };
3123
+ }
3124
+ return null;
3125
+ }
3126
+ function asRegExpPattern(value) {
3127
+ if (value && _typeof(value) === "object" && "source" in value && "flags" in value && typeof value.source === "string" && typeof value.flags === "string") {
3128
+ return value;
3129
+ }
3130
+ return undefined;
3131
+ }
3132
+
3133
+ /**
3134
+ * Apply schema/source fix resolution across a map of validation errors.
3135
+ *
3136
+ * Errors with `keyof:check-keys` or `router:check-route` are resolved against
3137
+ * the in-memory schema/source snapshot — valid references drop the error,
3138
+ * invalid ones are rewritten with a "did you mean…" message and have their
3139
+ * `fixes` cleared (so downstream code reports them as plain validation errors).
3140
+ * All other errors pass through untouched.
3141
+ *
3142
+ * Source paths with no remaining errors are removed from the result.
3143
+ */
3144
+ function resolveSchemaSourceFixes(validationErrors, snapshot) {
3145
+ var result = {};
3146
+ for (var sourcePathS in validationErrors) {
3147
+ var sourcePath = sourcePathS;
3148
+ var remaining = [];
3149
+ var _iterator2 = _createForOfIteratorHelper(validationErrors[sourcePath]),
3150
+ _step2;
3151
+ try {
3152
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
3153
+ var error = _step2.value;
3154
+ var resolved = resolveSchemaSourceFixForError(error, snapshot);
3155
+ if (resolved === null) {
3156
+ remaining.push(error);
3157
+ } else if (resolved.status === "remaining") {
3158
+ remaining.push(resolved.error);
3159
+ }
3160
+ }
3161
+ } catch (err) {
3162
+ _iterator2.e(err);
3163
+ } finally {
3164
+ _iterator2.f();
3165
+ }
3166
+ if (remaining.length > 0) {
3167
+ result[sourcePath] = remaining;
3168
+ }
3169
+ }
3170
+ return result;
3171
+ }
3172
+
2797
3173
  exports.Api = Api;
2798
3174
  exports.DEFAULT_ERROR_MESSAGE = DEFAULT_ERROR_MESSAGE;
2799
3175
  exports.DateOptions = DateOptions;
@@ -2843,12 +3219,18 @@ exports.ValDeployment = ValDeployment;
2843
3219
  exports.createRegExpFromPattern = createRegExpFromPattern;
2844
3220
  exports.createValClient = createValClient;
2845
3221
  exports.filterRoutesByPatterns = filterRoutesByPatterns;
3222
+ exports.findSimilar = findSimilar;
2846
3223
  exports.getErrorMessageFromUnknownJson = getErrorMessageFromUnknownJson;
2847
3224
  exports.getNextAppRouterSitemapTree = getNextAppRouterSitemapTree;
2848
3225
  exports.getNextAppRouterSourceFolder = getNextAppRouterSourceFolder;
2849
3226
  exports.getPatternFromModuleFilePath = getPatternFromModuleFilePath;
3227
+ exports.getRoutesOf = getRoutesOf;
3228
+ exports.getRoutesWithModulePaths = getRoutesWithModulePaths;
3229
+ exports.levenshtein = levenshtein;
2850
3230
  exports.parseRoutePattern = parseRoutePattern;
2851
3231
  exports.remirrorToRichTextSource = remirrorToRichTextSource;
3232
+ exports.resolveSchemaSourceFixForError = resolveSchemaSourceFixForError;
3233
+ exports.resolveSchemaSourceFixes = resolveSchemaSourceFixes;
2852
3234
  exports.richTextToRemirror = richTextToRemirror;
2853
3235
  exports.urlOf = urlOf;
2854
3236
  exports.validateRoutePatterns = validateRoutePatterns;
@@ -1837,7 +1837,8 @@ var Api = {
1837
1837
  query: {
1838
1838
  validate_sources: onlyOneBooleanQueryParam.optional(),
1839
1839
  validate_binary_files: onlyOneBooleanQueryParam.optional(),
1840
- exclude_patches: onlyOneBooleanQueryParam.optional()
1840
+ exclude_patches: onlyOneBooleanQueryParam.optional(),
1841
+ apply_patches: onlyOneBooleanQueryParam.optional()
1841
1842
  },
1842
1843
  cookies: {
1843
1844
  val_session: z.string().optional()
@@ -1874,7 +1875,7 @@ var Api = {
1874
1875
  source: z.any().optional(),
1875
1876
  //.optional(), // TODO: Json zod type
1876
1877
  baseSource: z.any().optional(),
1877
- // pre-patch source for compare view; set only when the module has pending patches
1878
+ // pre-patch source for compare view; only set when the server applies patches (apply_patches=true) and the module has pending patches
1878
1879
  patches: z.object({
1879
1880
  applied: z.array(PatchId),
1880
1881
  skipped: z.array(PatchId).optional(),
@@ -2790,4 +2791,379 @@ function createRegExpFromPattern(pattern) {
2790
2791
  }
2791
2792
  }
2792
2793
 
2793
- export { Api, DEFAULT_ERROR_MESSAGE, DateOptions, FileOptions, ImageOptions, ParentRef$1 as ParentRef, Patch, PatchBlock, PatchId, PatchJSON, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, RichTextOptions, SerializedArraySchema, SerializedBooleanSchema, SerializedDateSchema, SerializedDateTimeSchema, SerializedFileSchema, SerializedImageSchema, SerializedKeyOfSchema, SerializedLiteralSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedRecordSchema, SerializedRichTextSchema, SerializedRouteSchema, SerializedSchema, SerializedStringSchema, SerializedUnionSchema, SharedValConfig, VAL_CONFIG_SESSION_STORAGE_KEY, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, VAL_THEME_SESSION_STORAGE_KEY, ValCommit, ValDeployment, createRegExpFromPattern, createValClient, filterRoutesByPatterns, getErrorMessageFromUnknownJson, getNextAppRouterSitemapTree, getNextAppRouterSourceFolder, getPatternFromModuleFilePath, parseRoutePattern, remirrorToRichTextSource, richTextToRemirror, urlOf, validateRoutePatterns };
2794
+ var TYPE_ERROR_MESSAGE = "This is most likely a Val version mismatch or Val bug.";
2795
+ function getRoutesWithModulePaths(schemas, sources) {
2796
+ var routeMap = new Map();
2797
+ for (var moduleFilePathS in schemas) {
2798
+ var moduleFilePath = moduleFilePathS;
2799
+ var schema = schemas[moduleFilePath];
2800
+ var source = sources[moduleFilePath];
2801
+ if (schema && schema.type === "record" && schema.router && source && _typeof(source) === "object" && !Array.isArray(source)) {
2802
+ for (var key in source) {
2803
+ if (!routeMap.has(key)) {
2804
+ routeMap.set(key, moduleFilePath);
2805
+ }
2806
+ }
2807
+ }
2808
+ }
2809
+ var routes = Array.from(routeMap.entries()).map(function (_ref) {
2810
+ var _ref2 = _slicedToArray(_ref, 2),
2811
+ route = _ref2[0],
2812
+ moduleFilePath = _ref2[1];
2813
+ return {
2814
+ route: route,
2815
+ moduleFilePath: moduleFilePath
2816
+ };
2817
+ });
2818
+ return routes.sort(function (a, b) {
2819
+ return a.route.localeCompare(b.route);
2820
+ });
2821
+ }
2822
+ function getRoutesOf(schemas, sources) {
2823
+ return getRoutesWithModulePaths(schemas, sources).map(function (r) {
2824
+ return r.route;
2825
+ });
2826
+ }
2827
+
2828
+ // GPT generated levenshtein distance algorithm
2829
+ var levenshtein = function levenshtein(a, b) {
2830
+ var _ref3 = [a.length, b.length],
2831
+ m = _ref3[0],
2832
+ n = _ref3[1];
2833
+ if (!m || !n) return Math.max(m, n);
2834
+ var dp = Array.from({
2835
+ length: m + 1
2836
+ }, function (_, i) {
2837
+ return i;
2838
+ });
2839
+ for (var j = 1; j <= n; j++) {
2840
+ var prev = dp[0];
2841
+ dp[0] = j;
2842
+ for (var i = 1; i <= m; i++) {
2843
+ var temp = dp[i];
2844
+ dp[i] = a[i - 1] === b[j - 1] ? prev : Math.min(prev + 1, dp[i - 1] + 1, dp[i] + 1);
2845
+ prev = temp;
2846
+ }
2847
+ }
2848
+ return dp[m];
2849
+ };
2850
+ function findSimilar(key, targets) {
2851
+ return targets.map(function (target) {
2852
+ return {
2853
+ target: target,
2854
+ distance: levenshtein(key, target)
2855
+ };
2856
+ }).sort(function (a, b) {
2857
+ return a.distance - b.distance;
2858
+ });
2859
+ }
2860
+ function getKeyOfRecordAt(sourcePath, snapshot) {
2861
+ var _Internal$splitModule = Internal.splitModuleFilePathAndModulePath(sourcePath),
2862
+ _Internal$splitModule2 = _slicedToArray(_Internal$splitModule, 2),
2863
+ moduleFilePath = _Internal$splitModule2[0],
2864
+ modulePath = _Internal$splitModule2[1];
2865
+ var moduleSource = snapshot.sources[moduleFilePath];
2866
+ var moduleSchema = snapshot.schemas[moduleFilePath];
2867
+ if (!moduleSchema || moduleSource === undefined) {
2868
+ return {
2869
+ source: undefined,
2870
+ schema: undefined
2871
+ };
2872
+ }
2873
+ if (!modulePath) {
2874
+ return {
2875
+ source: moduleSource,
2876
+ schema: moduleSchema
2877
+ };
2878
+ }
2879
+ // Walk both schema and source. This intentionally only follows the
2880
+ // patterns produced by keyOf().sourcePath — typically a top-level record or
2881
+ // an object property pointing at a record. The walk is best-effort and
2882
+ // returns undefined source on any mismatch.
2883
+ var source = moduleSource;
2884
+ var schema = moduleSchema;
2885
+ var _iterator = _createForOfIteratorHelper(Internal.splitModulePath(modulePath)),
2886
+ _step;
2887
+ try {
2888
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
2889
+ var part = _step.value;
2890
+ if (source && _typeof(source) === "object" && !Array.isArray(source) && part in source) {
2891
+ source = source[part];
2892
+ } else {
2893
+ source = undefined;
2894
+ }
2895
+ if (schema && schema.type === "object" && schema.items[part]) {
2896
+ schema = schema.items[part];
2897
+ } else if (schema && schema.type === "record") {
2898
+ schema = schema.item;
2899
+ } else {
2900
+ schema = undefined;
2901
+ }
2902
+ }
2903
+ } catch (err) {
2904
+ _iterator.e(err);
2905
+ } finally {
2906
+ _iterator.f();
2907
+ }
2908
+ return {
2909
+ source: source,
2910
+ schema: schema
2911
+ };
2912
+ }
2913
+ function checkKeyIsValid(key, sourcePath, snapshot) {
2914
+ var _getKeyOfRecordAt = getKeyOfRecordAt(sourcePath, snapshot),
2915
+ keyOfModuleSource = _getKeyOfRecordAt.source,
2916
+ keyOfModuleSchema = _getKeyOfRecordAt.schema;
2917
+ if (keyOfModuleSchema && keyOfModuleSchema.type !== "record") {
2918
+ return {
2919
+ error: true,
2920
+ message: "Expected key at ".concat(sourcePath, " to be of type 'record'")
2921
+ };
2922
+ }
2923
+ if (keyOfModuleSource && _typeof(keyOfModuleSource) === "object" && !Array.isArray(keyOfModuleSource) && key in keyOfModuleSource) {
2924
+ return {
2925
+ error: false
2926
+ };
2927
+ }
2928
+ if (!keyOfModuleSource || _typeof(keyOfModuleSource) !== "object" || Array.isArray(keyOfModuleSource)) {
2929
+ return {
2930
+ error: true,
2931
+ message: "Expected ".concat(sourcePath, " to be a truthy object")
2932
+ };
2933
+ }
2934
+ var alternatives = findSimilar(key, Object.keys(keyOfModuleSource));
2935
+ if (alternatives.length === 0) {
2936
+ return {
2937
+ error: true,
2938
+ message: "Key '".concat(key, "' does not exist in ").concat(sourcePath, ".")
2939
+ };
2940
+ }
2941
+ return {
2942
+ error: true,
2943
+ message: "Key '".concat(key, "' does not exist in ").concat(sourcePath, ". Closest match: '").concat(alternatives[0].target, "'. Other similar: ").concat(alternatives.slice(1, 4).map(function (a) {
2944
+ return "'".concat(a.target, "'");
2945
+ }).join(", ")).concat(alternatives.length > 4 ? ", ..." : "")
2946
+ };
2947
+ }
2948
+ function checkRouteIsValid(route, includePattern, excludePattern, snapshot) {
2949
+ var routerModules = {};
2950
+ for (var moduleFilePathS in snapshot.schemas) {
2951
+ var moduleFilePath = moduleFilePathS;
2952
+ var schema = snapshot.schemas[moduleFilePath];
2953
+ if ((schema === null || schema === void 0 ? void 0 : schema.type) === "record" && schema.router) {
2954
+ var source = snapshot.sources[moduleFilePath];
2955
+ if (source && _typeof(source) === "object" && !Array.isArray(source)) {
2956
+ routerModules[moduleFilePath] = Object.keys(source);
2957
+ }
2958
+ }
2959
+ }
2960
+ var moduleEntries = Object.entries(routerModules);
2961
+ var foundInModule = null;
2962
+ for (var _i = 0, _moduleEntries = moduleEntries; _i < _moduleEntries.length; _i++) {
2963
+ var _moduleEntries$_i = _slicedToArray(_moduleEntries[_i], 2),
2964
+ _moduleFilePath = _moduleEntries$_i[0],
2965
+ routes = _moduleEntries$_i[1];
2966
+ if (routes.includes(route)) {
2967
+ foundInModule = _moduleFilePath;
2968
+ break;
2969
+ }
2970
+ }
2971
+ if (!foundInModule) {
2972
+ var allRoutes = moduleEntries.flatMap(function (_ref4) {
2973
+ var _ref5 = _slicedToArray(_ref4, 2),
2974
+ routes = _ref5[1];
2975
+ return routes;
2976
+ });
2977
+ if (allRoutes.length === 0) {
2978
+ return {
2979
+ error: true,
2980
+ message: "Route '".concat(route, "' could not be validated: No router modules found in the project. Use s.record(...).router(...) to define router modules.")
2981
+ };
2982
+ }
2983
+ allRoutes = filterRoutesByPatterns(allRoutes, includePattern, excludePattern);
2984
+ var alternatives = findSimilar(route, allRoutes);
2985
+ return {
2986
+ error: true,
2987
+ message: "Route '".concat(route, "' does not exist in any router module. ").concat(alternatives.length > 0 ? "Closest match: '".concat(alternatives[0].target, "'. Other similar: ").concat(alternatives.slice(1, 4).map(function (a) {
2988
+ return "'".concat(a.target, "'");
2989
+ }).join(", ")).concat(alternatives.length > 4 ? ", ..." : "") : "No similar routes found.")
2990
+ };
2991
+ }
2992
+ var patternValidation = validateRoutePatterns(route, includePattern, excludePattern);
2993
+ if (!patternValidation.valid) {
2994
+ return {
2995
+ error: true,
2996
+ message: patternValidation.message
2997
+ };
2998
+ }
2999
+ return {
3000
+ error: false
3001
+ };
3002
+ }
3003
+ /**
3004
+ * Resolves a single `keyof:check-keys` or `router:check-route` error against
3005
+ * the given schema/source snapshot. Returns:
3006
+ * - null if the error is not one of these fixes (caller should pass through)
3007
+ * - { status: "resolved" } if the referenced key/route is valid (drop error)
3008
+ * - { status: "remaining"; error } if invalid (rewritten error to surface)
3009
+ */
3010
+ function resolveSchemaSourceFixForError(error, snapshot) {
3011
+ var _error$fixes;
3012
+ var fixes = (_error$fixes = error.fixes) !== null && _error$fixes !== void 0 ? _error$fixes : [];
3013
+ if (fixes.includes("keyof:check-keys")) {
3014
+ if (!error.value) {
3015
+ return {
3016
+ status: "remaining",
3017
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3018
+ message: "Could not find a value for keyOf. ".concat(TYPE_ERROR_MESSAGE),
3019
+ typeError: true,
3020
+ fixes: undefined
3021
+ })
3022
+ };
3023
+ }
3024
+ if (_typeof(error.value) !== "object") {
3025
+ return {
3026
+ status: "remaining",
3027
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3028
+ message: "Expected keyOf validation error to have a 'value' property of type 'object'. Found: ".concat(_typeof(error.value), ". ").concat(TYPE_ERROR_MESSAGE),
3029
+ typeError: true,
3030
+ fixes: undefined
3031
+ })
3032
+ };
3033
+ }
3034
+ var valueObj = error.value;
3035
+ var key = valueObj.key;
3036
+ var valueSourcePath = valueObj.sourcePath;
3037
+ if (typeof key !== "string") {
3038
+ return {
3039
+ status: "remaining",
3040
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3041
+ message: "Expected keyOf validation error 'value' to have property 'key' of type 'string'. Found: ".concat(_typeof(key), ". ").concat(TYPE_ERROR_MESSAGE),
3042
+ typeError: true,
3043
+ fixes: undefined
3044
+ })
3045
+ };
3046
+ }
3047
+ if (typeof valueSourcePath !== "string") {
3048
+ return {
3049
+ status: "remaining",
3050
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3051
+ message: "Expected keyOf validation error 'value' to have property 'sourcePath' of type 'string'. Found: ".concat(_typeof(valueSourcePath), ". ").concat(TYPE_ERROR_MESSAGE),
3052
+ typeError: true,
3053
+ fixes: undefined
3054
+ })
3055
+ };
3056
+ }
3057
+ var res = checkKeyIsValid(key, valueSourcePath, snapshot);
3058
+ if (res.error) {
3059
+ return {
3060
+ status: "remaining",
3061
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3062
+ message: res.message,
3063
+ fixes: undefined
3064
+ })
3065
+ };
3066
+ }
3067
+ return {
3068
+ status: "resolved"
3069
+ };
3070
+ }
3071
+ if (fixes.includes("router:check-route")) {
3072
+ if (!error.value) {
3073
+ return {
3074
+ status: "remaining",
3075
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3076
+ message: "Could not find a value for route. ".concat(TYPE_ERROR_MESSAGE),
3077
+ typeError: true,
3078
+ fixes: undefined
3079
+ })
3080
+ };
3081
+ }
3082
+ if (_typeof(error.value) !== "object") {
3083
+ return {
3084
+ status: "remaining",
3085
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3086
+ message: "Expected route validation error to have a 'value' property of type 'object'. Found: ".concat(_typeof(error.value), ". ").concat(TYPE_ERROR_MESSAGE),
3087
+ typeError: true,
3088
+ fixes: undefined
3089
+ })
3090
+ };
3091
+ }
3092
+ var _valueObj = error.value;
3093
+ var route = _valueObj.route;
3094
+ if (typeof route !== "string") {
3095
+ return {
3096
+ status: "remaining",
3097
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3098
+ message: "Expected route validation error 'value' to have property 'route' of type 'string'. Found: ".concat(_typeof(route), ". ").concat(TYPE_ERROR_MESSAGE),
3099
+ typeError: true,
3100
+ fixes: undefined
3101
+ })
3102
+ };
3103
+ }
3104
+ var include = asRegExpPattern(_valueObj.include);
3105
+ var exclude = asRegExpPattern(_valueObj.exclude);
3106
+ var _res = checkRouteIsValid(route, include, exclude, snapshot);
3107
+ if (_res.error) {
3108
+ return {
3109
+ status: "remaining",
3110
+ error: _objectSpread2(_objectSpread2({}, error), {}, {
3111
+ message: _res.message,
3112
+ fixes: undefined
3113
+ })
3114
+ };
3115
+ }
3116
+ return {
3117
+ status: "resolved"
3118
+ };
3119
+ }
3120
+ return null;
3121
+ }
3122
+ function asRegExpPattern(value) {
3123
+ if (value && _typeof(value) === "object" && "source" in value && "flags" in value && typeof value.source === "string" && typeof value.flags === "string") {
3124
+ return value;
3125
+ }
3126
+ return undefined;
3127
+ }
3128
+
3129
+ /**
3130
+ * Apply schema/source fix resolution across a map of validation errors.
3131
+ *
3132
+ * Errors with `keyof:check-keys` or `router:check-route` are resolved against
3133
+ * the in-memory schema/source snapshot — valid references drop the error,
3134
+ * invalid ones are rewritten with a "did you mean…" message and have their
3135
+ * `fixes` cleared (so downstream code reports them as plain validation errors).
3136
+ * All other errors pass through untouched.
3137
+ *
3138
+ * Source paths with no remaining errors are removed from the result.
3139
+ */
3140
+ function resolveSchemaSourceFixes(validationErrors, snapshot) {
3141
+ var result = {};
3142
+ for (var sourcePathS in validationErrors) {
3143
+ var sourcePath = sourcePathS;
3144
+ var remaining = [];
3145
+ var _iterator2 = _createForOfIteratorHelper(validationErrors[sourcePath]),
3146
+ _step2;
3147
+ try {
3148
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
3149
+ var error = _step2.value;
3150
+ var resolved = resolveSchemaSourceFixForError(error, snapshot);
3151
+ if (resolved === null) {
3152
+ remaining.push(error);
3153
+ } else if (resolved.status === "remaining") {
3154
+ remaining.push(resolved.error);
3155
+ }
3156
+ }
3157
+ } catch (err) {
3158
+ _iterator2.e(err);
3159
+ } finally {
3160
+ _iterator2.f();
3161
+ }
3162
+ if (remaining.length > 0) {
3163
+ result[sourcePath] = remaining;
3164
+ }
3165
+ }
3166
+ return result;
3167
+ }
3168
+
3169
+ export { Api, DEFAULT_ERROR_MESSAGE, DateOptions, FileOptions, ImageOptions, ParentRef$1 as ParentRef, Patch, PatchBlock, PatchId, PatchJSON, RemirrorBr, RemirrorBulletList, RemirrorHeading, RemirrorImage, RemirrorJSON, RemirrorLinkMark, RemirrorListItem, RemirrorOrderedList, RemirrorParagraph, RemirrorText, RemirrorTextMark, RichTextOptions, SerializedArraySchema, SerializedBooleanSchema, SerializedDateSchema, SerializedDateTimeSchema, SerializedFileSchema, SerializedImageSchema, SerializedKeyOfSchema, SerializedLiteralSchema, SerializedNumberSchema, SerializedObjectSchema, SerializedRecordSchema, SerializedRichTextSchema, SerializedRouteSchema, SerializedSchema, SerializedStringSchema, SerializedUnionSchema, SharedValConfig, VAL_CONFIG_SESSION_STORAGE_KEY, VAL_ENABLE_COOKIE_NAME, VAL_SESSION_COOKIE, VAL_STATE_COOKIE, VAL_THEME_SESSION_STORAGE_KEY, ValCommit, ValDeployment, createRegExpFromPattern, createValClient, filterRoutesByPatterns, findSimilar, getErrorMessageFromUnknownJson, getNextAppRouterSitemapTree, getNextAppRouterSourceFolder, getPatternFromModuleFilePath, getRoutesOf, getRoutesWithModulePaths, levenshtein, parseRoutePattern, remirrorToRichTextSource, resolveSchemaSourceFixForError, resolveSchemaSourceFixes, richTextToRemirror, urlOf, validateRoutePatterns };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@valbuild/shared",
3
- "version": "0.97.0",
3
+ "version": "0.97.1",
4
4
  "private": false,
5
5
  "description": "Val shared types and utilities",
6
6
  "repository": {
@@ -31,7 +31,7 @@
31
31
  "dependencies": {
32
32
  "zod": "^4.3.5",
33
33
  "zod-validation-error": "^5.0.0",
34
- "@valbuild/core": "0.97.0"
34
+ "@valbuild/core": "0.97.1"
35
35
  },
36
36
  "files": [
37
37
  "dist",