elysia 1.3.13 → 1.3.15

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,28 +298,8 @@ 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 = "";
311
- if (customError !== void 0)
312
- message = typeof customError == "object" ? JSON.stringify(customError) : customError + "";
313
- else if (isProduction)
314
- message = JSON.stringify({
315
- type: "validation",
316
- on: type,
317
- summary: mapValueError(error).summary,
318
- message: error?.message,
319
- found: value
320
- });
321
- else {
322
- let schema = validator?.schema ?? validator, errors2 = "Errors" in validator ? [...validator.Errors(value)].map(mapValueError) : [...Value.Errors(validator, value)].map(mapValueError), expected;
301
+ let error = errors?.First() ?? ("Errors" in validator ? validator.Errors(value).First() : Value.Errors(validator, value).First()), accessor = error?.path || "root", schema = validator?.schema ?? validator, expected;
302
+ if (!isProduction)
323
303
  try {
324
304
  expected = Value.Create(schema);
325
305
  } catch (error2) {
@@ -330,28 +310,60 @@ var ValidationError = class _ValidationError extends Error {
330
310
  error: error2
331
311
  };
332
312
  }
333
- message = JSON.stringify(
334
- {
335
- type: "validation",
336
- on: type,
337
- summary: mapValueError(error).summary,
338
- property: accessor,
339
- message: error?.message,
340
- expected,
341
- found: value,
342
- errors: errors2
343
- },
344
- null,
345
- 2
346
- );
347
- }
313
+ let customError = error?.schema?.message || error?.schema?.error !== void 0 ? typeof error.schema.error == "function" ? error.schema.error(
314
+ isProduction ? {
315
+ type: "validation",
316
+ on: type,
317
+ found: value
318
+ } : {
319
+ type: "validation",
320
+ on: type,
321
+ value,
322
+ property: accessor,
323
+ message: error?.message,
324
+ summary: mapValueError(error).summary,
325
+ found: value,
326
+ expected,
327
+ errors: "Errors" in validator ? [
328
+ ...validator.Errors(
329
+ value
330
+ )
331
+ ].map(mapValueError) : [
332
+ ...Value.Errors(
333
+ validator,
334
+ value
335
+ )
336
+ ].map(mapValueError)
337
+ },
338
+ validator
339
+ ) : error.schema.error : void 0, message = "";
340
+ customError !== void 0 ? message = typeof customError == "object" ? JSON.stringify(customError) : customError + "" : isProduction ? message = JSON.stringify({
341
+ type: "validation",
342
+ on: type,
343
+ found: value
344
+ }) : message = JSON.stringify(
345
+ {
346
+ type: "validation",
347
+ on: type,
348
+ property: accessor,
349
+ message: error?.message,
350
+ summary: mapValueError(error).summary,
351
+ expected,
352
+ found: value,
353
+ errors: "Errors" in validator ? [...validator.Errors(value)].map(mapValueError) : [...Value.Errors(validator, value)].map(
354
+ mapValueError
355
+ )
356
+ },
357
+ null,
358
+ 2
359
+ );
348
360
  super(message);
349
361
  this.type = type;
350
362
  this.validator = validator;
351
363
  this.value = value;
352
364
  this.code = "VALIDATION";
353
365
  this.status = 422;
354
- Object.setPrototypeOf(this, _ValidationError.prototype);
366
+ this.valueError = error, this.expected = expected, this.customError = customError, Object.setPrototypeOf(this, _ValidationError.prototype);
355
367
  }
356
368
  get all() {
357
369
  return "Errors" in this.validator ? [...this.validator.Errors(this.value)].map(mapValueError) : (
@@ -379,6 +391,45 @@ var ValidationError = class _ValidationError extends Error {
379
391
  }
380
392
  });
381
393
  }
394
+ /**
395
+ * Utility function to inherit add custom error and keep the original Validation error
396
+ *
397
+ * @since 1.3.14
398
+ *
399
+ * @example
400
+ * ```ts
401
+ * new Elysia()
402
+ * .onError(({ error, code }) => {
403
+ * if (code === 'VALIDATION') return error.detail(error.message)
404
+ * })
405
+ * .post('/', () => 'Hello World!', {
406
+ * body: t.Object({
407
+ * x: t.Number({
408
+ * error: 'x must be a number'
409
+ * })
410
+ * })
411
+ * })
412
+ * ```
413
+ */
414
+ detail(message) {
415
+ if (!this.customError) return this.message;
416
+ let validator = this.validator, value = this.value, expected = this.expected, errors = this.all;
417
+ return isProduction ? {
418
+ type: "validation",
419
+ on: this.type,
420
+ found: value,
421
+ message
422
+ } : {
423
+ type: "validation",
424
+ on: this.type,
425
+ property: this.valueError?.path || "root",
426
+ message,
427
+ summary: mapValueError(this.valueError).summary,
428
+ found: value,
429
+ expected,
430
+ errors
431
+ };
432
+ }
382
433
  };
383
434
 
