@naturalcycles/nodejs-lib 15.81.1 → 15.82.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.
@@ -49,6 +49,11 @@ export declare class AjvSchema<IN = unknown, OUT = IN> {
49
49
  private getAJVValidateFunction;
50
50
  private static requireValidJsonSchema;
51
51
  private applyImprovementsOnErrorMessages;
52
+ private getErrorMessageForInstancePath;
53
+ private traverseSchemaPath;
54
+ private getChildSchema;
55
+ private getArrayItemSchema;
56
+ private getObjectPropertySchema;
52
57
  }
53
58
  export declare const HIDDEN_AJV_SCHEMA: unique symbol;
54
59
  export type WithCachedAjvSchema<Base, IN, OUT> = Base & {
@@ -162,12 +162,56 @@ export class AjvSchema {
162
162
  return;
163
163
  const { errorMessages } = this.schema;
164
164
  for (const error of errors) {
165
- if (errorMessages?.[error.keyword]) {
165
+ const errorMessage = this.getErrorMessageForInstancePath(this.schema, error.instancePath, error.keyword);
166
+ if (errorMessage) {
167
+ error.message = errorMessage;
168
+ }
169
+ else if (errorMessages?.[error.keyword]) {
166
170
  error.message = errorMessages[error.keyword];
167
171
  }
168
172
  error.instancePath = error.instancePath.replaceAll(/\/(\d+)/g, `[$1]`).replaceAll('/', '.');
169
173
  }
170
174
  }
175
+ getErrorMessageForInstancePath(schema, instancePath, keyword) {
176
+ if (!schema || !instancePath)
177
+ return undefined;
178
+ const segments = instancePath.split('/').filter(Boolean);
179
+ return this.traverseSchemaPath(schema, segments, keyword);
180
+ }
181
+ traverseSchemaPath(schema, segments, keyword) {
182
+ if (!segments.length)
183
+ return undefined;
184
+ const [currentSegment, ...remainingSegments] = segments;
185
+ const nextSchema = this.getChildSchema(schema, currentSegment);
186
+ if (!nextSchema)
187
+ return undefined;
188
+ if (nextSchema.errorMessages?.[keyword]) {
189
+ return nextSchema.errorMessages[keyword];
190
+ }
191
+ if (remainingSegments.length) {
192
+ return this.traverseSchemaPath(nextSchema, remainingSegments, keyword);
193
+ }
194
+ return undefined;
195
+ }
196
+ getChildSchema(schema, segment) {
197
+ if (!segment)
198
+ return undefined;
199
+ if (/^\d+$/.test(segment) && schema.items) {
200
+ return this.getArrayItemSchema(schema, segment);
201
+ }
202
+ return this.getObjectPropertySchema(schema, segment);
203
+ }
204
+ getArrayItemSchema(schema, indexSegment) {
205
+ if (!schema.items)
206
+ return undefined;
207
+ if (Array.isArray(schema.items)) {
208
+ return schema.items[Number(indexSegment)];
209
+ }
210
+ return schema.items;
211
+ }
212
+ getObjectPropertySchema(schema, segment) {
213
+ return schema.properties?.[segment];
214
+ }
171
215
  }
172
216
  const separator = '\n';
173
217
  export const HIDDEN_AJV_SCHEMA = Symbol('HIDDEN_AJV_SCHEMA');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.81.1",
4
+ "version": "15.82.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -229,13 +229,78 @@ export class AjvSchema<IN = unknown, OUT = IN> {
229
229
  const { errorMessages } = this.schema
230
230
 
231
231
  for (const error of errors) {
232
- if (errorMessages?.[error.keyword]) {
232
+ const errorMessage = this.getErrorMessageForInstancePath(
233
+ this.schema,
234
+ error.instancePath,
235
+ error.keyword,
236
+ )
237
+
238
+ if (errorMessage) {
239
+ error.message = errorMessage
240
+ } else if (errorMessages?.[error.keyword]) {
233
241
  error.message = errorMessages[error.keyword]
234
242
  }
235
243
 
236
244
  error.instancePath = error.instancePath.replaceAll(/\/(\d+)/g, `[$1]`).replaceAll('/', '.')
237
245
  }
238
246
  }
247
+
248
+ private getErrorMessageForInstancePath(
249
+ schema: JsonSchema<IN, OUT> | undefined,
250
+ instancePath: string,
251
+ keyword: string,
252
+ ): string | undefined {
253
+ if (!schema || !instancePath) return undefined
254
+
255
+ const segments = instancePath.split('/').filter(Boolean)
256
+ return this.traverseSchemaPath(schema, segments, keyword)
257
+ }
258
+
259
+ private traverseSchemaPath<IN = unknown, OUT = IN>(
260
+ schema: JsonSchema<IN, OUT>,
261
+ segments: string[],
262
+ keyword: string,
263
+ ): string | undefined {
264
+ if (!segments.length) return undefined
265
+
266
+ const [currentSegment, ...remainingSegments] = segments
267
+
268
+ const nextSchema = this.getChildSchema(schema, currentSegment)
269
+ if (!nextSchema) return undefined
270
+
271
+ if (nextSchema.errorMessages?.[keyword]) {
272
+ return nextSchema.errorMessages[keyword]
273
+ }
274
+
275
+ if (remainingSegments.length) {
276
+ return this.traverseSchemaPath(nextSchema, remainingSegments, keyword)
277
+ }
278
+
279
+ return undefined
280
+ }
281
+
282
+ private getChildSchema(schema: JsonSchema, segment: string | undefined): JsonSchema | undefined {
283
+ if (!segment) return undefined
284
+ if (/^\d+$/.test(segment) && schema.items) {
285
+ return this.getArrayItemSchema(schema, segment)
286
+ }
287
+
288
+ return this.getObjectPropertySchema(schema, segment)
289
+ }
290
+
291
+ private getArrayItemSchema(schema: JsonSchema, indexSegment: string): JsonSchema | undefined {
292
+ if (!schema.items) return undefined
293
+
294
+ if (Array.isArray(schema.items)) {
295
+ return schema.items[Number(indexSegment)]
296
+ }
297
+
298
+ return schema.items
299
+ }
300
+
301
+ private getObjectPropertySchema(schema: JsonSchema, segment: string): JsonSchema | undefined {
302
+ return schema.properties?.[segment as keyof typeof schema.properties]
303
+ }
239
304
  }
240
305
 
241
306
  const separator = '\n'