Validation Error

검증을 위해 Elysia.t를 사용하는 경우, 검증에 실패한 필드에 대한 커스텀 에러 메시지를 제공할 수 있습니다.

typescript
import { Elysia, t } from 'elysia'

new Elysia()
	.post(
		'/',
		({ body }) => body,
		{
			body: t.Object({
				age: t.Number({
					error: 'Age must be a number'
				})
			}, {
				error: 'Body must be an object'
			})
		}
	)
	.listen(3000)

Elysia는 기본 에러 메시지를 제공한 커스텀 메시지로 재정의합니다. Custom Validation Message를 참조하세요.

Validation Detail

기본적으로 Elysia는 다음과 같이 검증에 문제가 있는 내용을 설명하는 Validation Detail도 제공합니다:

json
{
	"type": "validation",
	"on": "params",
	"value": { "id": "string" },
	"property": "/id",
	"message": "id must be a number", 
	"summary": "Property 'id' should be one of: 'numeric', 'number'",
	"found": { "id": "string" },
	"expected": { "id": 0 },
	"errors": [
		{
			"type": 62,
			"schema": {
				"anyOf": [
					{ "format": "numeric", "default": 0, "type": "string" },
					{ "type": "number" }
				]
			},
			"path": "/id",
			"value": "string",
			"message": "Expected union value",
			"errors": [{ "iterator": {} }, { "iterator": {} }],
			"summary": "Property 'id' should be one of: 'numeric', 'number'"
		}
	]
}

하지만 커스텀 에러 메시지를 제공하면 Validation Detail이 완전히 재정의됩니다.

검증 세부 정보를 다시 가져오려면 커스텀 에러 메시지를 Validation Detail 함수로 감쌀 수 있습니다.

typescript
import { Elysia, t, validationDetail } from 'elysia'

new Elysia()
	.post(
		'/',
		({ body }) => body,
		{
			body: t.Object({
				age: t.Number({
					error: validationDetail('Age must be a number') 
				})
			}, {
				error: validationDetail('Body must be an object') 
			})
		}
	)
	.listen(3000)

Assignment

Elysia의 context를 확장해 봅시다.

  1. Provide a Custom Validation Message

    Let's override a validation message to be more friendly with your own words

Show answer

스키마에 error 속성을 제공하여 커스텀 에러 메시지를 제공할 수 있습니다.

typescript
import { Elysia, t } from 'elysia'

new Elysia()
	.post(
		'/',
		({ body }) => body,
		{
			body: t.Object({
				age: t.Number({
                    error: 'thing'
                })
			})
		}
	)
	.listen(3000)
  • index.ts

Error

Error: No Elysia server is running in index.ts
Did you forget to call `.listen()`?
    at parse (file:///vercel/path0/docs/.vitepress/.temp/utils.J44kKQwQ.js:202:66)
    at file:///vercel/path0/docs/.vitepress/.temp/utils.J44kKQwQ.js:240:30
    at new Promise (<anonymous>)
    at execute (file:///vercel/path0/docs/.vitepress/.temp/utils.J44kKQwQ.js:238:49)
    at Proxy.run (file:///vercel/path0/docs/.vitepress/.temp/store.CFqL75-f.js:190:51)
    at Proxy.wrappedAction (file:///vercel/path0/node_modules/pinia/dist/pinia.mjs:1394:26)
    at setup (file:///vercel/path0/docs/.vitepress/.temp/playground.eEZCBItr.js:133:50)
    at playground_vue_vue_type_script_setup_true_lang_default.setup (file:///vercel/path0/docs/.vitepress/.temp/playground.eEZCBItr.js:438:22)
    at callWithErrorHandling (/vercel/path0/node_modules/@vue/runtime-core/dist/runtime-core.cjs.prod.js:86:19)
    at setupStatefulComponent (/vercel/path0/node_modules/@vue/runtime-core/dist/runtime-core.cjs.prod.js:6365:25)