Registry / testing / graphql-validated-types

graphql-validated-types

JSON →
library2.11.0jsnpmunverified

A collection of GraphQL custom scalar types with fluent API for cleanup, validation, and default values. Version 2.11.0 extends GraphQLScalarType with chainable validators like .length(), .regex(), .trim(), and .base64(). Supports string, number, integer, and custom scalars. Useful for building type-safe GraphQL schemas with built-in sanitization. Compatible with graphql-tools and Apollo. No major updates since 2019, likely in maintenance mode.

npm install graphql-validated-types
INSTALL
IMPORT
SIG · GRAPHQL-VALIDATED-
G
graphql-validated-types
testingjavascriptv2.11.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GraphQLValidatedString
import { GraphQLValidatedString } from 'graphql-validated-types'
const GraphQLValidatedString = require('graphql-validated-types')
ESM import works with bundlers; for Node.js <13, use require but note that CJS export may be nested under .default
GraphQLValidatedScalar
import { GraphQLValidatedScalar } from 'graphql-validated-types'
import GraphQLValidatedScalar from 'graphql-validated-types'
Not a default export; must be destructured from named exports
GraphQLValidatedNumber
import { GraphQLValidatedNumber } from 'graphql-validated-types'
Similar usage as GraphQLValidatedString, but for numeric types

Creates a HexColor scalar type with transformation to uppercase, hex validation, fixed length, and default value.

import { GraphQLValidatedString } from 'graphql-validated-types'; const HexColor = new GraphQLValidatedString({ name: 'HexColor', description: 'A hex color string' }).toUpperCase().hex().length(6).default('000000'); console.log(HexColor.parseValue('ff00ff')); // 'FF00FF' console.log(HexColor.parseValue(undefined)); // '000000' try { HexColor.parseValue('XYZ'); } catch (e) { console.error(e.message); // does not match pattern }
Debug
Known issues
gotchaValidators are run in order; a prior validator's return value is passed to the next, but if throw errors they abort chain.
fix
Ensure cleanups (like .trim()) come before validators to avoid rejecting input with whitespace.
affects: >=1.0.0
deprecatedThe library uses deprecated GraphQLScalarType constructor; may break with graphql-js v16+.
fix
Upgrade to graphql-js v15 or use a newer library like 'graphql-type-json'.
affects: >=2.0.0
gotchaDefault values are only applied when input is undefined, not null.
fix
Explicitly handle null values in your resolvers or use GraphQLNonNull with default.
affects: >=1.0.0
gotchaThe library modifies the input value; .toUpperCase() persists even after .default().
fix
Chain .default() after .toUpperCase() to ensure default value is also uppercased.
affects: >=1.0.0
gotchaGraphQLValidatedNumber and GraphQLValidatedInteger coerce strings to numbers, but may throw on non-numeric strings.
fix
Always pass numbers or parse externally if input is string.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (intermediate value).hex is not a function
Calling .hex() before constructing the string validator (Fluent API order).
fix
new GraphQLValidatedString(...).hex() instead of .hex() on non-instantiated object.
Error: cannot read property 'name' of undefined
Missing `name` option in constructor.
fix
Always provide { name: 'YourType' } when instantiating.
TypeError: Invalid validator: validator must be a function
Passing non-function to .validator() method.
fix
Ensure argument is a function that returns value or throws.
GraphQLError: Expected type to be a GraphQL type
Using a validated type without wrapping in GraphQLNonNull or GraphQLList.
fix
Use new GraphQLNonNull(HexColor) if field is required.
Upgrade
Version history
2.11.0latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency - extends GraphQLScalarType from graphql-js
Agent activity
11 hits · last 30 days
node
10
Resources
graphql-validated-types — npm install graphql-validated-types · libregistry