384
435
  // src/cookies.ts
@@ -312,28 +312,8 @@ 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 = "";
325
- if (customError !== void 0)
326
- message = typeof customError == "object" ? JSON.stringify(customError) : customError + "";
327
- else if (isProduction)
328
- message = JSON.stringify({
329
- type: "validation",
330
- on: type,
331
- summary: mapValueError(error).summary,
332
- message: error?.message,
333
- found: value
334
- });
335
- else {
336
- let schema = validator?.schema ?? validator, errors2 = "Errors" in validator ? [...validator.Errors(value)].map(mapValueError) : [...Value.Errors(validator, value)].map(mapValueError), expected;
315
+ let error = errors?.First() ?? ("Errors" in validator ? validator.Errors(value).First() : Value.Errors(validator, value).First()), accessor = error?.path || "root", schema = validator?.schema ?? validator, expected;
316
+ if (!isProduction)
337
317
  try {
338
318
  expected = Value.Create(schema);
339
319
  } catch (error2) {
@@ -344,28 +324,60 @@ var ValidationError = class _ValidationError extends Error {
344
324
  error: error2
345
325
  };
346
326
  }
347
- message = JSON.stringify(
348
- {
349
- type: "validation",
350
- on: type,
351
- summary: mapValueError(error).summary,
352
- property: accessor,
353
- message: error?.message,
354
- expected,
355
- found: value,
356
- errors: errors2
357
- },
358
- null,
359
- 2
360
- );
361
- }
327
+ let customError = error?.schema?.message || error?.schema?.error !== void 0 ? typeof error.schema.error == "function" ? error.schema.error(
328
+ isProduction ? {
329
+ type: "validation",
330
+ on: type,
331
+ found: value
332
+ } : {
333
+ type: "validation",
334
+ on: type,
335
+ value,
336
+ property: accessor,
337
+ message: error?.message,
338
+ summary: mapValueError(error).summary,
339
+ found: value,
340
+ expected,
341
+ errors: "Errors" in validator ? [
342
+ ...validator.Errors(
343
+ value
344
+ )
345
+ ].map(mapValueError) : [
346
+ ...Value.Errors(
347
+ validator,
348
+ value
349
+ )
350
+ ].map(mapValueError)
351
+ },
352
+ validator
353
+ ) : error.schema.error : void 0, message = "";
354
+ customError !== void 0 ? message = typeof customError == "object" ? JSON.stringify(customError) : customError + "" : isProduction ? message = JSON.stringify({
355
+ type: "validation",
356
+ on: type,
357
+ found: value
358
+ }) : message = JSON.stringify(
359
+ {
360
+ type: "validation",
361
+ on: type,
362
+ property: accessor,
363
+ message: error?.message,
364
+ summary: mapValueError(error).summary,
365
+ expected,
366
+ found: value,
367
+ errors: "Errors" in validator ? [...validator.Errors(value)].map(mapValueError) : [...Value.Errors(validator, value)].map(
368
+ mapValueError
369
+ )
370
+ },
371
+ null,
372
+ 2
373
+ );
362
374
  super(message);
363
375
  this.type = type;
364
376
  this.validator = validator;
365
377
  this.value = value;
366
378
  this.code = "VALIDATION";
367
379
  this.status = 422;
368
- Object.setPrototypeOf(this, _ValidationError.prototype);
380
+ this.valueError = error, this.expected = expected, this.customError = customError, Object.setPrototypeOf(this, _ValidationError.prototype);
369
381
  }
370
382
  get all() {
371
383
  return "Errors" in this.validator ? [...this.validator.Errors(this.value)].map(mapValueError) : (
@@ -393,6 +405,45 @@ var ValidationError = class _ValidationError extends Error {
393
405
  }
394
406
  });
395
407
  }
408
+ /**
409
+ * Utility function to inherit add custom error and keep the original Validation error
410
+ *
411
+ * @since 1.3.14
412
+ *
413
+ * @example
414
+ * ```ts
415
+ * new Elysia()
416
+ * .onError(({ error, code }) => {
417
+ * if (code === 'VALIDATION') return error.detail(error.message)
418
+ * })
419
+ * .post('/', () => 'Hello World!', {
420
+ * body: t.Object({
421
+ * x: t.Number({
422
+ * error: 'x must be a number'
423
+ * })
424
+ * })
425
+ * })
426
+ * ```
427
+ */
428
+ detail(message) {
429
+ if (!this.customError) return this.message;
430
+ let validator = this.validator, value = this.value, expected = this.expected, errors = this.all;
431
+ return isProduction ? {
432
+ type: "validation",
433
+ on: this.type,
434
+ found: value,
435
+ message
436
+ } : {
437
+ type: "validation",
438
+ on: this.type,
439
+ property: this.valueError?.path || "root",
440
+ message,
441
+ summary: mapValueError(this.valueError).summary,
442
+ found: value,
443
+ expected,
444
+ errors
445
+ };
446
+ }
396
447
  };
397
448
 
