class-validator#ValidationArguments TypeScript Examples
The following examples show how to use
class-validator#ValidationArguments.
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: UploadSingleFileFSStorage.dto.ts From nestjs-form-data with MIT License | 6 votes |
@IsFile()
@HasMimeType(['text/plain'])
@MaxFileSize(5, {
message: (opt: ValidationArguments) => {
return `${opt.value.path}`;
},
})
@MinFileSize(3)
file: FileSystemStoredFile;
Example #3
Source File: SignatureIdValidator.ts From tatum-blockchain-connector with MIT License | 6 votes |
public validate(value: any, validationArguments?: ValidationArguments) {
const data = validationArguments?.object as any;
if (data.fromPrivateKey && data.signatureId) {
return false;
}
if (data.privateKey && data.signatureId) {
return false;
}
return true;
}
Example #4
Source File: validators.decorator.ts From bank-server with MIT License | 6 votes |
export function IsPassword(
validationOptions?: ValidationOptions,
): PropertyDecorator {
return (object: any, propertyName: string) => {
registerDecorator({
propertyName,
name: 'isPassword',
target: object.constructor,
constraints: [],
options: validationOptions,
validator: {
validate(value: string, _args: ValidationArguments) {
return /^[a-zA-Z0-9!@#$%^&*]*$/.test(value);
},
},
});
};
}
Example #5
Source File: IsEqualTo.ts From tezos-academy with MIT License | 6 votes |
export function IsEqualTo(property: string, validationOptions?: ValidationOptions) {
return (object: any, propertyName: string): any => {
registerDecorator({
name: 'isEqualTo',
target: object.constructor,
propertyName,
constraints: [property],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments): boolean {
const [relatedPropertyName] = args.constraints
const relatedValue = (args.object as any)[relatedPropertyName]
return value === relatedValue
},
defaultMessage(args: ValidationArguments): string {
const [relatedPropertyName] = args.constraints
return `$property must match ${relatedPropertyName} exactly`
},
},
})
}
}
Example #6
Source File: custom-validators.ts From relate with GNU General Public License v3.0 | 6 votes |
export function IsPluginConfig(validationOptions?: ValidationOptions) {
return (object: any, propertyName: string) => {
registerDecorator({
name: 'isPluginConfig',
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
validator: {
validate(value: any, _args: ValidationArguments) {
if (isObjectLike(value)) {
return Object.entries(value).every(([_, v]) => {
if (isArray(v)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return Array.from(v).every(isString);
}
return isString(v) || isBoolean(v);
});
}
return false;
},
defaultMessage(args?: ValidationArguments) {
const expectedMsg = 'Expected "{ [key: string]: string | string[] | boolean }"';
if (!args) {
return expectedMsg;
}
const strValue = JSON.stringify(args.value);
return `${expectedMsg} on "${args.property}" but found "${strValue}" instead`;
},
},
});
};
}
Example #7
Source File: metatype_relationship_pair.ts From Deep-Lynx with MIT License | 6 votes |
// any specific validators should be specified here
export function MetatypeRelationshipPairID(validationOptions?: ValidationOptions) {
return (object: object, propertyName: string) => {
registerDecorator({
name: 'MetatypeRelationshipPairID',
target: object.constructor,
propertyName,
constraints: [],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
return value instanceof MetatypeRelationshipPair && typeof value.id! === 'string';
},
},
});
};
}
Example #8
Source File: metatype_relationship.ts From Deep-Lynx with MIT License | 6 votes |
// any specific validators should be specified here
export function MetatypeRelationshipID(validationOptions?: ValidationOptions) {
return (object: object, propertyName: string) => {
registerDecorator({
name: 'MetatypeRelationshipID',
target: object.constructor,
propertyName,
constraints: [],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
return value instanceof MetatypeRelationship && typeof value.id! === 'string';
},
},
});
};
}
Example #9
Source File: metatype.ts From Deep-Lynx with MIT License | 6 votes |
export function MetatypeID(validationOptions?: ValidationOptions) {
return (object: object, propertyName: string) => {
registerDecorator({
name: 'MetatypeID',
target: object.constructor,
propertyName,
constraints: [],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
return value instanceof Metatype && typeof value.id! === 'string';
},
},
});
};
}
Example #10
Source File: user.ts From Deep-Lynx with MIT License | 6 votes |
// we have to copy the container validator as to avoid cyclical imports
export function ContainerID(validationOptions?: ValidationOptions) {
return (object: object, propertyName: string) => {
registerDecorator({
name: 'ContainerID',
target: object.constructor,
propertyName,
constraints: [],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
return value instanceof Container && typeof value.id === 'string';
},
},
});
};
}
Example #11
Source File: user.ts From Deep-Lynx with MIT License | 6 votes |
/*
These final operations are class-validator specific property validations
*/
export function UserID(validationOptions?: ValidationOptions) {
return (object: object, propertyName: string) => {
registerDecorator({
name: 'UserID',
target: object.constructor,
propertyName,
constraints: [],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
return value instanceof User && typeof value.id === 'string';
},
},
});
};
}
Example #12
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);
}
Example #13
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 #14
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 #15
Source File: match.ts From node-experience with MIT License | 5 votes |
validate(value: any, args: ValidationArguments)
{
const [relatedPropertyName] = args.constraints;
const relatedValue = (args.object as any)[relatedPropertyName];
return value === relatedValue;
}
Example #16
Source File: IsFieldEqual.ts From test with BSD 3-Clause "New" or "Revised" License | 5 votes |
async validate(value: string, args: ValidationArguments) {
const [target] = args.constraints
return value === args.object[target]
}
Example #17
Source File: entity-exists.constraint.ts From aqualink-app with MIT License | 5 votes |
defaultMessage(args: ValidationArguments) {
return `Foreign-key constraint error on ${args.constraints[0].name}.`;
}
Example #18
Source File: data_retention_validator.ts From Deep-Lynx with MIT License | 5 votes |
defaultMessage(args: ValidationArguments) {
// here you can provide default error message if validation failed
return 'Data Retention must be an integer greater than or equal to -1';
}
Example #19
Source File: entity-exists.constraint.ts From aqualink-app with MIT License | 5 votes |
async validate(id: number, args: ValidationArguments) {
const found = await this.connection
.getRepository(args.constraints[0])
.findOne(id);
if (!found) return false;
return true;
}
Example #20
Source File: user.inputs.ts From convoychat with GNU General Public License v3.0 | 5 votes |
defaultMessage(args: ValidationArguments) {
return "Color value ($value) is too dark";
}
Example #21
Source File: user.inputs.ts From convoychat with GNU General Public License v3.0 | 5 votes |
validate(text: string, args: ValidationArguments) {
return !isColorTooDark(text);
}
Example #22
Source File: SignatureIdValidator.ts From tatum-blockchain-connector with MIT License | 5 votes |
public defaultMessage(validationArguments?: ValidationArguments) {
return 'Either signatureId, or privateKey/fromPrivateKey must be present.';
}
Example #23
Source File: unique.validator.ts From NestJs-youtube with MIT License | 5 votes |
async validate(title: string, args: ValidationArguments): Promise<boolean> {
const postRepo: Repository<PostEntity> = await (
await import('../posts.entity')
).default;
return !!!(await postRepo.findOne({ where: { title } }));
}