本文整理汇总了TypeScript中class-validator.validate函数的典型用法代码示例。如果您正苦于以下问题:TypeScript validate函数的具体用法?TypeScript validate怎么用?TypeScript validate使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了validate函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createUser
public static async createUser (ctx: BaseContext) {
// get a user repository to perform operations with user
const userRepository: Repository<User> = getManager().getRepository(User);
// build up entity user to be saved
const userToBeSaved: User = new User();
userToBeSaved.name = ctx.request.body.name;
userToBeSaved.email = ctx.request.body.email;
// validate user entity
const errors: ValidationError[] = await validate(userToBeSaved); // errors is an array of validation errors
if (errors.length > 0) {
// return bad request status code and errors array
ctx.status = 400;
ctx.body = errors;
} else {
// save the user contained in the POST body
const user = await userRepository.save(userToBeSaved);
// return created status code and updated user
ctx.status = 201;
ctx.body = user;
}
}
示例2: beforeInsert
async beforeInsert(event: InsertEvent<any>) {
const validateErrors = await validate(event.entity)
if (validateErrors.length > 0) {
return Promise.reject({
status: 403,
message: getErrorMessage(validateErrors),
error: validateErrors
})
}
}
示例3: transform
async transform(value, metadata: ArgumentMetadata) {
const { metatype } = metadata;
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
throw new BadRequestException('Validation failed');
}
return value;
}
示例4: transform
async transform(value: any, metadata: ArgumentMetadata) {
const { metatype } = metadata;
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
console.log('Got these validation errors: ', errors);
throw new HttpException('Validation failed', HttpStatus.BAD_REQUEST);
}
return value;
}
示例5: beforeUpdate
async beforeUpdate(event: UpdateEvent<any>) {
const validateErrors = await validate(event.entity, {
// 更新操作不会验证没有涉及的字段
skipMissingProperties: true
})
if (validateErrors.length > 0) {
return Promise.reject({
status: 403,
message: getErrorMessage(validateErrors),
error: validateErrors
})
}
}
示例6: function
return function(
request: express.Request,
response: express.Response,
next: express.NextFunction
): any {
return validate(new type(request.body))
.then((errors: ValidationError[]) => {
if (errors.length) {
response.status(400).json(errors);
} else {
next();
}
});
};
示例7: transform
async transform(value: any, metadata: ArgumentMetadata) {
const { metatype } = metadata;
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
let e = this.List['bad_request'];
throw new HttpException(e, e.status);
}
return value;
}
示例8: transform
async transform(value, metadata: ArgumentMetadata) {
const { metatype } = metadata;
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object, { whitelist: true });
if (errors.length > 0) {
errors.forEach(err => {
throw new BadRequestException(err.constraints);
});
}
return object;
}
示例9: EntityClass
export default async <Entity extends Obj>(EntityClass: ObjectType<Entity>, value?: Entity & any, isUpdate = false): Promise<Entity> => {
const entityInstance = new EntityClass()
Object.keys(value).map(key => {
entityInstance[key] = value[key]
})
const validateErrors = await validate(entityInstance, {
// 更新操作不会验证没有涉及的字段
skipMissingProperties: isUpdate
})
if (validateErrors.length > 0) {
throw Error(validateErrors.toString())
}
return entityInstance
}
示例10: transform
//PipeTransform介面有transform(),這可以轉換input data並回傳
async transform(value, metadata: ArgumentMetadata) {
const { metatype } = metadata;
/*不檢查原生JavaScript的型別,因為刻意寫ValidationPipe,就是要使用自定義的DTO class
的屬性去做參數型別檢查,如果metatype是原生JavaScript的型別,就直接return 原始參數,
不做ValidationPipe的檢查。
*/
if (!metatype || !this.toValidate(metatype)) {
return value;
}
//這裡使用class-transformer的方法,將plain javascript object(像是JSON object),轉換成一個class的object。
const object = plainToClass(metatype, value);
//在CreateUserDTO,我們有使用class-validator的驗證裝飾器,validate()會回傳錯誤陣列。
const errors = await validate(object);
if (errors.length > 0) {
console.log('validate errors',errors);
throw new HttpException('檢驗錯誤', HttpStatus.BAD_REQUEST);
}
//ValidationPipe是特別打造出來檢驗型別用,最後要回傳原始參數,避免覆寫。
return value;
}