A configurable custom input number type for GraphQL with sanitization and validation.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Support additional type config like 'description' Jan 23, 2017
test Support additional type config like 'description' Jan 23, 2017
.babelrc use babel-plugin-add-module-exports to support module.exports Sep 4, 2017
.eslintrc Initial working version Jun 9, 2016
.gitignore .gitignore Jun 9, 2016
.npmignore coverage Jun 9, 2016
.travis.yml Update .travis.yml Apr 16, 2017
LICENSE
README.md Support additional type config like 'description' Jan 23, 2017
package.json use babel-plugin-add-module-exports to support module.exports Sep 4, 2017

README.md

graphql-input-number

Build Status Coverage Status npm version Dependency Status License

A configurable custom input number type for GraphQL with sanitization and validation.

Checkout graphql-input-string for validating string inputs.

Install

npm install --save graphql-input-number

Usage

import {
  GraphQLInputInt,
  GraphQLInputFloat,
} from 'graphql-input-number';

const argType = GraphQLInputInt({
  name: 'OneToNineInt',
  min: 1,
  max: 9,
});

new GraphQLObjectType({
  name: 'Query',
  fields: {
    input: {
      type: GraphQLInt,
      args: {
        number: {
          type: argType,
        },
      },
      resolve: (_, {number}) => {

        // 'number' IS AN INT BETWEEN 1 to 9.

      };
    },
  },
});

Options

GraphQLInputInt({
  // Type name.
  // [REQUIRED]
  name: string = null,

  // Sanitize function that is called at the end of sanitzation phase and before
  // validation phase.
  sanitize: ((number) => number) = null,

  // Minimum value allowed (inclusive).
  min: number = null,

  // Maximum value allowed (inclusive).
  max: number = null,

  // Test function that is called at the end of validation phase.
  test: ((number) => boolean) = null,

  // Custom error handler.
  // May throw an error or return a value.
  // If a value is returned, it will become the final value.
  error: ErrorHandler = () => throw GraphQLError,

  // Parse function that is called after validation phase before returning a
  // value.
  // May throw an error or return a value.
  parse: ((number) => any) = null,

  // If you want to pass some config to type constructor, simply add them here.
  // For example,
  description: string,
});

GraphQLInputFloat({
  ...same as GraphQLInputInt
});


type ErrorInfo = {
  type: string,
  value: number,
  message: ?string,
  ast: ?Ast,
  ...args,
};


type ErrorHandler = (ErrorInfo) => any;

License

The MIT License (MIT)

Copyright (c) 2016 Joon Ho Cho

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.