Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async user(
@Args('username') username?: string,
@Args('email') email?: string,
): Promise {
let user: User | undefined;
if (username) {
user = await this.usersService.findOneByUsername(username);
} else if (email) {
user = await this.usersService.findOneByEmail(email);
} else {
// Is this the best exception for a graphQL error?
throw new ValidationError('A username or email must be included');
}
if (user) return user;
throw new UserInputError('The user does not exist');
}
public async processInput(value) {
const { rules } = this.options;
const { name } = this.schemaField;
if (rules.required) {
if (value === '' || value === undefined || value === null) {
throw new ValidationError(`Field '${name}' is required`);
}
}
if (rules.urlsafe) {
if (!value.match(/^[A-Za-z][A-Za-z0-9_-]*$/)) {
throw new ValidationError(`Field '${name}' must be url-safe (/^[A-Za-z][A-Za-z0-9_-]*$/)`);
}
}
if (rules.min && rules.minValue) {
const min = Number(rules.minValue);
if (value.length < min) {
throw new ValidationError(
`Field '${name}' must be ${min} character${min === 1 ? '' : 's'} or more`
);
}
}
if (rules.max && rules.maxValue) {
const max = Number(rules.maxValue);
if (value.length > max) {
throw new ValidationError(
if (value === '' || value === undefined || value === null) {
throw new ValidationError(`Field '${name}' is required`);
}
}
if (rules.min && rules.minValue) {
const min = Number(rules.minValue);
if (num < min) {
throw new ValidationError(`Field '${name}' must be greater or equal to ${min}`);
}
}
if (rules.max && rules.maxValue) {
const max = Number(rules.maxValue);
if (num > max) {
throw new ValidationError(`Field '${name}' must be less or equal to ${max}`);
}
}
return value;
}
}
async updateUser(
@Args('username') username: string,
@Args('fieldsToUpdate') fieldsToUpdate: UpdateUserInput,
@Context('req') request: any,
): Promise {
let user: UserDocument | undefined;
if (!username && request.user) username = request.user.username;
try {
user = await this.usersService.update(username, fieldsToUpdate);
} catch (error) {
throw new ValidationError(error.message);
}
if (!user) throw new UserInputError('The user does not exist');
return user;
}