elysia 1.3.13 → 1.3.14

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.
@@ -298,24 +298,33 @@ var mapValueError = (error) => {
298
298
  var ValidationError = class _ValidationError extends Error {
299
299
  constructor(type, validator, value, errors) {
300
300
  value && typeof value == "object" && value instanceof ElysiaCustomStatusResponse && (value = value.response);
301
- let error = errors?.First() || (isProduction ? void 0 : "Errors" in validator ? validator.Errors(value).First() : Value.Errors(validator, value).First()), customError = error?.schema?.message || error?.schema?.error !== void 0 ? typeof error.schema.error == "function" ? error.schema.error({
302
- type,
303
- validator,
304
- value,
305
- get errors() {
306
- return [...validator.Errors(value)].map(
307
- mapValueError
308
- );
309
- }
310
- }) : error.schema.error : void 0, accessor = error?.path || "root", message = "";
301
+ let error = errors?.First() ?? ("Errors" in validator ? validator.Errors(value).First() : Value.Errors(validator, value).First()), accessor = error?.path || "root", customError = error?.schema?.message || error?.schema?.error !== void 0 ? typeof error.schema.error == "function" ? error.schema.error(
302
+ isProduction ? {
303
+ type: "validation",
304
+ on: type,
305
+ found: value
306
+ } : {
307
+ type: "validation",
308
+ on: type,
309
+ value,
310
+ summary: mapValueError(error).summary,
311
+ property: accessor,
312
+ message: error?.message,
313
+ found: value,
314
+ get errors() {
315
+ return [
316
+ ...validator?.Errors(value)
317
+ ].map(mapValueError);
318
+ }
319
+ },
320
+ validator
321
+ ) : error.schema.error : void 0, message = "";
311
322
  if (customError !== void 0)
312
323
  message = typeof customError == "object" ? JSON.stringify(customError) : customError + "";
313
324
  else if (isProduction)
314
325
  message = JSON.stringify({
315
326
  type: "validation",
316
327
  on: type,
317
- summary: mapValueError(error).summary,
318
- message: error?.message,
319
328
  found: value
320
329
  });
321
330
  else {
@@ -351,7 +360,7 @@ var ValidationError = class _ValidationError extends Error {
351
360
  this.value = value;
352
361
  this.code = "VALIDATION";
353
362
  this.status = 422;
354
- Object.setPrototypeOf(this, _ValidationError.prototype);
363
+ this.valueError = error, Object.setPrototypeOf(this, _ValidationError.prototype);
355
364
  }
356
365
  get all() {
357
366
  return "Errors" in this.validator ? [...this.validator.Errors(this.value)].map(mapValueError) : (
@@ -379,6 +388,45 @@ var ValidationError = class _ValidationError extends Error {
379
388
  }
380
389
  });
381
390
  }
391
+ /**
392
+ * Utility function to inherit add custom error and keep the original Validation error
393
+ *
394
+ * @since 1.3.14
395
+ *
396
+ * @example
397
+ * ```ts
398
+ * new Elysia()
399
+ * .onError(({ error, code }) => {
400
+ * if (code === 'VALIDATION') return error.detail(error.message)
401
+ * })
402
+ * .post('/', () => 'Hello World!', {
403
+ * body: t.Object({
404
+ * x: t.Number({
405
+ * error: 'x must be a number'
406
+ * })
407
+ * })
408
+ * })
409
+ * ```
410
+ */
411
+ detail(message) {
412
+ let validator = this.validator, value = this.value;
413
+ return isProduction ? {
414
+ type: "validation",
415
+ on: this.type,
416
+ found: value,
417
+ message
418
+ } : {
419
+ type: "validation",
420
+ on: this.type,
421
+ message,
422
+ summary: mapValueError(this.valueError).summary,
423
+ property: this.valueError?.path || "root",
424
+ found: value,
425
+ get errors() {
426
+ return [...validator.Errors(value)].map(mapValueError);
427
+ }
428
+ };
429
+ }
382
430
  };
383
431
 
384
432
  // src/cookies.ts
@@ -312,24 +312,33 @@ var mapValueError = (error) => {
312
312
  var ValidationError = class _ValidationError extends Error {
313
313
  constructor(type, validator, value, errors) {
314
314
  value && typeof value == "object" && value instanceof ElysiaCustomStatusResponse && (value = value.response);
315
- let error = errors?.First() || (isProduction ? void 0 : "Errors" in validator ? validator.Errors(value).First() : Value.Errors(validator, value).First()), customError = error?.schema?.message || error?.schema?.error !== void 0 ? typeof error.schema.error == "function" ? error.schema.error({
316
- type,
317
- validator,
318
- value,
319
- get errors() {
320
- return [...validator.Errors(value)].map(
321
- mapValueError
322
- );
323
- }
324
- }) : error.schema.error : void 0, accessor = error?.path || "root", message = "";
315
+ let error = errors?.First() ?? ("Errors" in validator ? validator.Errors(value).First() : Value.Errors(validator, value).First()), accessor = error?.path || "root", customError = error?.schema?.message || error?.schema?.error !== void 0 ? typeof error.schema.error == "function" ? error.schema.error(
316
+ isProduction ? {
317
+ type: "validation",
318
+ on: type,
319
+ found: value
320
+ } : {
321
+ type: "validation",
322
+ on: type,
323
+ value,
324
+ summary: mapValueError(error).summary,
325
+ property: accessor,
326
+ message: error?.message,
327
+ found: value,
328
+ get errors() {
329
+ return [
330
+ ...validator?.Errors(value)
331
+ ].map(mapValueError);
332
+ }
333
+ },
334
+ validator
335
+ ) : error.schema.error : void 0, message = "";
325
336
  if (customError !== void 0)
326
337
  message = typeof customError == "object" ? JSON.stringify(customError) : customError + "";
327
338
  else if (isProduction)
328
339
  message = JSON.stringify({
329
340
  type: "validation",
330
341
  on: type,
331
- summary: mapValueError(error).summary,
332
- message: error?.message,
333
342
  found: value
334
343
  });
335
344
  else {
@@ -365,7 +374,7 @@ var ValidationError = class _ValidationError extends Error {
365
374
  this.value = value;
366
375
  this.code = "VALIDATION";
367
376
  this.status = 422;
368
- Object.setPrototypeOf(this, _ValidationError.prototype);
377
+ this.valueError = error, Object.setPrototypeOf(this, _ValidationError.prototype);
369
378
  }
370
379
  get all() {
371
380
  return "Errors" in this.validator ? [...this.validator.Errors(this.value)].map(mapValueError) : (
@@ -393,6 +402,45 @@ var ValidationError = class _ValidationError extends Error {
393
402
  }
394
403
  });
395
404
  }
405
+ /**
406
+ * Utility function to inherit add custom error and keep the original Validation error
407
+ *
408
+ * @since 1.3.14
409
+ *
410
+ * @example
411
+ * ```ts
412
+ * new Elysia()
413
+ * .onError(({ error, code }) => {
414
+ * if (code === 'VALIDATION') return error.detail(error.message)
415
+ * })
416
+ * .post('/', () => 'Hello World!', {
417
+ * body: t.Object({
418
+ * x: t.Number({
419
+ * error: 'x must be a number'
420
+ * })
421
+ * })
422
+ * })
423
+ * ```
424
+ */
425
+ detail(message) {
426
+ let validator = this.validator, value = this.value;
427
+ return isProduction ? {
428
+ type: "validation",
429
+ on: this.type,
430
+ found: value,
431
+ message
432
+ } : {
433
+ type: "validation",
434
+ on: this.type,
435
+ message,
436
+ summary: mapValueError(this.valueError).summary,
437
+ property: this.valueError?.path || "root",
438
+ found: value,
439
+ get errors() {
440
+ return [...validator.Errors(value)].map(mapValueError);
441
+ }
442
+ };
443
+ }
396
444
  };
397
445
 
398
446
  // src/cookies.ts
@@ -312,24 +312,33 @@ var mapValueError = (error) => {
312
312
  var ValidationError = class _ValidationError extends Error {
313
313
  constructor(type, validator, value, errors) {
314
314
  value && typeof value == "object" && value instanceof ElysiaCustomStatusResponse && (value = value.response);
315
- let error = errors?.First() || (isProduction ? void 0 : "Errors" in validator ? validator.Errors(value).First() : Value.Errors(validator, value).First()), customError = error?.schema?.message || error?.schema?.error !== void 0 ? typeof error.schema.error == "function" ? error.schema.error({
316
- type,
317
- validator,
318
- value,
319
- get errors() {
320
- return [...validator.Errors(value)].map(
321
- mapValueError
322
- );
323
- }
324
- }) : error.schema.error : void 0, accessor = error?.path || "root", message = "";
315
+ let error = errors?.First() ?? ("Errors" in validator ? validator.Errors(value).First() : Value.Errors(validator, value).First()), accessor = error?.path || "root", customError = error?.schema?.message || error?.schema?.error !== void 0 ? typeof error.schema.error == "function" ? error.schema.error(
316
+ isProduction ? {
317
+ type: "validation",
318
+ on: type,
319
+ found: value
320
+ } : {
321
+ type: "validation",
322
+ on: type,
323
+ value,
324
+ summary: mapValueError(error).summary,
325
+ property: accessor,
326
+ message: error?.message,
327
+ found: value,
328
+ get errors() {
329
+ return [
330
+ ...validator?.Errors(value)
331
+ ].map(mapValueError);
332
+ }
333
+ },
334
+ validator
335
+ ) : error.schema.error : void 0, message = "";
325
336
  if (customError !== void 0)
326
337
  message = typeof customError == "object" ? JSON.stringify(customError) : customError + "";
327
338
  else if (isProduction)
328
339
  message = JSON.stringify({
329
340
  type: "validation",
330
341
  on: type,
331
- summary: mapValueError(error).summary,
332
- message: error?.message,
333
342
  found: value
334
343
  });
335
344
  else {
@@ -365,7 +374,7 @@ var ValidationError = class _ValidationError extends Error {
365
374
  this.value = value;
366
375
  this.code = "VALIDATION";
367
376
  this.status = 422;
368
- Object.setPrototypeOf(this, _ValidationError.prototype);
377
+ this.valueError = error, Object.setPrototypeOf(this, _ValidationError.prototype);
369
378
  }
370
379
  get all() {
371
380
  return "Errors" in this.validator ? [...this.validator.Errors(this.value)].map(mapValueError) : (
@@ -393,6 +402,45 @@ var ValidationError = class _ValidationError extends Error {
393
402
  }
394
403
  });
395
404
  }
405
+ /**
406
+ * Utility function to inherit add custom error and keep the original Validation error
407
+ *
408
+ * @since 1.3.14
409
+ *
410
+ * @example
411
+ * ```ts
412
+ * new Elysia()
413
+ * .onError(({ error, code }) => {
414
+ * if (code === 'VALIDATION') return error.detail(error.message)
415
+ * })
416
+ * .post('/', () => 'Hello World!', {
417
+ * body: t.Object({
418
+ * x: t.Number({
419
+ * error: 'x must be a number'
420
+ * })
421
+ * })
422
+ * })
423
+ * ```
424
+ */
425
+ detail(message) {
426
+ let validator = this.validator, value = this.value;
427
+ return isProduction ? {
428
+ type: "validation",
429
+ on: this.type,
430
+ found: value,
431
+ message
432
+ } : {
433
+ type: "validation",
434
+ on: this.type,
435
+ message,
436
+ summary: mapValueError(this.valueError).summary,
437
+ property: this.valueError?.path || "root",
438
+ found: value,
439
+ get errors() {
440
+ return [...validator.Errors(value)].map(mapValueError);
441
+ }
442
+ };
443
+ }
396
444
  };
397
445
 
398
446
  // src/cookies.ts
@@ -1817,6 +1817,8 @@ export default class Elysia<const in out BasePath extends string = '', const in
1817
1817
  }
1818
1818
  export { Elysia };
1819
1819
  export { t } from './type-system';
1820
+ export { validationDetail } from './type-system/utils';
1821
+ export type { ElysiaTypeCustomError, ElysiaTypeCustomErrorCallback } from './type-system/types';
1820
1822
  export { serializeCookie, Cookie, type CookieOptions } from './cookies';
1821
1823
  export type { Context, PreContext, ErrorContext } from './context';
1822
1824
  export { ELYSIA_TRACE, type TraceEvent, type TraceListener, type TraceHandler, type TraceProcess, type TraceStream } from './trace';