How to use the apollo-server-core.ValidationError function in apollo-server-core

To help you get started, we’ve selected a few apollo-server-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github EricKit / nest-user-auth / src / users / users.resolvers.ts View on Github external
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');
  }
github birkir / prime / packages / prime-field-string / src / PrimeFieldString.ts View on Github external
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(
github birkir / prime / packages / prime-field-number / src / PrimeFieldNumber.ts View on Github external
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;
  }
}
github EricKit / nest-user-auth / src / users / users.resolvers.ts View on Github external
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;
  }