398
449
  // src/cookies.ts
@@ -312,28 +312,8 @@ 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 = "";
325
- if (customError !== void 0)
326
- message = typeof customError == "object" ? JSON.stringify(customError) : customError + "";
327
- else if (isProduction)
328
- message = JSON.stringify({
329
- type: "validation",
330
- on: type,
331
- summary: mapValueError(error).summary,
332
- message: error?.message,
333
- found: value
334
- });
335
- else {
336
- let schema = validator?.schema ?? validator, errors2 = "Errors" in validator ? [...validator.Errors(value)].map(mapValueError) : [...Value.Errors(validator, value)].map(mapValueError), expected;
315
+ let error = errors?.First() ?? ("Errors" in validator ? validator.Errors(value).First() : Value.Errors(validator, value).First()), accessor = error?.path || "root", schema = validator?.schema ?? validator, expected;
316
+ if (!isProduction)
337
317
  try {
338
318
  expected = Value.Create(schema);
339
319
  } catch (error2) {
@@ -344,28 +324,60 @@ var ValidationError = class _ValidationError extends Error {
344
324
  error: error2
345
325
  };
346
326
  }
347
- message = JSON.stringify(
348
- {
349
- type: "validation",
350
- on: type,
351
- summary: mapValueError(error).summary,
352
- property: accessor,
353
- message: error?.message,
354
- expected,
355
- found: value,
356
- errors: errors2
357
- },
358
- null,
359
- 2
360
- );
361
- }
327
+ let customError = error?.schema?.message || error?.schema?.error !== void 0 ? typeof error.schema.error == "function" ? error.schema.error(
328
+ isProduction ? {
329
+ type: "validation",
330
+ on: type,
331
+ found: value
332
+ } : {
333
+ type: "validation",
334
+ on: type,
335
+ value,
336
+ property: accessor,
337
+ message: error?.message,
338
+ summary: mapValueError(error).summary,
339
+ found: value,
340
+ expected,
341
+ errors: "Errors" in validator ? [
342
+ ...validator.Errors(
343
+ value
344
+ )
345
+ ].map(mapValueError) : [
346
+ ...Value.Errors(
347
+ validator,
348
+ value
349
+ )
350
+ ].map(mapValueError)
351
+ },
352
+ validator
353
+ ) : error.schema.error : void 0, message = "";
354
+ customError !== void 0 ? message = typeof customError == "object" ? JSON.stringify(customError) : customError + "" : isProduction ? message = JSON.stringify({
355
+ type: "validation",
356
+ on: type,
357
+ found: value
358
+ }) : message = JSON.stringify(
359
+ {
360
+ type: "validation",
361
+ on: type,
362
+ property: accessor,
363
+ message: error?.message,
364
+ summary: mapValueError(error).summary,
365
+ expected,
366
+ found: value,
367
+ errors: "Errors" in validator ? [...validator.Errors(value)].map(mapValueError) : [...Value.Errors(validator, value)].map(
368
+ mapValueError
369
+ )
370
+ },
371
+ null,
372
+ 2
373
+ );
362
374
  super(message);
363
375
  this.type = type;
364
376
  this.validator = validator;
365
377
  this.value = value;
366
378
  this.code = "VALIDATION";
367
379
  this.status = 422;
368
- Object.setPrototypeOf(this, _ValidationError.prototype);
380
+ this.valueError = error, this.expected = expected, this.customError = customError, Object.setPrototypeOf(this, _ValidationError.prototype);
369
381
  }
370
382
  get all() {
371
383
  return "Errors" in this.validator ? [...this.validator.Errors(this.value)].map(mapValueError) : (
@@ -393,6 +405,45 @@ var ValidationError = class _ValidationError extends Error {
393
405
  }
394
406
  });
395
407
  }
408
+ /**
409
+ * Utility function to inherit add custom error and keep the original Validation error
410
+ *
411
+ * @since 1.3.14
412
+ *
413
+ * @example
414
+ * ```ts
415
+ * new Elysia()
416
+ * .onError(({ error, code }) => {
417
+ * if (code === 'VALIDATION') return error.detail(error.message)
418
+ * })
419
+ * .post('/', () => 'Hello World!', {
420
+ * body: t.Object({
421
+ * x: t.Number({
422
+ * error: 'x must be a number'
423
+ * })
424
+ * })
425
+ * })
426
+ * ```
427
+ */
428
+ detail(message) {
429
+ if (!this.customError) return this.message;
430
+ let validator = this.validator, value = this.value, expected = this.expected, errors = this.all;
431
+ return isProduction ? {
432
+ type: "validation",
433
+ on: this.type,
434
+ found: value,
435
+ message
436
+ } : {
437
+ type: "validation",
438
+ on: this.type,
439
+ property: this.valueError?.path || "root",
440
+ message,
441
+ summary: mapValueError(this.valueError).summary,
442
+ found: value,
443
+ expected,
444
+ errors
445
+ };
446
+ }
396
447
  };
397
448
 
398
449
  // 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';