Skip to content

@atscript/moost-validator – API Reference ​

@atscript/moost-validator bridges Atscript-annotated types with the Moost pipeline system. It exposes a handful of helpers; everything else (advanced options, CLI usage, custom plugins) lives in the Atscript documentation.

Exports ​

ExportTypeSummary
validatorPipe(opts?)TPipeFnValidates every value whose design type was generated by Atscript (type.validator(opts).validate(value)).
UseValidatorPipe(opts?)DecoratorSyntactic sugar for attaching validatorPipe() to classes, methods, or parameters.
validationErrorTransform()TInterceptorDefConverts ValidatorError into HttpError(400) so clients see a structured validation response instead of 500.
UseValidationErrorTransform()DecoratorShortcut for applying validationErrorTransform() at controller/handler level.

All exports are tree-shakeable. Peer dependencies: moost, @atscript/core / @atscript/typescript, and @moostjs/event-http (required by validationErrorTransform / UseValidationErrorTransform, which build an HttpError).

validatorPipe(opts?) ​

Attach once globally or locally when you need validation.

ts
import { validatorPipe } from '@atscript/moost-validator'

app.applyGlobalPipes(validatorPipe())

opts accepts any subset of TValidatorOptions (e.g. { partial: 'deep', errorLimit: 50 }). Options are forwarded directly to Atscript’s validator.

UseValidatorPipe(opts?) ​

ts
import { Controller } from 'moost'
import { Body, Post } from '@moostjs/event-http'
import { UseValidatorPipe } from '@atscript/moost-validator'
import { UpdateUserDto } from './update-user.dto.as'

@Controller('users')
export class UsersController {
  @Post()
  @UseValidatorPipe({ partial: true })
  async patch(@Body() dto: UpdateUserDto) {}
}

validationErrorTransform() ​

Without this interceptor a failed validation becomes an unhandled error (500). With it, Moost responds with 400 Bad Request and the validator details.

ts
import { validationErrorTransform } from '@atscript/moost-validator'

app.applyGlobalInterceptors(validationErrorTransform())

Example response:

json
{
  "statusCode": 400,
  "message": "email: Invalid email",
  "error": "Bad Request",
  "_body": [
    { "path": "email", "message": "Invalid email" }
  ]
}

The message is the first failing rule ("<path>: <message>"); the full error list is under the _body key.

UseValidationErrorTransform() ​

ts
import { UseValidationErrorTransform } from '@atscript/moost-validator'
import { CreateUserDto } from './create-user.dto.as'

@Post()
@UseValidationErrorTransform()
async create(@Body() dto: CreateUserDto) {}

Apply alongside UseValidatorPipe() when you want handler-level configuration instead of app-wide defaults.


Need more? See the validation overview for workflow guidance and the Atscript docs for DTO authoring, CLI usage, and plugin recipes.

Released under the MIT License.