validateAsync - asynchronous validation
Automatically throw an error if validation fails
quick start
Basic usage
ValidationError
property
method
1. toJSON() - Convert to JSON (for API responses)
2. getFieldError(field) - Get the error of the specified field
3. getFieldErrors() - Get error mapping for all fields
4. hasFieldError(field) - Check the field for errors
5. getErrorCount() - Get the number of errors
Express integration
Basic integration
Complete CRUD example
⚠️
SchemaUtilsis currently only responsible for generating derived schemas (such aspick()/omit()/partial()/extend()), and will not automatically delete extra fields on the runtime object. When returning a public response, please explicitly project the data at the business layer first, and then usevalidate()to verify the projection result.
Error handling
Custom error format
Classification error handling
logging
Complete example
User Registration API
API reference
validateAsync(schema, data, options?)
Parameters:
schema- JSON Schema or DslBuilder instancedata- Data to be validatedoptions- Authentication options (optional)locale- Language settings (such as 'zh-CN', 'en-US')format- Whether there is a formatting error (default true)
return value:
- Validation passed: return processed data
- Authentication failed: throw
ValidationError
Example:
ValidationError class
Constructor:
property:
name: 'ValidationError'message: string- Friendly error messageerrors: Array<Object>- Formatted list of error items (such aspath,message,keyword,params)data: any- raw input datastatusCode: 400- HTTP status code
method:
toJSON()- Convert to JSON formatgetFieldError(field)- Gets the error for the specified fieldgetFieldErrors()- Get error mapping for all fieldshasFieldError(field)- Check the field for errorsgetErrorCount()- Get the number of errors
Related documents
- SchemaUtils chain call - Schema reuse simplified method
- validate.md - Traditional synchronous validation method
- error-handling.md - Error handling guide
- validate-async complete example - Top-level
validateAsync(),ValidationErrorand async.custom()examples
Corresponding sample file
Example entry: validate-async.ts
Description: Covers the success path of validateAsync(), ValidationError capture, Promise-returning .custom(), and the optional method of business layer asynchronous checking after structure validation.