A configurable custom input number type for GraphQL with sanitization and validation.
Checkout graphql-input-string for validating string inputs.
npm install --save graphql-input-number
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.
};
},
},
});
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;
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.