Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import {
registerDecorator,
ValidationArguments,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface
} from 'class-validator';
@ValidatorConstraint()
export class HasNumberConstraint implements ValidatorConstraintInterface {
validate(value: string, args: ValidationArguments): boolean {
return /\d+/.test(value);
}
}
export function HasNumber(validationOptions?: ValidationOptions) {
return (object: object, propertyName: string) => {
registerDecorator({
name: 'HasNumber',
target: object.constructor,
propertyName,
options: validationOptions,
validator: HasNumberConstraint
});
};
import {
registerDecorator,
ValidationArguments,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface
} from 'class-validator';
@ValidatorConstraint()
export class HasLowerCaseConstraint implements ValidatorConstraintInterface {
validate(value: string, args: ValidationArguments): boolean {
return /[a-z]+/.test(value);
}
}
export function HasLowerCase(validationOptions?: ValidationOptions) {
return (object: object, propertyName: string) => {
registerDecorator({
name: 'HasLowerCase',
target: object.constructor,
propertyName,
options: validationOptions,
validator: HasLowerCaseConstraint
});
};
import {
registerDecorator,
ValidationArguments,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface
} from 'class-validator';
@ValidatorConstraint()
export class HasSpecialCharacterConstraint
implements ValidatorConstraintInterface {
validate(value: string, args: ValidationArguments): boolean {
return /[!@#$%^&*\(\)\-\_+]+/.test(value);
}
}
export function HasSpecialCharacter(validationOptions?: ValidationOptions) {
return (object: object, propertyName: string) => {
registerDecorator({
name: 'HasSpecialCharacter',
target: object.constructor,
propertyName,
options: validationOptions,
validator: HasSpecialCharacterConstraint
});
import {
registerDecorator,
ValidationArguments,
ValidationOptions,
ValidatorConstraint,
ValidatorConstraintInterface,
} from 'class-validator';
@ValidatorConstraint()
export class IsCustomIdConstrain implements ValidatorConstraintInterface {
validate(value: string, args: ValidationArguments) {
const id = value;
return (
id.startsWith(args.constraints[0]) &&
/^[a-z0-9]{8}-[a-z0-9]{4}-4[a-z0-9]{3}-[ab89][a-z0-9]{3}-[a-z0-9]{12}$/.test(
id.substring(args.constraints[0].length),
)
);
}
}
export function IsCustomId(
property: string,
validationOptions?: ValidationOptions,
) {
import {
ValidatorConstraint,
ValidatorConstraintInterface,
ValidationArguments
} from 'class-validator';
import { translate } from '../utils/translate';
@ValidatorConstraint()
export class EqualsToOtherProperty implements ValidatorConstraintInterface {
validate(text: string, args: ValidationArguments) {
const otherProperty =
args.constraints && args.constraints.length ? args.constraints[0] : '';
return args.object[otherProperty] === args.value;
}
defaultMessage(args: ValidationArguments) {
return translate('$property must be equal to $constraint1');
}
}
export function IsEmail(
options?: IsEmailOptions,
validationOptions?: ValidationOptions
) {
return classValidator.IsEmail(
options,
Object.assign(
{
message: "must be a valid email address"
},
validationOptions
)
);
}
@ValidatorConstraint({ async: true })
export class IsUserAlreadyExistByEmailConstraint
implements ValidatorConstraintInterface {
validate(email: any, args: ValidationArguments) {
return getConnection().getRepository(User).findOne({ email }).then(user => {
return !user;
});
}
}
export function IsUserAlreadyExistByEmail(
validationOptions?: ValidationOptions
) {
return function(object: Object, propertyName: string) {
registerDecorator({
target: object.constructor,
propertyName: propertyName,
} from 'class-validator';
import { FindManyOptions, Repository } from 'typeorm';
@ObjectType()
export class PageInfo implements Relay.PageInfo {
@Field(type => Boolean, { nullable: true })
hasNextPage?: boolean | null;
@Field(type => Boolean, { nullable: true })
hasPreviousPage?: boolean | null;
@Field(type => String, { nullable: true })
startCursor?: Relay.ConnectionCursor | null;
@Field(type => String, { nullable: true })
endCursor?: Relay.ConnectionCursor | null;
}
@ValidatorConstraint({ async: false })
class CannotUseWithout implements ValidatorConstraintInterface {
validate(value: any, args: ValidationArguments) {
const object = args.object as any;
const required = args.constraints[0] as string;
return object[required] !== undefined;
}
defaultMessage(args: ValidationArguments) {
return `Cannot be used without \`${args.constraints[0]}\`.`;
}
}
@ValidatorConstraint({ async: false })
class CannotUseWith implements ValidatorConstraintInterface {
validate(value: any, args: ValidationArguments) {
const object = args.object as any;
import { getFromContainer, registerDecorator, ValidationOptions, ValidatorConstraint, ValidatorConstraintInterface } from "class-validator";
import { StarkValidatorImpl } from "../../validator";
import { StarkValidator } from "../../validator.intf";
import { starkIsKBOValidatorName } from "../../validators/is-kbo";
/**
* StarkIsKbo validator constraint
* Validates that the KBO number provided is valid
*/
@ValidatorConstraint({ name: starkIsKBOValidatorName, async: false })
class StarkIsKBOConstraint implements ValidatorConstraintInterface {
/**
* Validates that the given KBO number is valid
* @param kbo - the kbo to validate
* @returns boolean - true if the kbo has been validated
*/
public validate(kbo: string): boolean {
const validator: StarkValidator = getFromContainer(StarkValidatorImpl);
return validator.starkIsKBO(kbo);
}
/**
* Default message displayed if the KBO number is not valid
* @returns a default message
*/
import { ValidationArguments, ValidatorConstraint, ValidatorConstraintInterface } from "class-validator";
@ValidatorConstraint({ name: "primitiveArrayValidation", async: false })
export class ValidatePrimitiveArray implements ValidatorConstraintInterface {
public validate(value: any, args: ValidationArguments) {
const [ elementClass ] = args.constraints;
return value instanceof Array &&
value.every((el) => el && el.constructor === elementClass);
}
}
}
@ValidatorConstraint({ async: false })
class CannotUseWithout implements ValidatorConstraintInterface {
validate(value: any, args: ValidationArguments) {
const object = args.object as any;
const required = args.constraints[0] as string;
return object[required] !== undefined;
}
defaultMessage(args: ValidationArguments) {
return `Cannot be used without \`${args.constraints[0]}\`.`;
}
}
@ValidatorConstraint({ async: false })
class CannotUseWith implements ValidatorConstraintInterface {
validate(value: any, args: ValidationArguments) {
const object = args.object as any;
const result = args.constraints.every(propertyName => {
return object[propertyName] === undefined;
});
return result;
}
defaultMessage(args: ValidationArguments) {
return `Cannot be used with \`${args.constraints.join('` , `')}\`.`;
}
}
@ArgsType()
export class ConnectionArgs implements Relay.ConnectionArguments {