npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

gql-error

v0.7.0

Published

Used to throw graphql error during validation

Readme

A simple package for throwing graphql validation error with localization.

Installing

npm install --save gql-error

How to use

This library provides two function validate() and error()
validate() can be used for form validations and error() can be used to throw normal graphql error.

validate(input, rules[, options]) takes 3 parameter
First parameter: input object we get from graphql. Ex: {email: "[email protected]", password: "gqlvalidator"}
Second parameter: rules for validation (example below).
Third parameter: options for localization and others.

error(message[, options]) takes 2 parameter
First parameter: concatenated string that matches json path in config files as shown below.
Second parameter: same as above third parameter.

localizeString(message[, options]) takes 2 parameter (This API actually doesn't throw any error but returns a localized message from our locale config files.) First parameter: concatenated string that matches json path in config files as shown below.

Options
  • lang (string) - Which localization to use. Default 'en'.
  • gql (boolean) - If true uses throws graphql error. If false, throws error as original nodejs error. Default true.

Example

Authentication example:
auth.resolver.js

import GQLError from 'gql-validator';
import rules from './auth.validation';

const resolvers = {
  Mutation: {
    login: (_, { input }, req) => {
      let lang = req.header('Accept-Language'); //en
      GQLError.validate(input, rules.login(), { lang: lang });

      return User
        .findOne({email: input.email})
        .then(user => {
          if (!user) GQLError.error('user.email.notFound', {lang: lang});

          if (input.password == 'gqlvalidator') {
            GQLError.error('user.password.incorrect', {lang: lang});
          }

          return user;
        })
        .catch(err => {
          throw err;
        })
    },

    register: (_, { input }, req) => {
      let lang = req.header('Accept-Language'); //se
      GQLError.validate(input, rules.register(), { lang: lang });

      return User
        .findOne({ where: { email: input.email } })
        .then(res => {
            if (res) GQLError.error('user.email.notAvailable', { lang: lang });

            return User
              .create({
                email: input.email,
                password: input.password
              })
              .then(user => {
                return user;
              })
              .catch(err => {
                throw err;
              });
        })
        .catch(err => {
          throw err;
        });
    }

validation rules (I used a different file ./auth.validation.js)
To learn more about rule convention, see how to make constraints here: http://validatejs.org

const rules = {
  register: () => {
    return {
      email: {
        presence: { message: "user.email.required" },
        email: { message: "user.email.invalid" }
      },
      password: {
        presence: { message: "user.password.required" },
        length: {
          minimum: 3, message: "user.password.minimum"
        }
      }
    };
  },
  
  login: () => {
    // rules here
  }
}

Message config files: en.json

{
  "user": {
    "email": {
      "required": "Email is required",
      "invalid": "Invalid email address",
      "notAvailable": "Email address not available",
      "notFound": "Email address not found"
    },
    "password": {
      "required": "Password is required",
      "minimum": "Minimum 3 character required",
      "incorrect": "Password is incorrect"
    }
  }
}

se.json (Used google translator, you get the idea :D)

{
  "user": {
    "email": {
      "required": "E-post krävs",
      "invalid": "Ogiltig e-postadress",
      "notAvailable": "E-postadressen är inte tillgänglig",
      "notFound": "E-postadressen hittades inte"
    },
    "password": {
      "required": "Lösenord krävs",
      "minimum": "Minst 3 tecken krävs",
      "incorrect": "Lösenord är inkorrekt"
    }
  }
}

You need to create separate files for different locales. Name should match exactly what you will use in option {lang: en}.

Example:
app\
--config\
----locales
------en.json
------se.json
------fr.json

For now your json file structure and path should look exactly like above.

Note:

In order to provide lang option either you will need to have access to 'Accept-Language' header option found in request inside graphql resolver or you need to find a way to provide lang option for third parameter for localisation. In above example I have passed req object as a third parameter in resolver.