Numeric validation including constrained integers, floating point numbers, and specialized numeric types. These scalars provide precise validation for various numeric constraints commonly needed in applications.
Validates positive integers (greater than 0).
/**
* GraphQL scalar for positive integer validation
* Accepts integers > 0
*/
const GraphQLPositiveInt: GraphQLScalarType;Example Values:
1421000000Validates negative integers (less than 0).
/**
* GraphQL scalar for negative integer validation
* Accepts integers < 0
*/
const GraphQLNegativeInt: GraphQLScalarType;Example Values:
-1-42-1000000Validates non-negative integers (greater than or equal to 0).
/**
* GraphQL scalar for non-negative integer validation
* Accepts integers >= 0
*/
const GraphQLNonNegativeInt: GraphQLScalarType;Example Values:
01421000000Validates non-positive integers (less than or equal to 0).
/**
* GraphQL scalar for non-positive integer validation
* Accepts integers <= 0
*/
const GraphQLNonPositiveInt: GraphQLScalarType;Example Values:
0-1-42-1000000Validates unsigned integers (non-negative integers).
/**
* GraphQL scalar for unsigned integer validation
* Accepts non-negative integers
*/
const GraphQLUnsignedInt: GraphQLScalarType;Example Values:
014294967295Validates integers within JavaScript's safe integer range.
/**
* GraphQL scalar for safe integer validation
* Accepts integers between Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
* Range: -9007199254740991 to 9007199254740991
*/
const GraphQLSafeInt: GraphQLScalarType;Example Values:
9007199254740991 (Number.MAX_SAFE_INTEGER)-9007199254740991 (Number.MIN_SAFE_INTEGER)42Validates 8-bit unsigned integers (0-255).
/**
* GraphQL scalar for byte validation
* Accepts integers from 0 to 255 (8-bit unsigned)
*/
const GraphQLByte: GraphQLScalarType;Example Values:
0128255Validates positive floating point numbers (greater than 0).
/**
* GraphQL scalar for positive float validation
* Accepts floating point numbers > 0
*/
const GraphQLPositiveFloat: GraphQLScalarType;Example Values:
0.13.1415942.51000000.0Validates negative floating point numbers (less than 0).
/**
* GraphQL scalar for negative float validation
* Accepts floating point numbers < 0
*/
const GraphQLNegativeFloat: GraphQLScalarType;Example Values:
-0.1-3.14159-42.5-1000000.0Validates non-negative floating point numbers (greater than or equal to 0).
/**
* GraphQL scalar for non-negative float validation
* Accepts floating point numbers >= 0
*/
const GraphQLNonNegativeFloat: GraphQLScalarType;Example Values:
0.00.13.1415942.5Validates non-positive floating point numbers (less than or equal to 0).
/**
* GraphQL scalar for non-positive float validation
* Accepts floating point numbers <= 0
*/
const GraphQLNonPositiveFloat: GraphQLScalarType;Example Values:
0.0-0.1-3.14159-42.5Validates unsigned floating point numbers (non-negative floats).
/**
* GraphQL scalar for unsigned float validation
* Accepts non-negative floating point numbers
*/
const GraphQLUnsignedFloat: GraphQLScalarType;Example Values:
0.00.13.14159999999.99Validates JavaScript BigInt values for arbitrarily large integers.
/**
* GraphQL scalar for BigInt validation
* Accepts JavaScript BigInt values for large integers
*/
const GraphQLBigInt: GraphQLScalarType;Example Values:
"123456789012345678901234567890""999999999999999999999999999999""-123456789012345678901234567890"Validates 64-bit signed integers.
/**
* GraphQL scalar for 64-bit signed integer validation
* Accepts integers in the range of 64-bit signed values
*/
const GraphQLLong: GraphQLScalarType;Example Values:
9223372036854775807 (2^63 - 1)-9223372036854775808 (-2^63)0import {
GraphQLPositiveInt,
GraphQLNonNegativeFloat,
GraphQLBigInt,
GraphQLByte
} from "graphql-scalars";
// In a GraphQL schema
const ProductType = new GraphQLObjectType({
name: "Product",
fields: {
id: { type: GraphQLPositiveInt },
price: { type: GraphQLNonNegativeFloat },
quantity: { type: GraphQLByte },
largeNumber: { type: GraphQLBigInt },
},
});
// Using with type definitions
const typeDefs = `
scalar PositiveInt
scalar NonNegativeFloat
scalar BigInt
scalar Byte
type Product {
id: PositiveInt
price: NonNegativeFloat
quantity: Byte
largeNumber: BigInt
}
`;Validation Examples:
// This will validate successfully
const validData = {
id: 42, // PositiveInt: > 0 ✓
price: 29.99, // NonNegativeFloat: >= 0 ✓
quantity: 100, // Byte: 0-255 ✓
largeNumber: "123456789012345678901234567890" // BigInt ✓
};
// These will fail validation
const invalidData = {
id: 0, // PositiveInt: must be > 0 ✗
price: -10.50, // NonNegativeFloat: must be >= 0 ✗
quantity: 300, // Byte: must be 0-255 ✗
largeNumber: "not-a-number" // BigInt: invalid format ✗
};// String constants for schema building
const PositiveInt: string; // "scalar PositiveInt"
const NegativeInt: string; // "scalar NegativeInt"
const NonNegativeInt: string; // "scalar NonNegativeInt"
const NonPositiveInt: string; // "scalar NonPositiveInt"
const UnsignedInt: string; // "scalar UnsignedInt"
const SafeInt: string; // "scalar SafeInt"
const Byte: string; // "scalar Byte"
const PositiveFloat: string; // "scalar PositiveFloat"
const NegativeFloat: string; // "scalar NegativeFloat"
const NonNegativeFloat: string; // "scalar NonNegativeFloat"
const NonPositiveFloat: string; // "scalar NonPositiveFloat"
const UnsignedFloat: string; // "scalar UnsignedFloat"
const BigInt: string; // "scalar BigInt"
const Long: string; // "scalar Long"// Mock data generators for testing
const PositiveIntMock: () => number;
const NegativeIntMock: () => number;
const NonNegativeIntMock: () => number;
const NonPositiveIntMock: () => number;
const UnsignedIntMock: () => number;
const SafeIntMock: () => number;
const ByteMock: () => number;
const PositiveFloatMock: () => number;
const NegativeFloatMock: () => number;
const NonNegativeFloatMock: () => number;
const NonPositiveFloatMock: () => number;
const UnsignedFloatMock: () => number;
const BigIntMock: () => string;
const LongMock: () => number;