h3 typebox
v0.6.0
typebox 및 ajv를 사용하여 h3에 대한 JSON 스키마 유효성 검사.
# Using npm
npm install h3-typebox
# Using yarn
yarn install h3-typebox
# Using pnpm
pnpm install h3-typebox
import { createServer } from 'http'
import { createApp } from 'h3'
import { validateBody , validateQuery , Type } from 'h3-typebox'
const app = createApp ( )
app . use ( '/' , async ( event ) => {
// Validate body
const body = await validateBody ( event , Type . Object ( {
optional : Type . Optional ( Type . String ( ) ) ,
required : Type . Boolean ( ) ,
} ) )
// Validate query
const query = validateQuery ( event , Type . Object ( {
required : Type . String ( ) ,
} ) )
} )
createServer ( app ) . listen ( process . env . PORT || 3000 )
TypeBox 문서에서 Type
사용하여 스키마를 정의하는 방법을 확인하세요.
validateBody
또는 validateQuery
에 옵션 개체를 정의할 수 있습니다. 현재 다음 옵션이 지원됩니다.
includeAjvFormats: Boolean
date
, date-time
또는 email
과 같은 일부 형식은 현재 JSONSchema 초안에 지정되어 있지만 기본적으로 ajv에는 포함되지 않지만 ajv-formats
패키지에서 제공됩니다. 이러한 형식 중 하나가 필요한 경우 다음과 같이 validateBody
또는 validateQuery
의 옵션에 includeAjvFormats: true
지정할 수 있습니다.
// Body
validateBody ( event , schema , { includeAjvFormats : true } )
// Query
validateQuery ( event , schema , { includeAjvFormats : true } )
현재 성능 및 보안상의 이유로 다음 확장 형식만 지원됩니다.
Type.Unsafe
메서드 또는 인라인 스키마를 사용하여 사용자 지정 스키마에서 사용할 수 있습니다.
const bodySchema = Type . Object ( {
optional : Type . Optional ( Type . String ( ) ) ,
dateTime : Type . String ( { format : 'date-time' } )
} )
validateBody ( event , bodySchema , { includeAjvFormats : true } )
pnpm install
사용하여 종속성 설치pnpm dev
사용하여 대화형 테스트 실행 로 만든?
MIT 라이센스에 따라 게시되었습니다.