class-validator#ValidateBy TypeScript Examples
The following examples show how to use
class-validator#ValidateBy.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: min-file-size.validator.ts From nestjs-form-data with MIT License | 7 votes |
export function MinFileSize(minSizeBytes: number, validationOptions?: ValidationOptions) {
return ValidateBy({
name: 'MinFileSize',
constraints: [minSizeBytes],
validator: {
validate(value: StoredFile, args: ValidationArguments) {
const size: number = args.constraints[0];
if (isFile(value)) {
return value.size >= size;
}
return false;
},
defaultMessage(validationArguments?: ValidationArguments): string {
return `Maximum file size is ${validationArguments.constraints[0]}`;
},
},
}, validationOptions);
}
Example #2
Source File: has-mime-type.validator.ts From nestjs-form-data with MIT License | 6 votes |
export function HasMimeType(allowedMimeTypes: string[], validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy({
name: 'HasMimeType',
constraints: [allowedMimeTypes],
validator: {
validate(value: StoredFile, args: ValidationArguments) {
const allowedMimeTypes: string[] = args.constraints[0] || [];
if (isFile(value)) {
return allowedMimeTypes.includes(value.mimetype);
}
return false;
},
defaultMessage(validationArguments?: ValidationArguments): string {
const allowedMimeTypes: string[] = validationArguments.constraints[0] || [];
return `File must be of one of the types ${allowedMimeTypes.join(', ')}`;
},
},
}, validationOptions);
}
Example #3
Source File: is-file.validator.ts From nestjs-form-data with MIT License | 6 votes |
export function IsFile(validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy({
name: 'IsFile',
constraints: [],
validator: {
validate(value: any, args: ValidationArguments) {
return isFile(value);
},
defaultMessage(validationArguments?: ValidationArguments): string {
return `Field "${validationArguments.property}" does not contain file`;
},
},
}, validationOptions);
}
Example #4
Source File: max-file-size.validator.ts From nestjs-form-data with MIT License | 6 votes |
export function MaxFileSize(maxSizeBytes: number, validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy({
name: 'MaxFileSize',
constraints: [maxSizeBytes],
validator: {
validate(value: StoredFile, args: ValidationArguments) {
const size: number = args.constraints[0];
if (isFile(value)) {
return value.size <= size;
}
return false;
},
defaultMessage(validationArguments?: ValidationArguments): string {
return `Maximum file size is ${validationArguments.constraints[0]}`;
},
},
}, validationOptions);
}