Skip to content

Validation

Vona provides a very concise and flexible mechanism for verifying request parameters based on Zod

1. Automatically infer Zod Schema: Basic type/Dto/Entity

If the parameter type is Basic type/Dto/Entity, then the system will automatically infer the corresponding Zod Schema for verification

For example, findOne(@Arg.query('id') id: number), the type of id is number, then the automatically inferred Schema is: z.number()

For another example, findOne(@Arg.query() query: DtoStudentInfo), the type of query is Dto: DtoStudentInfo, then the automatically inferred Schema is: z.object({...})

  • List of automatically inferred types
NameDescription
stringz.string()
numberz.number()
booleanz.boolean()
Dtoz.object({...})
Entityz.object({...})

2. Specify Zod Schema

We can also explicitly specify Zod Schema to provide more complex validation rules. For example, if the URL is /?id=1, we require id to be number and >=6. Then, we can pass parameters to query: z.number().min(6)

typescript
import z from 'zod';

class ControllerStudent3 {
  @Web.get()
  findOne(@Arg.query('id', z.number().min(6)) id: number) {}
}

3. Extending Zod Schema properties

We can also extend new properties based on the existing Zod Schema

For example, we want to specify id as number, and it is optional, with a default value of 3

typescript
import { v } from 'vona-module-a-openapiutils';

class ControllerStudent3 {
  @Web.get()
  findOne(@Arg.query('id', v.default(3), v.optional()) id: number) {}
}

First, the system automatically infers the schema For z.number(), then, append the optional and default attributes in sequence, and finally generate the schema: z.number().optional().default(3)

Therefore, the above code is equivalent to:

typescript
class ControllerStudent3 {
  @Web.get()
  findOne(@Arg.query('id', z.number().optional().default(3)) id: number) {}
}

Also equivalent to:

typescript
class ControllerStudent3 {
  @Web.get()
  findOne(@Arg.query('id', v.default(3), z.number().optional()) id: number) {}
}

4. Decorator: @Arg.filter

The decorator @Arg.filter supports more advanced query parameters, including columns/where/orders/pageNo/pageSize

5. Tool: v.array

For Array type parameters, Vona also provides convenient tools. For example, we require ids to be number[]:

typescript
class ControllerStudent3 {
  @Web.get()
  findOne(@Arg.query('ids', v.array(Number)) ids: number[]) {}
}

Equivalent to:

typescript
class ControllerStudent3 {
  @Web.get()
  findOne(@Arg.query('ids', v.array(z.number())) ids: number[]) {}
}

For another example, we get the students array from the Request body, which is of type DtoStudentInfo[]:

typescript
class ControllerStudent3 {
  @Web.post()
  update(@Arg.body('students', v.array(DtoStudentInfo)) students: DtoStudentInfo[]) {}
}

6. Tool: v.lazy

For classes that may cause circular references, you can use v.lazy to create a lazy schema:

typescript
@Dto()
export class DtoUserLazy {
  @Api.field(v.optional(), v.lazy(() => DtoUserLazy))
  user?: DtoUserLazy;

  @Api.field(v.optional(), v.array(v.lazy(() => DtoRoleLazy)))
  roles?: DtoRoleLazy[];
}

Decorator List

NameDescription
@Arg.paramGets the value from the request params
@Arg.queryGets the value from the request query
@Arg.bodyGets the value from the request body
@Arg.headersGets the value from the request headers
@Arg.fieldsGets the value from the request upload
@Arg.fieldGets the value from the request upload
@Arg.filesGets the value from the request upload
@Arg.fileGets the value from the request upload
@Arg.userGets the current user
@Arg.filterGets the value from the request query

Tool List

VonaJS puts all the utility methods for extending Zod Schema into the group v, thus reducing the mental burden

1. Basic Tools

NameDescription
v.requiredProvide a custom error message for required; otherwise, use Zod's built-in error message
v.optionaloptional
v.defaultdefault
v.objectobject
v.strictObjectSame as z.strictObject(schema.shape)
v.looseObjectSame as z.looseObject(schema.shape)
v.arrayarray
v.lazyCreate a lazy schema

2. String Tools

NameDescription
v.emailemail
v.urlurl
v.uuiduuid
v.ipv4ipv4
v.ipv6ipv6
v.minSupports string/number
v.maxSupports string/number
v.trimtrim
v.toLowerCasetoLowerCase
v.toUpperCasetoUpperCase
v.lowercaselowercase
v.uppercaseuppercase
v.regexregex

3. Openapi Tools

NameDescription
v.openapiopenapi
v.titletitle
v.descriptiondescription
v.exampleexample

4. Serializer Tools

NameDescription
v.serializerExcludeExclude fields
v.serializerTransformTransform field values
v.serializerSensitiveDesensitize fields
v.serializerGetterGenerate field values ​​using a getter

5. Zod Tools

NameDescription
v.refineProvides Zod Refine capabilities
v.transformProvides Zod Transform capabilities

6. Query Filter Tools

名称说明
v.filterSet the parameters for the Query Filter
v.filterTransformProvides Filter Transform capabilities
v.filterDateRangeConverts date range into a conditional statement

7. Special Tools

NameDescription
v.tableIdentityBased on the current system configuration, provides number or bigint validation rules
v.captchaProvides captcha options

Controller

For a more complete guide on using Controller, see: Controller

Entity

The fields of Entity also support Validation. See: Entity

Dto

The fields of Dto also support Validation. See: Dto

Released under the MIT License.